0% found this document useful (0 votes)
13 views75 pages

C Programs

The document contains a series of C programming exercises designed for practice, covering various concepts such as displaying messages, mathematical calculations, loops, and patterns. Each exercise includes a brief description followed by the corresponding C code implementation. Topics range from basic input/output operations to more complex algorithms involving iterations and selections.

Uploaded by

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

C Programs

The document contains a series of C programming exercises designed for practice, covering various concepts such as displaying messages, mathematical calculations, loops, and patterns. Each exercise includes a brief description followed by the corresponding C code implementation. Topics range from basic input/output operations to more complex algorithms involving iterations and selections.

Uploaded by

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

C Programming Questions for Practice

Expressing Algorithm: Sequence


--> Write a C program to display the statement “Hello Brother! How are you? “

//This is a simple program to display a statement


#include<conio.h>
#include<stdio.h>
void main ()// main method
{
printf("Hello Brother ! How are you ?");
getch();
}
--> Write a C program to accept a number and display its cube .

//This is a simple program to calculate the cube of a


number
#include<stdio.h>
#include<conio.h>
void main ()
{
int x, y;
printf("Enter a number:");
scanf("%d",&x);
y=x*x*x;// multiplication of x three times
printf("The cube of %d is %d",x,y);
getch();
} 3
--> Write a C program to calculate the simple interest and number of years as input
from user.

#include<stdio.h>
#include<conio.h>
void main ()
{
float rateOfInterest, years, principal, simpleInterest;
// clear the output screen
printf("Enter the principal amount, rate of interest and
no. of years:");
scanf("%f%f%f", &principal, &rateOfInterest, &years);
simpleInterest = principal * rateOfInterest*years / 100; //
formula
printf("The simple interest is %f", simpleInterest) ;
getch();
}
--> Write a C program to accept basic salary from user and calculate gross salary that
includes basic salary , 50% DA , 40%HRA

#include<stdio.h>
#include<conio.h>
void main ()
{
float basic,hra,da,gross;

printf("Enter the basic salary:");


scanf("%f", &basic);
hra=40*basic/100;
da = 50*basic/100;
gross=basic+da+hra; 4
printf("The total salary is %f",gross);
getch();
}
--> Write a C program to accept radius of circle and calculate its area and perimeter.

#include<stdio.h>
#include<conio.h>
void main ()
{
float radius, area, perimeter;

printf("Enter the radius of the circle:");


scanf("%f",&radius);
area= 3.14*radius*radius;// formula
perimeter= 2*3.14*radius; // formula
printf("The area of circle is= %f and its perimeter is =
%f", area, perimeter);
getch();
}
--> Write a C program to accept three digit number and print them separately

#include<stdio.h>
#include<conio.h>
void main ()
{
int d1, d2, d3, // store the digits separately
int number; // actual number

printf("Enter a three digit number:");


scanf("%d",&number);
d3=number%10;
number=number/10;
d2= number%10;
number =number / 10; 5
d1=number%10;
printf("The digits are :%d\n%d\n%d",d1,d2,d3);
getch();
}
--> Write a C program to accept three numbers and find greatest number

#include<stdio.h>
#include<conio.h>
void main ()
{
int n1, n2, n3, greater;

printf("Enter three numbers:");


scanf("%d %d %d", &n1,&n2,&n3);
if( n1>n2)
{
if(n1>n3)
printf("The largest number is :%d",n1);
else if
printf("The largest number is :%d",n3);
else if
printf("The largest number is :%d",n2);
}
getch();
}
--> Write a C program to convert Fahrenheit to Celsius

#include<stdio.h>
#include<conio.h>
void main ()
{
float f, c;

printf("Enter the temperature in fahrenheit:");


scanf("%f",&f);
c=(f-32)/1.8; 6
printf("The temperature in degree celsius is equal:%f",c);
getch();
}
--> Write a C program to swap two integers

#include<stdio.h>
#include<conio.h>
void main ()
{
int a, b, temp;

printf("Enter two numbers:");


scanf("%d %d",&a,&b);
temp=a; // store first value in temp
a=b;
b=temp;
printf("After swapping the values are a=%d and b=%d",a,b);
getch();
}
--> Write a C program to accept two digit number and display its reverse.

#include<stdio.h>
#include<conio.h>
void main ()
{
int a, digit1, digit2;

printf("Enter a number:");
scanf("%d",&a);
digit1=a%10;
a=a/10;
digit2=a%10;
printf("Reversed no is %d%d",digit1,digit2);
getch();
}
--> Write a C program to check whether number is even or odd.

#include<stdio.h>
#include<conio.h>
void main ()
{
int a, rem;

printf("Enter a number:");
scanf("%d",&a);
rem=a%2;
if(rem==0)
printf("Even number");
else
printf("Odd number");
getch();
}
--> Write a C program to accept a float number and display the integer part using type
casting technique.

#include<stdio.h>
#include<conio.h>
void main ()
{
int a;
float b;

printf("Enter a float number:");


scanf("%f",&b);
a=(int)b;// type casting
printf("The integer part of the number is:%d",a);
getch();
}
--> Write a C program to calculate square roots of quadratic equation
8
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int a,b,c;
float x1,x2;

printf("Enter the coefficients of the quadratic


equation:");
scanf("%d %d %d",&a,&b,&c);
x1=(-b+pow((b*b-4*a*c),0.5))/(2*a);// root 1
x2=(-b-pow((b*b-4*a*c),0.5))/(2*a); // root 2
printf("The roots of quadratic equation are:%f and
%f",x1,x2);
getch();
} 9
Expressing Algorithm: Iteration
--> Write a C program to display the word “ structured programming approach “ five
times using the loop.

#include<stdio.h>
#include<conio.h>
void main ()
{
int i;

for(i=1;i<=5;i++)
{
printf("structured programming approach \n");
}
getch();
}
--> Write a C program display first “n” natural numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;

printf("Enter a number: ");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\n",i);
}
getch();
} 10
--> Write a C program to find factorial of given number using ITERATION.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact;

printf("Enter a number: ");


scanf("%d",&n);
for(i=1,fact=1;i<=n;i++)
{
fact=fact * i;
}
printf("Factorial of this number is:%d\n",fact);
getch();
}
--> Write a C program to display first n odd numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;

printf("Enter a number: ");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("%d\n",2*i+1);
}
getch();
} 11
--> Write a C program to display odd numbers up to n.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;

printf("Enter a number: ");


scanf("%d",&n);
for(i=1;i<=n;i+=2)
{
printf("%d\n",i);
}
getch();
}
--> Write a C program to display sum of first n natural numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;

printf("Enter a number: ");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("Sum= %d\n",sum);
getch();
} 12
--> Write a C program to calculate sum of squares of first n natural numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;

printf("Enter a number: ");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i*2;
}
printf("Sum= %d\n",sum);
getch();
}
--> Write a C program to display first n elements of Fibonacci series .

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,i,n;

printf("Enter a number: ");


scanf("%d",&n);
printf("Fibonacci Series\n0\n1\n");
for(i=1;i<=n-2;i++)
{
c=a+b;
printf("%d\n",c);
a=b;
b=c;
}
getch();
} 13
--> Write a C program to calculate the value of following series

1+1/2+1/3+1/4+1/5+.............. +1/n
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
float sum=0.0;

printf("Enter a number: ");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+1.0/i;
}
printf("The value of the series is:%f",sum);
getch();
}
--> Write a C program to calculate the value of following series

1 + 1/2! + 1/3! + 1/4! ......... + 1/n!


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1;
float sum=0.0;

printf("Enter a number: ");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
sum=sum+1.0/fact;
}
printf("The value of the series is:%f",sum);
getch();} 14
--> Write a C program to display the following

1
11
111
1111
11111
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;

for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("1");
}
printf("\n");
}
getch();
}
--> Write a C program to display the following

*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;

for(i=1;i<=5;i++)
{ 15
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
--> Write a C program to display n such lines

@
@@
@@@
@@@@
.
.
N such lines
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;

printf("Enter the number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("@");
}
printf("\n");
}
getch();
}
--> Write a C program to display following pattern
16
@
@@
@@@
@@@@
.
.
.
N such lines
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;

printf("Enter the number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("@");
}
printf("\n");
}
getch();
}
--> Write a C program to display diamond as shown ( display spaces between two stars)
17
*
**
***
****
*****
****
***
**
*
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;

printf("Enter the number of * in the centre line:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
for(i=n-1;i>=1;i--)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++) 18
{
printf("* ");
}
printf("\n");
}
getch();
}
--> Write a C program to display pattern given below

1
12
123
1234
12345
123456
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;

printf("Enter the number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
--> Write a C program to display following pattern asking the user number of lines.
19
1
121
12321
1234321
123454321
12345654321
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;

printf("Enter the number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
} 20
--> Write a C program to display following pattern asking the user for the number of
lines

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;

printf("Enter the number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%c",(char)(j+64));
}
for(j=i-1;j>=1;j--)
{
printf("%c",(char)(j+64));
}
printf("\n");
}
getch();
}
--> Write a C program to display following pattern

A 21
BC
DEF
GHIJ
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,k;

printf("Enter the number of lines:");


scanf("%d",&n);
for(i=1,k=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%c ",64+k++);
}
printf("\n");
}
getch();
}
--> Write a C program to count the number of digits in a user entered number

#include<stdio.h>
#include<conio.h>
void main()
{
int n,count=0;

printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
{
n=n/10;
count++;
}
printf("Number of digits=%d",count); 22
getch();
}
--> Write a C program to reverse the digits of a user entered number and store in
another variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,reverse=0,digit;

printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
{
digit=n%10;
n=n/10;
reverse=reverse*10+digit;
}
printf("The reverse number is:%d",reverse);
getch();
}
--> Write a C program to convert decimal number to binary format

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;

printf("Enter a number");
scanf("%d",&n);
printf("Binary form is:");
for(i=15;i>=0;i--)
{
printf("%d",n/(int)pow(2,i));
n=n%(int)(pow(2,i));
} 23
getch();
}
--> Write a C program to display following pattern

1
21A
321AB
4321ABC
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;

printf("Enter the number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=i;j>=1;j--)
{
printf("%d",j);
}
for(j=1;j<=i-1;j++)
{
printf("%c",(j+'A'-1));
}
printf("\n");
}
getch();
}
Expressing Algorithm: Selection 24
--> Write a C program to check whether the entered number is divisible by 10 or not .

#include<stdio.h>
#include<conio.h>
void main()
{
int n;

printf("Enter a number:");
scanf("%d",&n);
if(n%10==0)
{
printf("The number is divisible by 10");
}
else
{
printf("The number is not divisible by 10");
}
getch();
}
--> Write a C program to display first 10 numbers divisible by 5 and 8.

#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,n=1;

while(i<10)
{
if(n%5==0 && n%8==0)
{
printf("%d\n",n);
i++;
}
n++; 25
}
getch();
}
--> Write a C program to check whether entered number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int i=2,n;

printf("Enter a number:");
scanf("%d",&n);
while(n%i!=0)
{
i++;
}
if(n==i)
{
printf("Prime Number");
}
else
{
printf("Not a prime number");
}
getch();
}
--> Write a C program to display first n prime numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,x=1;

printf("Enter a number:");
scanf("%d",&n); 26
printf("The following are %d prime numbers\n",n);
while (n!=0)
{
x++;
i=2;
while(x%i!=0)
{
i++;
}
if(x==i)
{
printf("%d\n",x);
n--;
}
}
getch();
}
--> Write a C program to check whether number is Armstrong or not .

#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,digit,x,copy;

printf("Enter a number:");
scanf("%d",&x);
copy=x;
while(x!=0)
{
digit=x%10;
sum=sum+digit*digit*digit;
x=x/10;
}
if(sum==copy)
{
printf("Armstrong Number"); 27
}
else
{
printf("Not an Armstrong Number");
}
getch();
}
--> Write a C program to display first ‘n’ Armstrong numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int sum,digit,x=99,copy,n;

printf("Enter a number:");
scanf("%d",&n);
printf("The list of %d Armstrong number is given
below\n",n);
while(n!=0)
{
x++;
copy=x;
sum=0;
while(x!=0)
{
digit=x%10;
sum=sum+digit*digit*digit;
x=x/10;
}
if(sum==copy)
{
printf("%d\n",copy);
n--;
}
x=copy; 28
}
getch();
}
--> Write a C program to check whether entered year is leap year or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int year;

printf("Enter a year:");
scanf("%d",&year);
if(year%4==0 && year%100!=0 || year%100==0 && year%400==0)
printf("Leap Year");
else
printf("Not a leap year");
getch();
}
--> Write a C program to display factors of user entered number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;

printf("Enter a number:");
scanf("%d",&n);
printf("Factors are:\n");
i=2;
while(i<n)
{
if(n%i==0)
printf("%d\n",i);
i++; 29
}
getch();
}
--> Write a C program to display class as per the students marks. The marks
should be entered by students (use switch case)
Marks Class
70-100 Distinction
60-69 First class
50-59 Second class
40-49 Pass Class
0-39 Fail
#include<stdio.h>
#include<conio.h>
void main()
{
int marks;

printf("Enter the marks out of 100:");


scanf("%d",&marks);
if(marks==100)
printf("Distinction");
else
{
switch(marks/10)
{
case 0:
case 1:
case 2:
case 3: printf("Fail");
break;
case 4:printf("Pass Class");
break;
case 5:printf("Second Class");
break;
case 6:printf("First Class"); 30
break;
case 7:
case 8:
case 9:printf("Distinction");
break;
default:printf("Invalid marks");
}
}
getch();
}
--> Write a C program to display user entered single digit number in words.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;

printf("Enter a single digit number:");


scanf("%d",&n);
switch(n)
{
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"); 31
break;
case 8:printf("Eight");
break;
case 9:printf("Nine");
break;
}
getch();
}
--> Write a C menu driven program to perform add/subtract/multiply/divide/
modulus ( options must be entered by user)

#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,result;
char choice;

printf("Enter the numbers:");


scanf("%d %d",&no1,&no2);
printf("+.Add\n-
.Subtract\n*.Multiply\n/.Divide\n%.Modulus\nEnter your
choice:");
choice=getche();
printf("\n");
switch(choice)
{
case '+':result=no1+no2;
printf("Sum= %d",result);
break;
case '-':result=no1-no2;
printf("Difference= %d",result);
break;
case '*':result=no1*no2;
printf("Product= %d",result);
break; 32
case '/':result=no1/no2;
printf("Quotient= %d",result);
break;
case '%':result=no1%no2;
printf("Remainder= %d",result);
break;
default:printf("Invalid choice");
}
getch();
}
--> Write a C program to display the month name by accepting the month
number from user.

#include<stdio.h>
#include<conio.h>
void main()
{
int month;

printf("Enter a month number:");


scanf("%d",&month);
switch(month)
{
case 1:printf("January");
break;
case 2:printf("February");
break;
case 3:printf("March");
break;
case 4:printf("April");
break;
case 5:printf("May");
break;
case 6:printf("June");
break;
case 7:printf("July");
break; 33
case 8:printf("August");
break;
case 9:printf("September");
break;
case 10:printf("October");
break;
case 11:printf("November");
break;
case 12: printf("December");
break;
default:printf("Invalid month number");
}
getch();
}
--> Write a C program to make use of goto statement

#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0;

again:
printf("Enter a number:");
scanf("%d",&n);
total=total+n;
if(total<100)
goto again;
printf("Total=%d",total);
getch();
}
--> Write a C program to make use of break statement
34
#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0,i;

for(i=1;i<=10;i++)
{
printf("Enter a number:");
scanf("%d",&n);
if(n>99)
break;
total=total+n;
}
printf("Total=%d",total);
getch();
}
--> Write a C program to make use of continue statement

#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0,i;

for(i=1;i<=5;i++)
{
printf("Enter a number:");
scanf("%d",&n);
if(n>99)
{
printf("Number is greater than 99\n");
i--;
continue;
}
total=total+n; 35
}
printf("Total=%d",total);
getch();
}
--> Write a C program to find GCD of given numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,gcd;

printf("Enter two numbers:");


scanf("%d %d",&n1,&n2);
if (n1<n2)
gcd=n1;
else gcd=n2;
while(n1%gcd!=0 || n2%gcd!=0)
{
gcd--;
}
if(gcd==1)
printf("GCD doesnt exists");
else
printf("GCD=%d",gcd);
getch();
}
--> Write a C program to find LCM of given number

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,lcm;

printf("Enter two numbers:"); 36


scanf("%d %d",&n1,&n2);
if (n1>n2)
lcm=n1;
else lcm=n2;
while(lcm%n1!=0 || lcm%n2!=0)
{
lcm++;
}
printf("LCM= %d",lcm);
getch();
}
Decomposition of Solutions: 37
--> Write a C program to add two numbers using function.

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,sum;
int add (int a, int b);

printf("Enter two numbers:");


scanf("%d %d",&n1,&n2);
sum=add(n1,n2);
printf("Sum=%d",sum);
getch();
}
int add (int a, int b)
{
int c;
c=a+b;
return c;
}
--> Write a C program to find average of two numbers using function.

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
void avg (int a, int b, int c);

printf("Enter three numbers:");


scanf("%d %d %d",&n1,&n2,&n3);
avg(n1,n2,n3);
getch();
}
void avg (int a, int b, int c)
{
float average; 38
average=(a+b+c)/3.0;
printf("Average=%f",average);
}
--> Write a C program to find factorial of a given number using function.

#include<stdio.h>
#include<conio.h>
void main()
{
int no,factorial;
int fact (int no);

printf("Enter a number:");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial=%d",factorial);
getch();
}
int fact (int no)
{
int i,ans;
for(i=1,ans=1;i<=no;i++)
{
ans=ans*i;
}
return ans;
}
--> Write a C program to find the value of nCr , using a function.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,ncr;
int fact (int no);

printf("Enter the values of n and r:"); 39


scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("nCr=%d",ncr);
getch();
}
int fact (int no)
{
int i,ans;
for(i=1,ans=1;i<=no;i++)
{
ans=ans*i;
}
return ans;
}
--> Write a C program to find the value of nPr using a function.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,npr;
int fact (int no);

printf("Enter the values of n and r:");


scanf("%d %d",&n,&r);
npr=fact(n)/fact(n-r);
printf("nPr=%d",npr);
getch();
}
int fact (int no)
{
int i,ans;
for(i=1,ans=1;i<=no;i++)
{
ans=ans*i;
}
return ans; 40
}
--> Write a C program to find GCD/ LCM of given number.

#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,gcd;
int GCD (int no1, int no2);

printf("Enter two numbers:");


scanf("%d %d",&no1,&no2);
gcd=GCD(no1,no2);
if(gcd==1)
printf("GCD doesnt exist");
else
printf("GCD=%d",gcd);
getch();
}
int GCD (int no1, int no2)
{
int gcd;
if(no1<no2)
gcd=no1;
else
gcd=no2;
while(no1%gcd!=0 || no2%gcd!=0)
{
gcd--;
}
return gcd;
}
--> Write a C-recursive program to find factorial of a given number

#include<stdio.h>
#include<conio.h> 41
void main()
{
int no,factorial;
int fact (int no);

printf("Enter a number:");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial=%d",factorial);
getch();
}
int fact (int no)
{
if(no==1)
return 1;
else
return (no * fact (no-1));
}
--> Write a C program to find n Fibonacci elements using recursion.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
int fibo (int ,int ,int);

printf("Enter the number of elements:");


scanf("%d",&n);
printf("0\n");
for(i=1;i<=n-1;i++)
{
printf("%d\n",fibo(0,1,i));
}
getch();
}
int fibo (int a, int b, int i) 42
{
if(i==1)
return b;
else
return (fibo(b,a+b,--i));
}
--> Write a C program to find power of X using recursion.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,x,y;
int exponential (int x, int n);

printf("Enter the values of x and n:");


scanf("%d %d",&x,&n);
y=exponential(x,n);
printf("The value of x raise to n is %d",y);
getch();
}
int exponential (int x, int n)
{
if(n==1)
return x;
else
return (x*exponential(x,n-1));
}
--> Write a C program to compute sum of first n natural number.

#include<stdio.h>
#include<conio.h> 43
void main()
{
int no,sum;
int recursion (int no);

printf("Enter a number:");
scanf("%d",&no);
sum=recursion(no);
printf("Sum of numbers from 1 to n is %d",sum);
getch();
}
int recursion (int no)
{
if(no==1)
return 1;
else
return (no + recursion (no-1));
}
--> Write a C program to check whether the number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
void prime(int n);

printf("Enter a number:");
scanf("%d",&n);
prime(n);
getch();
}
void prime(int n)
{
int i=2;
while(n%i!=0)
{ 44
i++;
}
if(n==i)
{
printf("Prime Number");
}
else
{
printf("Not a prime number");
}
}
Scalar and additional C Data types :
ARRAYS : 45
--> Write a C program to accept n integers from user and print them one in
each line.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100];

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
printf("The numbers entered are\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}
--> Write a C program to accept n integers and find average of all these
numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100],sum=0;
float avg;

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]); 46
}
for(i=0;i<=n-1;i++)
{
sum=sum+a[i];
}
avg=sum;
avg=avg/n;
printf("The average of the numbers entered is %f",avg);
getch();
}
--> Write a C program to accept n integers and display the count of even and
odd integers.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100],even=0;

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++)
{
if (a[i]%2==0)
even++;
}
printf("The count of even numbers is %d and that of odd
numbers is %d",even,(n-even));
getch();
} 47
--> Write a C program to evaluate following series :

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100],sum1=0,sum2=0,sum;

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++)
{
sum1 = sum1 + a[i] * a[i];
sum2 = sum2 +a[i];
}
sum = sum1 - sum2 * sum2;
printf("The value of the series is %d",sum);
getch();
}
--> Write a C program to find largest numbers from n numbers.

#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,a[100],large;

printf("Enter the number of elements:");


scanf("%d",&n); 48
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
large=a[0];
for(i=1;i<=n-1;i++)
{
if(large<a[i])
large=a[i];
}
printf("The largest number is %d",large);
getch();
}
--> Write a C program to find smallest numbers from n numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100],small;
int smallest (int a[], int n);

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
small=smallest(a,n);
printf("The smallest number is %d",small);
getch();
}
int smallest (int a[], int n)
{
int i,small; 49
small=a[0];
for(i=1;i<=n-1;i++)
{
if(small>a[i])
small=a[i];
}
return small;
}
--> Write a C program to display reverse of an array using a function

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100];
void reverse (int a[], int n);

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
reverse(a,n);
getch();
}
void reverse (int a[], int n)
{
int i,rev[100];
for(i=0;i<=n-1;i++)
{
rev[n-i-1]=a[i];
}
printf("The reverse of this array is:\n");
for(i=0;i<=n-1;i++)
{ 50
printf("%d\n",rev[i]);
}
}
--> Write a C program to sort the numbers using ascending order using an
array.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100],x,index;
void ascend (int a[], int n);

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
ascend(a,n);
getch();
}
void ascend (int a[], int n)
{
int i,j,temp;
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
} 51
printf("After sorting\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}
--> Write a C program to sort the numbers using descending order using an
array.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100];
void descend (int a[], int n);

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
descend(a,n);
getch();
}
void descend (int a[], int n)
{
int i,j,temp;
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp; 52
}
}
}
printf("After sorting\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}
--> Write a C program to accept m *n matrix and display it as a natural form.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,a[10][10];

printf("Enter the number of rows and columns:");


scanf("%d %d",&m,&n);
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("The entered matrix is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
} 53
getch();
}
--> Write a C program to calculate and display the average of all the elements
in matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,a[10][10],sum=0;
float avg;

printf("Enter the number of rows and columns:");


scanf("%d %d",&m,&n);
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
sum=sum+a[i][j];
}
}
avg=1.0 * sum/(m*n);
printf("The average is equal to %f",avg);
getch();
}
--> Write a C program to find largest number in matrix.
54
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,a[10][10],large;

printf("Enter the number of rows and columns:");


scanf("%d %d",&m,&n);
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
large=a[0][0];
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(large<a[i][j])
large=a[i][j];
}
}
printf("The largest element in the matrix is
%d",large);
getch();
}
--> Write a C program to sum of diagonal elements of a square matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,a[10][10],sum=0; 55
printf("Enter the number of rows / columns:");
scanf("%d",&m);
n=m;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i==j)
sum+=a[i][j];
}
}
printf("The sum of diagonal elements is %d",sum);
getch();
}
--> Write a C program to add two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,a[10][10],b[10][10],c[10][10];

printf("Enter the number of rows and columns:");


scanf("%d %d",&m,&n);
printf("Enter the elements of Matrix 1\n");
for(i=0;i<=m-1;i++)
{ 56
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of Matrix 2\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("The sum of two matrices is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
--> Write a C program to find transpose of two matrices of m*n.

#include<stdio.h>
#include<conio.h> 57
void main()
{
int m,n,i,j,a[10][10];
void transpose (int a[10][10],int m, int n);

printf("Enter the number of rows and columns:");


scanf("%d %d",&m,&n);
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("The original Matrix is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d \t",a[i][j]);
}
printf("\n");
}
transpose(a,m,n);
getch();
}
void transpose (int a[10][10], int m, int n)
{
int b[10][10],i,j;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
b[j][i]=a[i][j];
} 58
}
printf("The transpose of this matrix is:\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
}
--> Write a C program to find transpose of square matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,a[10][10];
void transpose (int a[10][10],int m, int n);

printf("Enter the number of rows / columns:");


scanf("%d",&m);
n=m;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("The original Matrix is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]); 59
}
printf("\n");
}
transpose(a,m,n);
getch();
}
void transpose (int a[10][10], int m, int n)
{
int i,j,temp;
for(i=0;i<=m-1;i++)
{
for(j=i;j<=n-1;j++)
{
temp=a[j][i];
a[j][i]=a[i][j];
a[i][j]=temp;
}
}
printf("The transpose of this matrix is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
--> Write a C program to multiply two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{ 60
int m,n,p,i,j,a[10][10],b[10][10];
void MatMul (int a[10][10],int b[10][10], int m, int n,
int p);

printf("Enter the number of rows and columns of matrix


1:");
scanf("%d %d",&m,&n);
printf("Enter the values of matrix 1\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("The number of rows for matrix 2 will be
%d\n",n);
printf("Enter the columns of matrix 2:");
scanf("%d",&p);
printf("Enter the elements of matrix 2:\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=p-1;j++)
{
printf("Enter a value:");
scanf("%d",&b[i][j]);
}
}
MatMul(a,b,m,n,p);
getch();
}
void MatMul (int a[10][10], int b[10][10], int m, int
n, int p)
{
int i,j,k,c[10][10];
for(i=0;i<=m-1;i++) 61
{
for(j=0;j<=p-1;j++)
{
c[i][j]=0;
for(k=0;k<=n-1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("The resultant matrix is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=p-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
--> Write a C program to shift the elements to one position of 1-D array. ( right
shifting)

#include<conio.h>
#include<stdio.h>
void main()
{
int n,i,a[100],temp;

printf("Enter the number of elements:");


scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value");
scanf("%d",&a[i]); 62
}
temp=a[n-1];
for(i=n-1;i>0;i--)
{
a[i]=a[i-1];
}
a[0]=temp;
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}
--> Write a C program to accept month number and display month name (use
2-D char array )

#include<stdio.h>
#include<conio.h>
void main()
{
char
a[12][10]={"January","February","March","April","May",
"June","July","August","September","October","November"
, "December"};
int m;

printf("Enter the month number:");


scanf("%d",&m);
printf("The month name is %s",a[m-1]);
getch();
}
--> Write a C program to find sum of column elements.

#include<stdio.h> 63
#include<conio.h>
void main()
{
int m,n,i,j,a[10][10],b[10];

printf("Enter the number of rows and columns of


matrix:");
scanf("%d %d",&m,&n);
printf("Enter the values of matrix\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=n-1;i++)
{
b[i]=0;
for(j=0;j<=m-1;j++)
{
b[i]=b[i]+a[j][i];
}
}
printf("The entered matrix is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The sum of the columns are:\n");
for(i=0;i<=n-1;i++)
{ 64
printf("%d\t",b[i]);
}
getch();
}
STRINGS :
--> Write a C program to accept and display a string also display its length.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
int l;
char a[100];

printf("Enter a string\n");
gets(a);
l=strlen(a);
printf("The length of the entered string is: %d",l);
getch();
}
--> Write a C program to accept a string, copy it into another string and display
the new string.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100];

printf("Enter a string\n");
gets(a);
strcpy(b,a);
printf("The new string is %s",b);
getch(); 65
}
--> Write a C program to accept two strings , compare them and display if they
are equal or not . If they are not equal, display the one which is greater.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100];

printf("Enter two strings:\n");


gets(a);
gets(b);
if(strcmp(a,b)==0)
printf("The strings are equal ");
else if(strcmp(a,b)>0)
printf("%s string is greater",a);
else
printf("%s string is greater",b);
getch();
}
--> Write a C program to accept two strings , join them and display the result.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100];

printf("Enter two strings:\n");


gets(a);
gets(b);
strcat(a,b);
printf("The concatenated string is %s",a);
getch(); 66
}
--> Write a C program to accept the string and find the number of vowels in it.

#include<conio.h>
#include<stdio.h>
void main()
{
char a[100];
int i,len=0,count=0;

printf("Enter a string:\n");
gets(a);
while(a[len]!=0)
{
len++;
}
for(i=0;i<=len-1;i++)
{
if(a[i]=='a' ||a[i]=='e' || a[i]=='i' || a[i]=='o' ||
a[i]=='u' || a[i]=='A' || a[i]=='E' || a[i]=='I' ||
a[i]=='O' || a[i]=='U')
count++;
}
printf("The total number of vowels are: %d",count);
getch();
}
--> Write a C program to reverse the user-entered string. ( do not use string
header file )

#include<stdio.h>
#include<conio.h>
void main()
{
int n=0; 67
char a[100];
void reverse (char a[100], int n);

printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{
n++;
}
reverse(a,n);
getch();
}
void reverse (char a[100], int n)
{
int i;
char temp;
for(i=0;i<=(n-1)/2;i++)
{
temp=a[n-i-1];
a[n-i-1]=a[i];
a[i]=temp;
}
printf("The reverse of this string is: %s",a);
}
--> Write a C program to check whether string is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n=0,i;
char a[100],rev[100];

printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{ 68
n++;
}
for(i=0;i<=(n-1);i++)
{
rev[n-i-1]=a[i];
}
for(i=0;i<=n-1;i++)
{
if(a[i]!=rev[i])
break;
}
if(i==n)
printf("The string is palindrome.");
else
printf("The string is not palindrome.");
getch();
}
--> Write a C program to reverse a sentence but do not reverse words.

#include<conio.h>
#include<stdio.h>
void main()
{
int n=0,i,j,k;
char a[100],rev[100];

printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0,j=0;i<=n;i++)
{
if(a[i]==' '|| a[i]=='\0')
{
for(k=0;j<=i;k++,j++) 69
{
rev[n-i+k]=a[j];
}
}
}
rev[k-1]=' ';
rev[n]='\0';
printf("The reversed sentence is: %s",rev);
getch();
}
--> Write a C program to count number of blank spaces, digits, vowels and
consonents in the string.

#include<conio.h>
#include<stdio.h>
void main()
{
char a[100];
int i,len=0,vowels=0,spaces=0,digits=0,consonants=0;

printf("Enter a string:\n");
gets(a);
while(a[len]!=0)
{
len++;
}
for(i=0;i<=len-1;i++)
{
if(a[i]=='a' ||a[i]=='e' || a[i]=='i' || a[i]=='o' ||
a[i]=='u' || a[i]=='A' || a[i]=='E' || a[i]=='I' ||
a[i]=='O' || a[i]=='U')
vowels++;
else
{
if((a[i]>='a' && a[i]<='z') || (a[i]>='A' &&
a[i]<='Z'))
consonants++; 70
else
{
if(a[i]>='0' &&a[i]<='9')
digits++;
else
{
if(a[i]==' ')
spaces++;
}
}
}
}
printf("The total number of vowels are: %d\nThe total
number of spaces are:%d\nThe total number of digits
are: %d\nThe total number of consonants are: %d",
vowels,spaces,digits,consonants);
getch();
}
SCALAR DATA TYPES :
--> Write a C program to initialize an automatic variable and increment it in the
function. Call this function thrice and print the value of these variable every time
after incrementing.

#include<stdio.h>
#include<conio.h>
void incr()
{
int i=0;
i++;
printf("%d\n",i);
}
int main()
{
incr();
incr();
incr();
getch(); 71
}
--> Write a C program to demonstrate the access of global variable.

#include<stdio.h>
#include<conio.h>
int a=5;
void main()
{
int a=10;
printf("%d\n",a);
printf("%d\n",::a);
a=::a+a;
printf("%d\n",a);
printf("%d\n",::a);
::a=a;
printf("%d\n",a);
printf("%d\n",::a);
getch();
}
POINTERS:
--> Write a program to add two numbers using function. Pass the pointers to
the variables as reference to the function.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
void sum (int *p1, int *p2);

printf("Enter two numbers");


scanf("%d %d",&a,&b);
sum(&a,&b);
getch();
}
void sum (int *p1, int *p2)
{ 72
int c;
c=*p1+*p2;
printf("The sum is equal to %d",c);
}
--> Write a program to swap two numbers using function. Pass the values to be
swapped to this function using call-by-value method.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
void swap (int a, int b);

printf("Enter two numbers:");


scanf("%d %d",&a,&b);
printf("The values of a and b in the main function
before calling the swap function are %d and %d\n",a,b);
swap(a,b);
printf("The values of a and b in main function after
calling the swap function are %d and %d\n",a,b);
getch();
}
void swap (int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("The values of a and b in the swap function
after swapping are %d and %d\n",a,b);
}
--> Write a program to swap two numbers using function. Pass the values to be
swapped to this function using call-by-reference method.
73
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
void swap (int *p1, int *p2);

printf("Enter two numbers:");


scanf("%d %d",&a,&b);
printf("The values of a and b in the main function
before calling the swap function are %d and %d\n",a,b);
swap(&a,&b);
printf("The values of a and b in main function after
calling the swap function are %d and %d\n",a,b);
getch();
}
void swap (int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
printf("The values of a and b in the swap function
after swapping are %d and %d\n",*p1,*p2);
}

You might also like