25 Frequently Asked C- programs
Introduction
In this article, you will be able to learn and know about some essential C-programs that are
frequently and often asked in university exams.
Some important C-programs are:
Write a program to display your address.
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“ Raj
Kumar ”);
printf(“\n S/O
Manoj Kumar ”);
printf(“ \n Main
road”);
printf(“ \n
Hazaribagh-825301”);
getch();
}
Output:
Raj Kumar
S/O Manoj Kumar
Main road
Hazaribagh-825301
Write a program to find the area of a rectangle.
#include<stdio.h>
#include<conio.h>
void main()
{
int l,b, ar;
l=10;
b=5;
ar=l*b;
printf(“\n Area=%d
”, ar);
getch();
}
Output:
Area=50
Write a program to find the area of a triangle.
#include<stdio.h>
#include<conio.h>
void main()
{
int b,h, ar;
b=10;
h=5;
ar=b*h/2;
printf(“\nArea=%d
”,ar);
getch();
}
Output:
Area=25
Write a program to find area of a square.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, ar;
a=5;
ar=a*a;
printf(“\nArea=%d”,ar);
getch();
}
Output:
Area=25
Write a program to find the area of a circle.
#include<stdio.h>
#include<conio.h>
void main()
{
float pi,r, ar;
pi=3.14159;
r=7;
ar=pi*r*r;
printf(“\nArea=%f
”,ar);
getch();
}
Output:
Area=153.93791
Write a program to find simple interest.
#include<stdio.h>
#include<conio.h>
void main()
{
float p, r, t,
si;
p=1000;
r=6.5;
t=2;
si=p*r*t/100;
printf(“\n Si=%f
”,si);
getch();
}
Output:
Si=130
Write a program to find the sum of
first n natural numbers.
#include<stdio.h> /*function prototype*/
#include<conio.h>
void main( )
{
int n,sum; /*variable
declaration statement*/
printf(“\nEnter the numbers of terms whose sum is required:”);
scanf(“%d”,&n);
sum=n*(n+1)/2;
printf(“\nSum of first %d natural numbers=%d”,n,sum);
getch( ); /*freeze the
monitor*/
}
Output:
Enter the numbers of terms whose sum is required:5
Sum of first 5 natural numbers=15
Write a program to swap two numbers using a third variable.
#include<stdio.h>
/*function prototype*/
#include<conio.h>
void main( ) /*variable
declaration statement*/
{
int a,b,c; /*variable initialization statement*/
clrscr( ); /*clear the
screen*/
a=10;
b=20; /*variable
initialization statement*/
printf(“\nValues before swapping”);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
c=a;
a=b;
b=c;
printf(“\nValues after swapping\n”);
printf(“a=%d”,a);
printf(“\nb=%d”,b);
getch( ); /*freeze the
monitor*/
}
Output:
Values before swapping.
a=10
b=20
Values after swapping.
a=20
b=10
OR
Write a program to swap two numbers without using a third
variable.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b; /*variable
declaration statement*/
clrscr( ); /*clear the
screen*/
a=10;
b=20; /*variable
initialization statement*/
printf(“\nValues before swapping”);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
a=a+b;
b=a-b; /*Assignment
statement*/
a=a-b;
printf(“\nValues after swapping”);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
getch( ); /*freeze the
monitor*/
}
Output:
Values before swapping.
a=10
b=20
Values after swapping.
a=20
b=10
Write a program to convert centigrade into Fahrenheit and
vice versa.
#include<stdio.h> /*function
prototype*/
#include<conio.h>
void main( )
{
float c1,c2,f1,f2;
clrscr( );
printf(“\nEnter temperature in celsius:”);
scanf(“%f”,c1);
f1=(9*c)/5+32; /*Assignment statement*/
printf(“\nTemperature in Fahrenheit=%f”,f1);
printf(“\nEnter temperature in fahrenheit:”);
scanf(“%f”,f2);
c2=(f-32)*9/5;
printf(“\nTemperature in Celsius= %f”,c2);
getch( ); /*freeze the
monitor*/
}
Output:
Enter temperature in Celsius:5
Temperature in Fahrenheit=41
Enter temperature in Fahrenheit:40
Temperature in Celsius=4.444445
Write a program to display a number using a conditional
operator.
#include<stdio.h>
/*function prototype*/
#include<conio.h>
void main( )
{
int a,b; /*variable
declaration */
printf(“\nEnter a number:”);
scanf(“%d”,&a);
b=(a>=10)? 20:50;
printf(“\n%d ”,b);
getch(); /*freeze the
monitor*/
}
Output:
Enter a number: 22
20
Write a program to find the greater of two numbers.
#include<stdio.h> /*function prototype*/
#include<conio.h>
void main( )
{
int a,b; /*variable declaration statement*/
clrscr( );
printf(“\nEnter two
numbers:”);
scanf(“\n%d \n%d
\n”,&a,&b);
if(a>b)
{
printf(“\n%d is
greater”,a);
}
else
{
printf(“\n%d is
greater”,b);
}
getch(); /*freeze the
monitor*/
}
Output:
Enter two numbers:
5
10
10 is greater
Write a program to find the greatest of three numbers.
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void main( )
{
int a,b,c; /*variable declaration statement*/
clrscr( );
printf(“\nEnter three
numbers:”);
scanf(“\n%d \n %d \n
%d\n”,&a,&b,&c);
if((a>b)&&(a>c))
{
printf(“\n%d is
greatest”,a);
}
elseif
((b>a)&&(b>c))
{
printf(“\n%d is
greatest”,b);
}
else
{
printf(“\n%d is
greatest”,c);
}
getch( ); /*freeze the
monitor*/
}
Output:
Enter three numbers:
5
6
7
7 is greatest
Write a program to display days of a week using a switch
case.
#include<stdio.h> /*function
prototype*/
#include<conio.h>
void main( )
{
int ch; /*variable declaration statement*/
clrscr( );
printf(“\nEnter your
choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\nSunday”);
break;
case 2:
printf(“\nMonday”);
break;
case 3:
printf(“\nTuesday”);
break;
case 4:
printf(“\nWednesday”);
break;
case 5:
printf(“\nThursday”);
break;
case 6:
printf(“\nFriday”);
break;
case 7:
printf(“\nSaturday”);
break;
default:
printf(“\nInvalid
number”);
break;
}
getch(); /*freeze the
monitor*/
}
Output:
Enter your choice: 2
Monday
Write a program to display 1st five even
numbers.
#include<stdio.h>
#include<conio.h> /*function prototype*/
void main()
{
int a; /*variable declaration statement*/
for(a=1;a<=10;a++) /*for loop begins*/
{
if(a%2==0) /*conditional statement*/
{
printf(“%d
\n”,a);
}
}
getch(); /*freeze the
monitor*/
}
Output:
2
4
6
8
10
OR
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void main()
{
int a; /*variable declaration statement*/
for(a=2;a<=10;a+=2) /*for loop begins*/
{
printf(“%d
\n”,a);
}
getch(); /*freeze the
monitor*/
}
Output:
2
4
6
8
10
OR
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void main()
{
int a,b; /*variable declaration statement*/
for(a=1;a<=5;a++) /*for loop begins*/
{
b=2*a; /*Assignment
statement*/
printf(“%d
\n”,b);
}
getch(); /*freeze the
monitor*/
}
Output:
2
4
6
8
10
Write a program to display 1st five odd
numbers.
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void main()
{
int a; /*variable declaration statement*/
for(a=1;a<=10;a+=2) /*for loop begins*/
{
printf(“%d
\n”,a);
}
getch(); /*freeze the
monitor*/
}
Output:
1
3
5
7
9
OR
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void main()
{
int a; /*variable declaration statement*/
for(a=1;a<=10;a++) /*for loop begins*/
{
if(a%2!=0) /*Conditional statement*/
{
printf(“%d
\n”,a);
}
}
getch(); /*freeze the
monitor*/
}
Output:
1
3
5
7
9
Write a program to find the first 5 odd numbers using for
loop.
#include<stdio.h>
#include<conio.h> /*function prototype*/
void main()
{
int a,b; /*variable
declaration statement*/
for(a=0;a<=4;a++) /*for loop begins*/
{
b=2a+1; /*Assignment
statement*/
printf(“\n%d”,b);
}
getch(); /*freeze the
monitor*/
}
Output:
1
3
5
7
9
Write a program to find a factorial of 5 using for
loop.
#include<stdio.h>
#include<conio.h> /*function prototype*/
void main()
{
int a,b; /*variable declaration */
b=1;
for(a=1;a<=5;a++) /*for loop begins*/
{
b*=a; /*compound assignment operation*/
}
printf(“\nThe
required factorial=%d”,b);
getch(); /*freeze the
monitor*/
}
Output:
The required
factorial=120
Write a program to check a prime number.
#include<stdio.h>
#include<conio.h> /*function prototype*/
void main()
{
int n,a,c=0;
clrscr();
printf(“\n Enter
a number:”);
scanf(“%d”,&n);
for(a=1,a<=n;a++)
{
if (n%a==0) /*conditional statement*/
c++;
}
if(c++2)
printf(“%d is a
prime number”,n);
else
printf(“%d is not
a prime number”,n);
getch(); /*freeze the
monitor*/
}
Output:
Enter a number: 9
9 is not a prime
number
Enter a number: 5
5 is a prime
number
Write a program to display the following output.
*
**
***
****
#include<stdio.h>
#include<conio.h> /*function prototype*/
void main()
{
int a,b,n; /*variable declaration statement*/
printf(“\nEnter number of rows:”);
scanf(“%d”,&n);
for(a=1;a<=n;a++) /*for loop begins*/
{
for(b=1;b<=a;b++)
{
printf(“*”);
}
printf(“\n”);
}
getch(); /*freeze the
monitor*/
}
Output:
Enter number of
rows: 4
*
**
***
****
Write a program to display the following output.
****
***
**
*
#include<stdio.h>
#include<conio.h> /*function prototype*/
void main()
{
int a,b,n; /*variable declaration statement*/
printf(“\nEnter number of rows:”);
scanf(“%d”,&n);
for(a=1;a<=n;a++)
{
for(b=a;b<=n;b++)
{
printf(“*”);
}
printf(“\n”);
}
getch(); /*freeze the
monitor*/
}
Output:
Enter number of
rows: 4
****
***
**
*
Write a program to display the following output.
1
22
333
4444
55555
#include<stdio.h>
#include<conio.h> /*function prototype*/
void main()
{
int a,b,n;
printf(“\nEnter number of rows:”);
scanf(“%d”,&n);
for(a=1;a<=n;a++) /*for loop begins*/
{
for(b=1;b<=a;b++)
{
printf(“%d”,a);
}
printf(“\n”);
}
getch(); /*freeze the
monitor*/
}
Output:
Enter number of
rows:5
1
22
333
4444
55555
Write a program to display Fibonacci series.
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void main( )
{
int a,b,c,n; /*variable declaration statement*/
printf(“\nEnter the number of terms:”);
scanf(“%d”,&n);
a=0;
b=1; /*Assignment
statement*/
printf(“\n%6d%6d”,a,b);
c=a+b;
do /*do while loop begins*/
{
printf(“%6d”,c);
a=b;
b=c; /*Assignment statement*/
c=a+b;
}while(c<=n);
getch(); /*freeze the
monitor*/
}
Output:
Enter the number of terms: 5
0 1
1 2 3
5
Enter the number of terms: 6
0 1
1 2 3
5 8
Write a program to reverse a given number using do while loop.
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void main( )
{
int n,rev;
/*variable declaration statement*/
printf(“\nEnter a number:”);
scanf(“%d”,&n);
printf(“\nReversed number:”);
do /*do while loop begins*/
{
rev=(n%10);
printf(“%d”,rev);
n/=10; /*Assignment
statement*/
}while(n!=0);
getch(); /*freeze the
monitor*/
}
Output:
Enter a number:123
Reversed number:321
Write a program
to check that a number is an Armstrong number.
#include<stdio.h>
#include<conio.h>
/*function prototype*/
#include<math.h>
main( )
{
int a,b,c,sum;
clrscr();
sum=0;
printf(“\nEnter a number:”);
scanf(“%d”,&a);
b=a;
while(b!=0) /*while loop begins*/
{
c=b%10;
sum+=pow(c,3); /*Assignment
statement*/
b=b/10;
}
if(sum==a)
{
printf(“\n\n%d is an Armstrong number:”,a);
}
else
{
printf(“\n\n%d is not an Armstrong number:”,a);
}
getch(); /*freeze the
monitor*/
}
Output:
Enter a number: 153
153 is an Armstrong number.
Enter a number: 50
50 is not an Armstrong number.
Write a program to display reverse of a number.
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void main( )
{
int a,b;
printf(“\nEnter a number:”);
scanf(“%d”,&a);
printf(“\nReversed number:”);
while(a!=0)
{
b=a%10; /*Assignment
statement*/
printf(“%d”,b);
a/=10;
}
getch(); /*freeze the
monitor*/
}
Output:
Enter a number: 123
Reversed number: 321
Enter a number: 456
Reversed number: 654
To know more visit https://zueducator.blogspot.com
No comments:
Post a Comment