Top 100 Programs Part 2
Top 100 Programs Part 2
The HCF or the Highest Common Factor of two numbers is the largest common
factor of two or more values. The HCF can be calculated using some simple
mathematical tricks. The following algorithm will determine how a c program
can calculate the HCF of two numbers.
#include <stdio.h>
int main()
{
//for initialize variables
int a, b, i, hcf;
a = 12;
b = 16;
//find hcf of number
for(i = 1; i <= a || i <= b; i++)
{
if( a%i == 0 && b%i == 0 )
hcf = i;
}
//display hcf
printf("HCF = %d", hcf);
return 0;
}
Question 2:
Write a C program to find the LCM of two number.
The Least Common Multiple (LCM) is also referred to as the Lowest Common
Multiple (LCM) and Least Common Denominator (LCD). The least common
multiple, or LCM, is another number that’s useful in solving many math
problems. Let’s find the LCM of 12 and 44. One way to find the least common
multiple of two numbers is to first list the prime factors of each number.
12 = 2 × 2 × 3
44 = 2 × 2 × 11
#include<stdio.h>
void lcm_two_no(int,int);
int main()
{
int n1,n2;
Question 3:
Write a C program to find GCD or HCF of Two Numbers
The Highest Common Multiple or the Greatest Common Divisor is the greatest
number that exactly divides both numbers. It is possible to calculate this
number through simple mathematical calculations. The following algorithm
shows how the GCD of two numbers is calculated.
Ex:-
// checking divisibility by 0
if (p == 0)
return q;
if (q == 0)
return p;
// base case
if (p == q)
return p;
// p is greater
if (p > q)
return gcd(p-q, q);
else
return gcd(p, q-p);
}
Question 4:
Binary to decimal conversion:-
To show on fig(1)
#include<stdio.h>
int main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
//num/=10;
num = num / 10 ;
//base*=2;
base = base * 2;
}
//display binary number
printf("The Binary num is = %d \n", binary_val);
//display decimal number
printf("Its decimal equivalent is = %d \n", decimal_val);
return 0;
}
Question 5:
Binary to Octal Conversion:-
Binary to octal conversion can be easily done with the help of simple
calculations. The following section includes a stepwise procedure for such a
conversion. In this process, a binary number is inputted by a user and is later
converted to an octal number.
#include<stdio.h>
int main()
{
//For initialize variables
long int binary_num, octal_num = 0, j = 1, rem;
//Inserting the binary number from the user
while(binary_num != 0)
{
return 0;
}
Question 6:
Decimal to binary conversion:-
Ex:- (180)10=(11101000)2
/*
* C program to accept a decimal number and convert it to binary
* and count the number of 1's in the binary number
*/
#include<stdio.h>
int main()
{
//for initialize a variables
long number, dec_num, rem, base = 1, bin = 0, count = 0;
//To insert a number
printf("Insert a decimal num \n");
scanf("%ld", &number);
dec_num = number;
while(number > 0)
{
rem = number % 2;
/* To count no.of 1s */
if (rem == 1)
{
count++;
}
Question 7:
Decimal to Octal Conversion:-
(143)10=(217)8
int main()
{
//Variable initialization
long dec_num, rem, quotient;
int i, j, octalno[100];
//Taking input from user
printf("Enter a number for conversion: ");
//Storing the value in dec_num variable
scanf("%ld",&dec_num);
quotient = dec_num;
i=1;
//Storing the octal value in octalno[] array
while (quotient!=0)
{
octalno[i++]=quotient%8;
quotient=quotient/8;
}
//Printing the octalno [] in reverse order
printf("\nThe Octal of %ld is:\n\n",dec_num);
for (j=i-1;j>0;j--)
//display it
printf ("%d", octalno[j]);
return 0;
}
Question 8:
Octal to binary conversion:-
/*
* C Program to Convert Octal to Binary number
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char octalnum[MAX];
//For initialize
long i = 0;
//taking user input of octalnum
printf("Insert an octal number: ");
scanf("%s", octalnum);
printf("Equivalent binary number: ");
while (octalnum[i])
{
//use switch case for multiple condition
switch (octalnum[i])
{
case '0':
printf("000"); break;
case '1':
printf("001"); break;
case '2':
printf("010"); break;
case '3':
printf("011"); break;
case '4':
printf("100"); break;
case '5':
printf("101"); break;
case '6':
printf("110"); break;
case '7':
printf("111"); break;
//for invalid case
default:
printf("\n Invalid octal digit %c ", octalnum[i]);
return 0;
}
i++;
}
return 0;
}
Question 9:
Octal to Decimal Conversion:-
It is easy to convert an octal number to decimal number. For this, the user is
asked to enter an octal number which is converted to a decimal number
following a series of steps. The algorithm below illustrates this process in a
step wise process. It is then followed by a C program that converts an Octal
number to a decimal number.
#include<stdio.h>
#include<math.h>
int main()
{
//Variable Initialization
long int oct, dec = 0;
int i = 0;
//Taking input from user
printf("Enter an octal number: ");
scanf("%ld", &oct);
//Conversion of octal to decimal
while(oct != 0)
{
dec = dec +(oct % 10)* pow(8, i++);
//oct/=10;
oct = oct / 10;
}
//display
printf("Equivalent decimal number: %ld",dec);
return 0;
}
Question 10:
Write a C program to find Quadrants in which coordinates lie:-
#include<stdio.h>
int main()
{
//for initialization of coordinates
int x, y; //user input
printf("Insert the value for variable X and Y\n");
scanf("%d %d", &x, &y);
//find true condition of first quadrant
if (x > 0 && y > 0)
printf("point (%d, %d) lies in the First quadrant\n",x,y);
//find second quadrant
else if (x < 0 && y > 0)
printf("point (%d, %d) lies in the Second quadrant\n",x,y);
//To find third quadrant
else if (x < 0 && y < 0)
printf("point (%d, %d) lies in the Third quadrant\n",x,y);
//To find Fourth quadrant
else if (x > 0 && y < 0)
printf("point (%d, %d) lies in the Fourth quandrant\n",x,y);
//To find dose not lie on origin
else if (x == 0 && y == 0)
printf("point (%d, %d) lies at the origin\n",x,y);
return 0;
}
Question 11:
N students are looking to find r seats in a classroom. Some of the seats are
already occupied and only a few can accommodate in the classroom. The
available seats are assumed as r and n number of people are looking to
accommodate within the room.
#include<stdio.h>
scanf("%ld",&n);
scanf("%ld",&r);
// Base condition
// Swap n and r when n is less than r
if(n < r)
{
temp=n;
n=r;
r=temp;
}
numerator=fact(n);
denominator=fact(n-r);
permutation=numerator/denominator;
printf("\nNum of ways people can be seated : ");
printf("%ld\n",permutation);
}
Question 12:
Write a C program to find Maximum number of handshakes:-
Working:-
Step 1: Start
Step 2: The user is asked to insert an integer value n, representing the number
of people
Step 5: Stop
Question 13:
To find the sum of two fractions we will be using the concept of LCM and GCD.
Working:-
Step 1. Start.
Step 4. Find numerator using this condition (n1*d2) +(d1*n2 ) where n1,n2 are
numerator and d1 andd d2 are denominator .
Step 8. Stop.
#include <stdio.h>
int main()
{
//for initialize variables
int numerator1, denominator1,numerator2,denominator2,x,y,c,gcd_no;
//To take user input of numerators and denominators
printf("\nEnter the numerator for 1st number : ");
scanf("%d",&numerator1);
printf("\nEnter the denominator for 1st number : ");
scanf("%d",&denominator1);
printf("\nEnter the numerator for 2nd number : ");
scanf("%d",&numerator2);
printf("\nEnter the denominator for 2nd number : ");
scanf("%d",&denominator2);
//numerator
x=(numerator1*denominator2)+(denominator1*numerator2);
//denominator
y=denominator1*denominator2;
// Trick part. Reduce it to the simplest form by using gcd.
for(c=1; c <= x && c <= y; ++c)
{
if(x%c==0 && y%c==0)
gcd_no = c;
}
//To display fraction of givien numerators and denominators
printf("\nThe added fraction is %d/%d ",x/gcd_no,y/gcd_no);
printf("\n");
return 0;
}
Question 14:
Replace all 0’s with 1 in a given integer:-
The replace all program in C programming works to replace the numbers with
zero, where the number must be an integer. All the zeros (if encountered) in
the given program will be replaced by 1.
Ex- number is 12004509 all 0’s are replays of 1’s so number is 12114519.
Question 15:
#include<stdio.h>
int sum_of_two_primes(int n);
int main()
{
int n, i;
}
}
if(flag == 0)
printf(“%d cannot be expressed as the sum of two primes\n”, n)
return 0;
}
int sum_of_two_primes(int n)
{
int i, isPrime = 1;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = 0;
break;
}
}
return isPrime;
}
Question 16:
Count possible decodings of a given digit sequence:-
The decoding programs are the most possible questions asked and are largely
practiced in C programming. The program counts the number of possible
decodings of the entered digit by the user of the given sequence.
For example, if the digit sequence is “12” then there are two possible
decodings for this – One of them is ‘AB’ when we decode 1 as ‘A’ and 2 as ‘B’.
Now we can also decode this digit sequence “12” as ‘L’ when we decode digits
1 and 2 taken together as an integer 12.
// In case second last digit is smaller than 2 and last digit is smaller than 7,
then last two digits form a valid character
if (dig[k-2] == '1' || (dig[k-2] == '2' && dig[k-1] < '7') )
cnt[k] += cnt[k-2];
}
return cnt[a];
}
int main()
{
char dig[15];
printf("\n Enter the sequence : ");
gets(dig);
int a = strlen(dig);
printf("\n Possible count of decoding of the sequence : %d\n",
cnt_decoding_digits(dig, a));
return 0;
}
Question 17:
A character is a vowel or consonant using C
Given a character, check if it is vowel or consonant. Vowels are in Uppercase
‘A’, ‘E’, ‘I’, ‘O’, ‘U’ and Lowercase ‘a’, ‘e’, ‘i’, ‘o’, ‘u’. and All other characters
both uppercase and lowercase (‘B’, ‘C’, ‘D’, ‘F’,’ b’, ‘c’,’ d’, ‘f’,…..) are
consonants. In this article, we will show you, How to write a C program to
check Vowel or Consonant with an example.
#include <stdio.h>
int main()
{
char c;
int isLowerVowel, isUpperVowel;
printf("Enter an alphabet: ");
scanf("%c",&c);
if (isLowerVowel || isUpperVowel)
printf("%c is a vowel", c);
//to check character is alphabet or not
else
printf("%c is a consonant", c);
return 0;
}
Question 18:
Character is alphabet or not:
The C program checks whether the character entered is an alphabet or not.
The program makes use of character value inserted by the user. This value can
range between lowercase or uppercase alphabets, such as ‘a’ and <= ‘z’ and ‘A’
and <= ’Z’. If the character inserted by the user lies between the above
category or ranges then the character is an alphabet and if it does not lie
within the given range then it is not.
else
printf("The entered character %c is not an Alphabet”, a);
return 0;
}
Question 19:
Finding Area of a circle
I am going to tell you about finding an area of a circle in C programming.The
area of a circle is number of square units inside the circle. Standard formula to
calculate the area of a circle is: A=πr² .where π =22/7( value is 3.141 )
You can compute the area of a Circle if you know its radius, by simple formula
A = 3.14 * r * r in C programming.We allow user to enter radius, and then find
the area of the cirle.
#include<stdio.h>
int main()
{
area = pi*radius*radius;
Question 21:
Prime Number
we know that,the whole number are the basic counting number 0,1,2,3….and
so on.So A whole number greater than 1 that can not be made by multiplying
other whole numbers.
So prime number has two factor is 1 and number itself is called prime
numberThe number 1 is neither prime nor composite.
//Main function
int main()
{
int i,j,count=0;
for(i=2;i<=100;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d",i);
count=0;
}
}
Question 21:
Finding the number of digits in an integer
Today, we will be learning how to code a C program for finding the digits in an
integer entered by a user. An integer is made up of a group of digits, i.e; it is a
combination of digits from 0-9
Here we will use loops along with an arithmetic operator.This program takes
an integer from the user and calculates the number of digits. For example: If
the user enters 6589, the output of the program will be 4.
#include <stdio.h>
int main()
{
//to initialize of variable
int count=0;
int n,c;
else
{
while(n!=0)
{
//find last digit of number
n=n/10;
//count of a number
++count;
}
printf("the total digit in giving number %d is : %d",c,count);
}
return 0;
}
Question 22:
Algorithm
//C++ program
//convert number to text
#include<iostream>
#include<string.h>
using namespace std;
//main Program
void numToWords(string num)
{
int length_of_string = strlen(num);
if (length_of_string == 0){
cout<<“String is Empty”;
return;
}
Question 22:
Number of days in a given month of a given year
Number of days in any month of a year can vary specifically in February as the
cycle of leap year repeats in every 4 years when the year is leap February gives
the count to 29 days but the when the year is not leap it gives count to 28 days
and so no of days in a year varies from 365 to 366.
Rather than February every month gives the count of 30 or 31 days in any case
whether the year is a leap or not.
#include <stdio.h>
int main()
{
int month, year;
printf("enter the month : ");
scanf("%d",&month);
printf("enter the year : ");
scanf("%d",&year);
if(((month==2) && (year%4==0)) || ((year%100==0)&&(year%400==0)))
{
printf("Number of days is 29");
}
else if(month==2)
{
printf("Number of days is 28");
}
else if(month==1 || month==3 || month==5 || month==7 || month==8 ||
month==10 || month==12)
{
printf("Number of days is 31");
}
else
{
printf("Number of days is 30");
}
return 0;
}
Question 23:
Occurrence of a Digit in a given Number
we will count the number of occurrences of a given digit in the given input
number.
The input may lie within the range of integer.
If the digit does not occur in the input it should print 0 else the count of digits.
Sample Input :
Enter a number : 897982
Enter the digit : 9
Output : 2
#include <stdio.h>
int main() {
int n;
int d;
int count=0;
scanf(“%d%d“,&n,&d);
while(n)
{
int k = n%10;
n=n/10;
if(k==d)
{
count++;
}
}
printf(“%d“,count);
return 0;
}
Question 24:
Number of integers which has exactly X divisors
In this page we will learn how to find number of integers which has exactly x
divisors. Divisors are numbers which perfectly divides a number. For example
take 6 as a number then divisors of 6 are 1,2,3,6. ‘1’ and number itself are
always divisors of the same number. If a number has exactly 2 divisors then
that number is a prime number.
Here our task is to write a C program to count out how many number of
integers which has exactly x divisors. This can be done by finding number of
divisors that each number has and then checking with the given ‘X‘. If X and
number of divisors match then we will increment the counter value.
Example:
Input
Number=30
X=3
Output
#include <stdio.h>
int main()
{
int Number,X,count1;
printf(“\nEnter range of number :”);
scanf(“%d“,&Number);
printf(“\nEnter exact number of divisors :”);
scanf(“%d“,&X);
count1 = 0;
for(int i=1;i<=Number;i++)
{
int count2 = 0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count2++;
}
}
if(count2==X)
{
count1++;
printf(“%d “,i);
}
}
printf(“\n%d“,count1);
return 0;
}
Question 25:
Finding Roots of a Quadratic Equation
In this C program, we will find the roots of a quadratic equation [ax2 + bx + c].
We can solve a Quadratic Equation by finding its roots. Mainly roots of the
quadratic equation are represented by a parabola in 3 different patterns like :
No Real Roots
One Real Root
Two Real Roots
We get the roots of the equation which satisfies any one of the above
conditions :
X = [-b (+or-)[Squareroot(pow(b,2)-4ac)]]/2a
Enter value of a :1
Enter value of b :-7
Enter value of c :12
Output
Two Real Roots
4.0
3.0
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, d, root1, root2, r, i;
printf(“Enter value of a, b and c: “);
scanf(“%lf %lf %lf”, &a, &b, &c);
d = b * b – 4 * a * c;
return 0;
}