0% found this document useful (0 votes)
16 views33 pages

PROGRAMMING QUESTIONS-1

The document contains a series of C programming exercises that cover various topics such as finding maximum values, checking even/odd numbers, calculating areas and perimeters, and performing arithmetic operations. It also includes exercises on bitwise operations, conditional statements, and input/output for different data types. Each exercise is presented with a code snippet that demonstrates the required functionality.

Uploaded by

harshrastogi2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views33 pages

PROGRAMMING QUESTIONS-1

The document contains a series of C programming exercises that cover various topics such as finding maximum values, checking even/odd numbers, calculating areas and perimeters, and performing arithmetic operations. It also includes exercises on bitwise operations, conditional statements, and input/output for different data types. Each exercise is presented with a code snippet that demonstrates the required functionality.

Uploaded by

harshrastogi2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Write a C program to find maximum between two numbers using conditional operator.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b;
printf("Enter the no 1:");
scanf("%d",&a);
printf("\nEnter the no 2:");
scanf("%d",&b);
if(a>b)
{
printf("%d is greater than %d",a,b);
}
printf("%d is greater than %d",b,a);
return 0;
}

Write a C program to find maximum between three numbers using conditional operator.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,c;
printf("Enter the no 1:");
scanf("%d",&a);
printf("\nEnter the no 2:");
scanf("%d",&b);
printf("\nEnter the no 3:");
scanf("%d",&c);
if(a>b && a>c)
{
printf("\n%d is greater than %d and %d",a,b,c);
}
else if(b>a && b>c)
{
printf("\n%d is greater than %d and %d",b,a,c);
}
else
printf("\n%d is greater than %d and %d",c,a,b);
return 0;
}

Write a C program to check whether a number is even or odd using conditional


operator.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int no;
printf("Enter the number:");
scanf("%d",&no);

if(no%2==0)
{
printf("\n%d is an even number",no);
}
else

printf("\n%d is an odd number",no);

return 0;
}

Write a C program to check whether year is leap year or not using conditional
operator.
Write a C program to check whether character is an alphabet or not using
conditional operator.

Write a C program to perform input/output of all basic data types.


Write a C program to enter two numbers and find their sum.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,sum=0;
printf("Enter the number 1:");
scanf("%d",&a);
printf("Enter the number 2:");
scanf("%d",&b);
sum=a+b;
printf("Sum of numbers is: %d",sum);

return 0;
}

Write a C program to enter two numbers and perform all arithmetic operations.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,sum=0;
printf("Enter the number 1:");
scanf("%d",&a);
printf("Enter the number 2:");
scanf("%d",&b);
printf("\nSum of numbers is: %d",a+b);
printf("\nSubtract of numbers is: %d",a-b);
printf("\nProduct of numbers is: %d",a*b);
printf("\nDivision of numbers is: %d",a/b);

return 0;
}

Write a C program to enter length and breadth of a rectangle and find its
perimeter.
#include <stdio.h>
#include <stdlib.h>

int main()
{
float per=0.00;
float l,b;
printf("Enter the length:");
scanf("%f",&l);
printf("Enter the breadth:");
scanf("%f",&b);
per=2*(l+b);
printf("\nPerimeter of an rectangle is: %f",per);

return 0;
}

Write a C program to enter length and breadth of a rectangle and find its area.
#include <stdio.h>
#include <stdlib.h>

int main()
{
float area=0.00;
float l,b;
printf("Enter the length:");
scanf("%f",&l);
printf("Enter the breadth:");
scanf("%f",&b);
area=l*b;
printf("\nArea of an rectangle is: %f",area);

return 0;
}

Write a C program to enter radius of a circle and find its diameter, circumference
and area.
#include <stdio.h>
#include <stdlib.h>

int main()
{
float area=0.00, circum=0.00;
float r;
printf("Enter the radius of a circle:");
scanf("%f",&r);
circum=2*3.14*r;
area=3.14*r*r;
printf("\nDiameter of circle is: %f",2*r);
printf("\nCircumference of circle is: %f",circum);
printf("\nArea of circle is: %f",area);

return 0;
}

Write a C program to enter length in centimeter and convert it into meter and
kilometer.
#include <stdio.h>
#include <stdlib.h>

int main()
{
float dist_cm=0.00;
float dist_m=0.00;
float dist_km=0.00;
printf("Enter the distance in centimeters:");
scanf("%f",&dist_cm);
dist_m=dist_cm/100.00;
dist_km=dist_cm/100000.00;
printf("\nDistance in centimeter is: %f cm",dist_cm);
printf("\nDistance in meter is: %f m",dist_m);
printf("\nDistance in kilometer is: %f km",dist_km);

return 0;
}

Write a C program to enter temperature in Celsius and convert it into Fahrenheit.


#include <stdio.h>
#include <stdlib.h>

int main()
{
float cel=0.00;
float fah=0.00;
printf("Enter the temperature in celsius:");
scanf("%f",&cel);
fah=((cel*9)/5);
printf("The temperature in fahrenheit is: %f fah",fah);

return 0;
}

Write a C program to enter temperature in Fahrenheit and convert to Celsius.


#include <stdio.h>
#include <stdlib.h>

int main()
{
float cel=0.00;
float fah=0.00;
printf("Enter the temperature in fahrenheit:");
scanf("%f",&fah);
cel=((32-fah)*5)/9;
printf("The temperature in celsius is: %f cel",cel);

return 0;
}

Write a C program to convert days into years, weeks and days.


Write a C program to find power of any number x ^ y.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x=2,y=4,z=0;
z=pow(x,y);
printf("%d",z);

return 0;
}

Write a C program to enter any number and calculate its square root.
Write a C program to enter two angles of a triangle and find the third angle.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int ang1,ang2,sum=180,ang3;
printf("Enter the angle 1:\n");
scanf("%d",&ang1);
printf("Enter the angle 2:\n");
scanf("%d",&ang2);
ang3=sum-(ang1+ang2);
printf("Angle 3 is:%d",ang3);

return 0;
}

Write a C program to enter base and height of a triangle and find its area.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int base,height,area;
printf("Enter the base of the triangle:\n");
scanf("%d",&base);
printf("Enter the height of the triangle:\n");
scanf("%d",&height);
area=(base*height)/2;
printf("Area of the triangle is: %d",area);

return 0;
}

Write a C program to calculate area of an equilateral triangle.


#include <stdio.h>
#include <stdlib.h>

int main()
{
float side,area;
printf("Enter the side of an equilateral triangle:\n");
scanf("%f",&side);
area=(1.732*side*side)/4;
printf("Area of the triangle is: %f",area);

return 0;
}

Write a C program to enter marks of five subjects and calculate total, average and
percentage.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int eng, hin, math, sci, sst, total;
float average, per;
printf("Enter the marks in English:\n");
scanf("%d",&eng);
printf("Enter the marks in Hindi:\n");
scanf("%d",&hin);
printf("Enter the marks in Maths:\n");
scanf("%d",&math);
printf("Enter the marks in Science:\n");
scanf("%d",&sci);
printf("Enter the marks in Social Science:\n");
scanf("%d",&sst);
total=eng+hin+math+sci+sst;
average=total/5;
per=(total*100)/125;
printf("Total is: %d",total);
printf("Average is: %f",average);
printf("Percentage is: %f",per);

return 0;
}

Write a C program to enter P, T, R and calculate Simple Interest.


#include <stdio.h>
#include <stdlib.h>

int main()
{
float prin,rate,time,inter,amount=0.00;
printf("Enter the principle:\n");
scanf("%f",&prin);
printf("Enter the time:\n");
scanf("%f",&time);
printf("Enter the rate of interest:\n");
scanf("%f",&rate);
inter=(prin*time*rate)/100;
amount=prin+inter;
printf("Simple interest is %f\n",inter);
printf("Total amount is %f:\n",amount);

return 0;
}

Write a C program to enter P, T, R and calculate Compound Interest.


#include <stdio.h>
#include <stdlib.h>

int main()
{
float prin,rate,time,inter1,inter2,amount=0.00;
printf("Enter the principle:\n");
scanf("%f",&prin);
printf("Enter the time:\n");
scanf("%f",&time);
printf("Enter the rate of interest:\n");
scanf("%f",&rate);
inter1=((rate/100)+1);
inter2=prin*pow(inter1,time);
amount=prin+inter2;
printf(" Interest is %f\n",inter2);
printf("Total amount is %f\n",amount);
return 0;
}

BITWISE EXERCISE

Write a C program to check Least Significant Bit (LSB) of a number is set or not.
Write a C program to check Most Significant Bit (MSB) of a number is set or not.
Write a C program to get nth bit of a number.
Write a C program to set nth bit of a number.
Write a C program to clear nth bit of a number.
Write a C program to toggle nth bit of a number.
Write a C program to get highest set bit of a number.
Write a C program to get lowest set bit of a number.
Write a C program to count trailing zeros in a binary number.
Write a C program to count leading zeros in a binary number.
Write a C program to flip bits of a binary number using bitwise operator.
Write a C program to count total zeros and ones in a binary number.
Write a C program to rotate bits of a given number.
Write a C program to convert decimal to binary number system using bitwise
operator.
Write a C program to swap two numbers using bitwise operator.
Write a C program to check whether a number is even or odd using bitwise operator.

IF AND ELSE EXERCISE

Write a C program to check whether a number is negative, positive or zero.


#include <stdio.h>
#include <stdlib.h>

int main()
{
float prin,rate,time,inter1,inter2,amount=0.00;
printf("Enter the principle:\n");
scanf("%f",&prin);
printf("Enter the time:\n");
scanf("%f",&time);
printf("Enter the rate of interest:\n");
scanf("%f",&rate);
inter1=((rate/100)+1);
inter2=prin*pow(inter1,time);
amount=prin+inter2;
printf(" Interest is %f\n",inter2);
printf("Total amount is %f\n",amount);

return 0;
}

Write a C program to check whether a number is divisible by 5 and 11 or not.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int no;
printf("Enter the number:\n");
scanf("%d",&no);
if(no%5==0)
{
if(no%11==0)
printf("%d is a number divisible by both 5 and 11",no);
else
printf("%d is a number divisible by 5 only",no);

else if(no%11==0)
{
printf("%d is a number divisible by 11 only",no);
}
else
printf("%d is neither divisible by 11 nor 5",no);
return 0;
}

Write a C program to check whether a number is even or odd.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int no;
printf("Enter the number:\n");
scanf("%d",&no);
if(no%2==0)
{
printf("%d is an even number",no);
}
else
printf("%d is an odd number",no);
return 0;
}

Write a C program to check whether a year is leap year or not.


Write a C program to check whether a character is alphabet or not.
Write a C program to input any alphabet and check whether it is vowel or consonant.
Write a C program to input any character and check whether it is alphabet, digit or
special character.
Write a C program to check whether a character is uppercase or lowercase alphabet.
Write a C program to input angles of a triangle and check whether triangle is valid
or not.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int angle1,angle2,angle3,sum=0;
printf("Enter angle 1:\n");
scanf("%d",&angle1);
printf("Enter angle 2:\n");
scanf("%d",&angle2);
printf("Enter angle 3:\n");
scanf("%d",&angle3);
sum=angle1+angle2+angle3;
if(sum==180)
{
printf("These angles form triangle.");
}
else
printf("These angles do not form triangle");
return 0;
}

Write a C program to input all sides of a triangle and check whether triangle is
valid or not.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int s1,s2,s3;
printf("Enter side 1:\n");
scanf("%d",&s1);
printf("Enter side 2:\n");
scanf("%d",&s2);
printf("Enter side 3:\n");
scanf("%d",&s3);
if(s1+s2>s3 && s2+s3>s1 && s3+s1>s2)
{
printf("These sides form triangle.");
}
else
printf("These sides do not form triangle");
return 0;
}
Write a C program to check whether the triangle is equilateral, isosceles or
scalene triangle.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int s1,s2,s3;
printf("Enter side 1:\n");
scanf("%d",&s1);
printf("Enter side 2:\n");
scanf("%d",&s2);
printf("Enter side 3:\n");
scanf("%d",&s3);
if(s1+s2>s3 && s2+s3>s1 &&s3+s1>s2)
{
printf("These sides form triangle.");
}
else
printf("These sides do not form triangle");
return 0;
}
Write a C program to find all roots of a quadratic equation.
Write a C program to input marks of five subjects Physics, Chemistry, Biology,
Mathematics and Computer. Calculate percentage and grade according to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
#include <stdio.h>
#include <stdlib.h>

int main()
{
int phy, chem, math, bio, comp, total;
float per;
printf("Enter the marks in Physics:\n");
scanf("%d",&phy);
printf("Enter the marks in Chemistry:\n");
scanf("%d",&chem);
printf("Enter the marks in Maths:\n");
scanf("%d",&math);
printf("Enter the marks in Biology:\n");
scanf("%d",&bio);
printf("Enter the marks in Social Computer:\n");
scanf("%d",&comp);
total=phy+chem+math+bio+comp;
per=(total*100)/125;
printf("\nTotal is: %d",total);
printf("\nPercentage is: %f",per);
if(per>90)
{
printf("\nPerfect, you secured grade 'A'");
}
else if(per>80 && per<=90)
{
printf("\nVery Good, you secured grade 'B'");
}
else if(per>70 && per<=80)
{
printf("\nGood, you secured grade 'C'");
}
else if(per>60 && per<=70)
{
printf("\nAverage, you secured grade 'D'");
}
else if(per>40 && per<=60)
{
printf("\nBelow average, you secured grade 'E'");
}
else
printf("\nSorry, you are fail, you secured grade 'F'");

return 0;
}

Write a C program to input basic salary of an employee and calculate its Gross
salary according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
#include <stdio.h>
#include <stdlib.h>

int main()
{
float basic_sal=0.00,gross_sal=0.00,hra,da;
printf("\nEnter Basic salary:");
scanf("%f",&basic_sal);
if(basic_sal<10000)
{
hra=0.2*basic_sal;
da=0.8*basic_sal;
gross_sal=basic_sal+hra+da;
printf("\nGross salary is: %f",gross_sal);
}
else if(basic_sal<20000 && basic_sal>10000)
{
hra=0.25*basic_sal;
da=0.9*basic_sal;
gross_sal=basic_sal+hra+da;
printf("\nGross salary is: %f",gross_sal);
}
else
hra=0.3*basic_sal;
da=0.95*basic_sal;
gross_sal=basic_sal+hra+da;
printf("\nGross salary is: %f",gross_sal);

return 0;
}

Write a C program to input electricity unit charges and calculate total electricity
bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill.
#include <stdio.h>
#include <stdlib.h>

int main()
{
float unit=0.00,bill=0.00;
printf("\nEnter Units Consumed:");
scanf("%f",&unit);
if(unit<=50)
{
bill=unit*0.50 + 0.20*unit;
printf("\nTotal Bill is: %f",bill);
}
else if(unit<150 )
{
bill=25+ (unit-50)*0.75 + 0.20*unit;
printf("\nTotal Bill is: %f",bill);
}
else if(unit<250 )
{
bill=100 + (unit-150)*1.20 + 0.20*unit;
printf("\nTotal Bill is: %f",bill);
}
else
bill=220+ (unit-250)*1.50 + 0.20*bill;
printf("\nTotal Bill is: %f",bill);

return 0;
}

LOOP AND ITERATION EXERCISES

Write a C program to print all natural numbers from 1 to n. - using while loop
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=1,n;
printf("Enter the natural number up to which you want to print: \n");
scanf("%d",&n);
while(i!=n+1)
{
printf("\n%d",i);
i=i+1;
}
return 0;
}

Write a C program to print all natural numbers in reverse (from n to 1). - using
while loop
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
printf("Enter the last natural number which you want to print: \n");
scanf("%d",&n);
int i=n;
while(i!=0)
{
printf("\n%d",i);
i=i-1;
}
return 0;
}

Write a C program to print all alphabets from a to z. - using while loop


Write a C program to print all even numbers between 1 to 100. - using while loop
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=2;

while(i!=100)
{
printf("\n%d",i);
i=i+2;
}
return 0;
}

Write a C program to print all odd number between 1 to 100.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=1;

while(i!=101)
{
printf("\n%d",i);
i=i+2;
}
return 0;
}

Write a C program to find sum of all natural numbers between 1 to n.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=1,n,sum=0;
printf("Enter n:");
scanf("%d",&n);

while(i!=n+1)
{
sum=sum+i;
i=i+1;
}
printf("%d",sum);
return 0;
}

Write a C program to find sum of all even numbers between 1 to n.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=0,n,sum=0;
printf("Enter n:");
scanf("%d",&n);

while(i<=n)
{
sum=sum+i;
i=i+2;
}
printf("%d",sum);
return 0;
}

Write a C program to find sum of all odd numbers between 1 to n.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=1,n,sum=0;
printf("Enter n:");
scanf("%d",&n);

while(i<=n)
{
sum=sum+i;
i=i+2;
}
printf("%d",sum);
return 0;
}

Write a C program to print multiplication table of any number.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=1,n;
printf("Enter n:");
scanf("%d",&n);

while(i<=10)
{
printf("\n%d * %d = %d",n,i,n*i);
i++;
}
return 0;
}
Write a C program to count number of digits in a number.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=0,n;
printf("Enter n:");
scanf("%d",&n);

while(n!=0)
{

n=n/10;
i=i+1;
}
printf("%d",i);
return 0;
}

Write a C program to find first and last digit of a number.


#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,n,l;
printf("Enter n:");
scanf("%d",&n);
l=n%10;

while(n>=10)
{
n=n/10;
}
printf(" first digit is = %d",n);
printf("\n last digit is = %d",l);

return 0;
}

Write a C program to find sum of first and last digit of a number.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=0,n,l;
printf("Enter n:");
scanf("%d",&n);
l=n%10;

while(n>=10)
{
n=n/10;
}
printf(" first digit is = %d",n);
printf("\n last digit is = %d",l);
printf("\n Sum of the first and last digit of a number is :%d",l+n);

return 0;
}

Write a C program to swap first and last digits of a number.


Write a C program to calculate sum of digits of a number.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=0,n,l;
printf("Enter n:");
scanf("%d",&n);

while(n>0)
{
i=n%10;
l=l+i;
n=n/10;

}
printf(" Sum of digits = %d",l);

return 0;
}

Write a C program to calculate product of digits of a number.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=1,n,l=1;
printf("Enter n:");
scanf("%d",&n);

while(n>1)
{
i=n%10;
l=l*i;
n=n/10;

}
printf(" Sum of digits = %d",l);

return 0;
}

Write a C program to enter a number and print its reverse.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int rem,n,rev=0;
printf("Enter n:");
scanf("%d",&n);

while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;

}
printf(" Reverse of digits = %d",rev);

return 0;
}

Write a C program to check whether a number is palindrome or not.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int no,rem,rev,original;
printf("Enter the number:\n");
scanf("%d",&no);
original=no;
while(no!=0)
{
rem=no%10;
rev=10*rev+rem;
no=no/10;
}
if(rev==original)
{
printf("The no is a palindrome number");
}
else
printf("The number is not a palindrome number");
return 0;
}

Write a C program to find frequency of each digit in a given integer.


Write a C program to enter a number and print it in words.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int no,rem,rev,rem1;
printf("Enter the number:\n");
scanf("%d",&no);
while(no!=0)
{
rem=no%10;
rev=10*rev+rem;
no=no/10;
}
while(rev!=0)
{
rem1=rev%10;
switch(rem1)
{
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
}

rev=rev/10;
};
return 0;
}

Write a C program to print all ASCII character with their values.


Write a C program to find power of a number using for loop.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int no,pow=1,i,value=1;
printf("Enter the number:\n");
scanf("%d",&no);
printf("Enter the power:\n");
scanf("%d",&pow);
for(i=1;i<=pow;i++)
{
value=value*no;
}
printf("Final value is = %d",value);

return 0;
}

Write a C program to find all factors of a number.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int no,value=1,i;
printf("Enter the number:\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
if(no%i==0)
{
printf("\n%d is a factor of %d",i,no);
}
}

return 0;
}
Write a C program to calculate factorial of a number.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int no,value=1,i;
printf("Enter the number:\n");
scanf("%d",&no);
for(i=no;i>=1;i--)
{
value=value*i;
}
printf("Factorial is = %d",value);

return 0;
}

Write a C program to find HCF (GCD) of two numbers.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int no1,no2,min,i,hcf;
printf("Enter the numbers:\n");
scanf("%d %d",&no1,&no2);
min=(no1<no2)?no1:no2;
for(i=1;i<=min;i++)
{
if( no1%i==0 && no2%i==0 )
hcf=i;
}
printf("The HCF of numbers %d and %d is %d",no1,no2,hcf);
return 0;
}

Write a C program to find LCM of two numbers.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int no1,no2,min,i,hcf,lcm;
printf("Enter the numbers:\n");
scanf("%d %d",&no1,&no2);
min=(no1<no2)?no1:no2;
for(i=1;i<=min;i++)
{
if( no1%i==0 && no2%i==0 )
hcf=i;
}
lcm=(no1*no2)/hcf;
printf("The LCM of numbers %d and %d is %d",no1,no2,lcm);
printf("The HCF of numbers %d and %d is %d",no1,no2,hcf);
return 0;
}

Write a C program to check whether a number is Prime number or not.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int no,i,c=0;
printf("Enter the number:\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
if( no%i==0 )
c++;
}
if(c>2)
{
printf("%d is a composite number",no);
}
else
printf("%d is a prime number",no);
return 0;
}

Write a C program to print all Prime numbers between 1 to n.


Write a C program to find sum of all prime numbers between 1 to n.
Write a C program to find all prime factors of a number.
Write a C program to check whether a number is Armstrong number or not.
Write a C program to print all Armstrong numbers between 1 to n.
Write a C program to check whether a number is Perfect number or not.
Write a C program to print all Perfect numbers between 1 to n.
Write a C program to check whether a number is Strong number or not.
Write a C program to print all Strong numbers between 1 to n.
Write a C program to print Fibonacci series up to n terms.

Write a C program to find one's complement of a binary number.


Write a C program to find two's complement of a binary number.
Write a C program to convert Binary to Octal number system.
Write a C program to convert Binary to Decimal number system.
Write a C program to convert Binary to Hexadecimal number system.
Write a C program to convert Octal to Binary number system.
Write a C program to convert Octal to Decimal number system.
Write a C program to convert Octal to Hexadecimal number system.
Write a C program to convert Decimal to Binary number system.
Write a C program to convert Decimal to Octal number system.
Write a C program to convert Decimal to Hexadecimal number system.
Write a C program to convert Hexadecimal to Binary number system.
Write a C program to convert Hexadecimal to Octal number system.
Write a C program to convert Hexadecimal to Decimal number system.
Write a C program to print Pascal triangle upto n rows.
Star pattern programs - Write a C program to print the given star patterns.
Number pattern programs - Write a C program to print the given number patterns.

FUNCTIONS AND RECURSION EXERCISES

Write a C program to find cube of any number using function.


Write a C program to find diameter, circumference and area of circle using
functions.
Write a C program to find maximum and minimum between two numbers using functions.
Write a C program to check whether a number is even or odd using functions.
Write a C program to check whether a number is prime, Armstrong or perfect number
using functions.

Write a C program to find all prime numbers between given interval using functions.
Write a C program to print all strong numbers between given interval using
functions.
Write a C program to print all Armstrong numbers between given interval using
functions.
Write a C program to print all perfect numbers between given interval using
functions.

Write a C program to find power of any number using recursion.


Write a C program to print all natural numbers between 1 to n using recursion.
Write a C program to print all even or odd numbers in given range using recursion.
Write a C program to find sum of all natural numbers between 1 to n using
recursion.
Write a C program to find sum of all even or odd numbers in given range using
recursion.
Write a C program to find reverse of any number using recursion.
Write a C program to check whether a number is palindrome or not using recursion.
Write a C program to find sum of digits of a given number using recursion.
Write a C program to find factorial of any number using recursion.
Write a C program to generate nth Fibonacci term using recursion.
Write a C program to find GCD (HCF) of two numbers using recursion.
Write a C program to find LCM of two numbers using recursion.
Write a C program to display all array elements using recursion.
Write a C program to find sum of elements of array using recursion.
Write a C program to find maximum and minimum elements in array using recursion.

ARRAY EXERCISES

Write a C program to read and print elements of array.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,arr[5];

printf("Enter the values in an array:");


for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<5;i++)
{
printf("\n %d",arr[i]);
}
return 0;
}

Write a C program to print all negative elements in an array.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,arr[10],b[10],no,j=0;
printf("Enter the values in an array:");
for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
if(arr[i]<0)
{
b[j]=arr[i];
j++;
}
}
no=j;
for(i=0;i<j;i++)
{
printf("\n %d",b[i]);
}
return 0;
}

Write a C program to find sum of all array elements.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,arr[10],sum=0;

printf("Enter the values in an array:");


for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
sum=sum+arr[i];
}
printf("%d",sum);
return 0;
}

Write a C program to find maximum and minimum element in an array.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,arr[10],sum=0,max=0,min=0;

printf("Enter the values in an array:");


for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
if(max<arr[i])
max=arr[i];
}
printf("Maximum element of an array is:%d",max);
for(i=0;i<10;i++)
{
if(min>arr[i])
min=arr[i];
}
printf("\nMinimum element of an array is:%d",min);

return 0;
}

Write a C program to find second largest element in an array.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,arr[10],sum=0,max=0,seclarge=0;

printf("Enter the values in an array:");


for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
if(max<arr[i])
max=arr[i];
}
printf("Maximum element of an array is:%d",max);
for(i=0;i<10;i++)
{
if(arr[i]<max)
seclarge=arr[i];
}
printf("Second Largest element of an array is:%d",seclarge);

return 0;
}

Write a C program to count total number of even and odd elements in an array.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,arr[10],count=0,count1=0;

printf("Enter the values in an array:");


for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
if(arr[i]%2==0)
{
count++;
}
else
count1++;
}
printf("Total number of even numbers are:%d",count);
printf("\nTotal number of odd numbers are:%d",count1);

return 0;
}

Write a C program to count total number of negative elements in an array.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,arr[10],count=0,count1=0;

printf("Enter the values in an array:");


for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
if(arr[i]<0)
{
count++;
}

}
printf("Total number of negative numbers are:%d",count);

return 0;
}

Write a C program to copy all elements from an array to another array.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=0,j=0,arr[10],array[10];

printf("Enter the values in an array:");


for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{

array[j]=arr[i];
j++;
}
for(i=0;i<j;i++)
{
printf("\n%d",array[i]);
}

return 0;
}

Write a C program to insert an element in an array.


Write a C program to delete an element from an array at specified position.
#include<stdio.h>

void main()
{
int a[100],i,n,pos;

printf("\nEnter no of elements\n");
scanf("%d",&n);

printf("Enter the elements\n");


for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

printf("Elements of array are\n");


for(i=0;i<n;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}

printf("Enter the position from which the number has to be deleted\n");


scanf("%d",&pos);
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nOn Deletion, new array we get is\n");
for(i=0;i<n;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
}

Write a C program to count frequency of each element in an array.


Write a C program to print all unique elements in the array.
Write a C program to count total number of duplicate elements in an array.
Write a C program to delete all duplicate elements from an array.
Write a C program to merge two array to third array.
Write a C program to find reverse of an array.
Write a C program to put even and odd elements of array in two separate array.
Write a C program to search an element in an array.
Write a C program to sort array elements in ascending or descending order.
Write a C program to sort even and odd elements of array separately.
Write a C program to left rotate an array.
Write a C program to right rotate an array

MATRICES EXERCISES

Write a C program to add two matrices.


Write a C program to subtract two matrices.
Write a C program to perform Scalar matrix multiplication.
Write a C program to multiply two matrices.
Write a C program to check whether two matrices are equal or not.
Write a C program to find sum of main diagonal elements of a matrix.
Write a C program to find sum of minor diagonal elements of a matrix.
Write a C program to find sum of each row and column of a matrix.
Write a C program to interchange diagonals of a matrix.
Write a C program to find upper triangular matrix.
Write a C program to find lower triangular matrix.
Write a C program to find sum of upper triangular matrix.
Write a C program to find sum of lower triangular matrix.
Write a C program to find transpose of a matrix.
Write a C program to find determinant of a matrix.
Write a C program to check Identity matrix.
Write a C program to check Sparse matrix.
Write a C program to check Symmetric matrix.

STRING EXERCISE

Write a C program to find length of a string.


Write a C program to copy one string to another string.
Write a C program to concatenate two strings.
Write a C program to compare two strings.

Write a C program to convert lowercase string to uppercase.


Write a C program to convert uppercase string to lowercase.
Write a C program to toggle case of each character of a string.

Write a C program to find total number of alphabets, digits or special character in


a string.
Write a C program to count total number of vowels and consonants in a string.
Write a C program to count total number of words in a string.
Write a C program to find reverse of a string.
Write a C program to check whether a string is palindrome or not.
Write a C program to reverse order of words in a given string
Write a C program to find first occurrence of a character in a given string.
Write a C program to find last occurrence of a character in a given string.
Write a C program to search all occurrences of a character in given string.
Write a C program to count occurrences of a character in given string.
Write a C program to find highest frequency character in a string.
Write a C program to find lowest frequency character in a string.
Write a C program to count frequency of each character in a string.
Write a C program to remove first occurrence of a character from string.
Write a C program to remove last occurrence of a character from string.
Write a C program to remove all occurrences of a character from string.
Write a C program to remove all repeated characters from a given string.
Write a C program to replace first occurrence of a character with another in a
string.
Write a C program to replace last occurrence of a character with another in a
string.
Write a C program to replace all occurrences of a character with another in a
string.
Write a C program to find first occurrence of a word in a given string.
Write a C program to find last occurrence of a word in a given string.
Write a C program to search all occurrences of a word in given string.
Write a C program to count occurrences of a word in a given string.
Write a C program to remove first occurrence of a word from string.
Write a C program to remove last occurrence of a word in given string.
Write a C program to remove all occurrence of a word in given string.

Write a C program to trim leading white space characters from given string.
Write a C program to trim trailing white space characters from given string.
Write a C program to trim both leading and trailing white space characters from
given string.
Write a C program to remove all extra blank spaces from given string.

POINTERS EXERCISE

Write a C program to create, initialize and use pointers.


Write a C program to add two numbers using pointers.
Write a C program to swap two numbers using pointers.
Write a C program to input and print array elements using pointer.
Write a C program to copy one array to another using pointers.
Write a C program to swap two arrays using pointers.
Write a C program to reverse an array using pointers.
Write a C program to search an element in array using pointers.
Write a C program to access two dimensional array using pointers.
Write a C program to add two matrix using pointers.
Write a C program to multiply two matrix using pointers.
Write a C program to find length of string using pointers.
Write a C program to copy one string to another using pointers.
Write a C program to concatenate two strings using pointers.
Write a C program to compare two strings using pointers.
Write a C program to find reverse of a string using pointers.
Write a C program to sort array using pointers.
Write a C program to return multiple value from function using pointers.

FILES HANDLING EXERCISES

Write a C program to create a file and write contents, save and close the file.
Write a C program to read file contents and display on console.
Write a C program to read numbers from a file and write even, odd and prime numbers
to separate file.
Write a C program to append content to a file.
Write a C program to compare two files.
Write a C program to copy contents from one file to another file.
Write a C program to merge two file to third file.
Write a C program to count characters, words and lines in a text file.
Write a C program to remove a word from text file.
Write a C program to remove specific line from a text file.
Write a C program to remove empty lines from a text file.
Write a C program to find occurrence of a word in a text file.
Write a C program to count occurrences of a word in a text file.
Write a C program to count occurrences of all words in a text file.
Write a C program to find and replace a word in a text file.
Write a C program to replace specific line in a text file.
Write a C program to print source code of same program.
Write a C program to convert uppercase to lowercase character and vice versa in a
text file.
Write a C program to find properties of a file using stat() function.
Write a C program to check if a file or directory exists.
Write a C program to rename a file using rename() function.
Write a C program to list all files and sub-directories recursively.

patterns
#include <stdio.h>
#include <stdlib.h>

int main()
{
int row,col,n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=1;col<=row;col++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Enter the number of rows:10
*
**
***
****
*****
******
*******
********
*********
**********

#include <stdio.h>
#include <stdlib.h>

int main()
{
int row,col,n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=1;col<=(n+1)-row;col++)
{
printf("*");
}
printf("\n");

}
return 0;
}
Enter the number of rows:10
**********
*********
********
*******
******
*****
****
***
**
*

#include <stdio.h>
#include <stdlib.h>

int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=0;row<=n;row++)
{
for(col=0;col<=n;col++)
{
if(col<=((n-row)-1))
printf(" ");
else
printf(" *");
}
printf("\n");
}
return 0;
}

Enter the number of rows:10


*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *

#include <stdio.h>
#include <stdlib.h>

int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=0;row<=n;row++)
{
for(col=0;col<=n;col++)
{
if(col<=((n-row)-1))
printf(" ");
else
printf("*");
}
printf("\n");
}
return 0;
}

Enter the number of rows:10


*
**
***
****
*****
******
*******
********
*********
**********
***********

#include <stdio.h>
#include <stdlib.h>

int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=0;row<=n;row++)
{
for(col=0;col<=n;col++)
{
if(col<=((n-row)-1))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=1;col<=row;col++)
{
printf("%d ",col);
}
printf("\n");
}
return 0;
}
Enter the number of rows:10
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

#include <stdio.h>
#include <stdlib.h>

int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=1;col<=(n+1)-row;col++)
{
printf("%d ",col);
}
printf("\n");
}
return 0;
}

Enter the number of rows:10


1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

#include <stdio.h>
int main()
{
int height,space,i,j,k;
printf("Enter the height of the triangle: ");
scanf("%d",&height);
space=height;
for( i=1;i<=height;i++)
{
for( j=1;j<=space-1;j++)
{
printf(" ");
}
for( k=1;k<=2*i-1;k++)
{
printf("*");
}
space--;

printf("\n");
}
return 0;
}

Enter the height of the triangle: 10


*
***
*****
*******
*********
***********
*************
***************
*****************
*******************

#include <stdio.h>
int main()
{
int height,space,i,j,k,m=0;
printf("Enter the height of the triangle: ");
scanf("%d",&height);
space=height;
for( i=1;i<=height;i++)
{
for( j=1;j<i;j++)
{
printf(" ");
}
for( k=1;k<=(2*height-1)-2*m;k++)
{
printf("*");
}
m++;

printf("\n");
}
return 0;
}

Enter the height of the triangle: 10


*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
Enter the number of rows:10
**********
*********
********
*******
******
*****
****
***
**
*

You might also like