C programming Example
-- Write a c Program to find largest among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3,highest;
clrscr();
printf("Enter first number");
scanf("%d",&num1);
printf("Enter second number");
scanf("%d",&num2);
printf("Enter third number");
scanf("%d",&num3);
if((num1>num2)&&(num1>num3))
{
printf("FIRST NO IS HIGHEST");
}
else if((num2>num1)&&(num2>num3))
{
printf("SECOND NO IS HIGHEST");
}
else
{
printf("THIRD NUMBER IS HIGHEST");
}
}
Example 2:- Write a program in c to calculate simple Interest .
/ /Function with argument and return value
//Calculate Simple Interest
#include<stdio.h>
#include<conio.h>
float calculatesi(int amt,float rate, float no)
{
float si;
si=(amt*no*rate)/100;
return si;
}
void main()
{
int p;
float n,r,si;
clrscr();
printf("\nEnter Principal Amount, no of years, rate of interest : ");
scanf("%d%f%f",&p,&n,&r);
si=calculatesi(p,n,r);
printf("\nSI=%f",si);
getch();
}
Example 3.
/*Write a program to print area of a circle. A(circle)= 3.142 * R * R in C language*/
#include<stdio.h>
#include<conio.h>
void main()
{
float AREA, R;
clrscr();
printf("Enter redius;");
scanf("%f",&R);
AREA = 3.14*R*R;
printf("AREA of the givan is %6.2f",AREA);
getch();
}
Example 4:-
/* write a program to print simple interest SI=(PNR)/100 in C lang */
#include<stdio.h>
#include<conio.h>
main()
{
float SI,P,N,R;
clrscr();
printf("\n enter Principle Amount:");
scanf("%f",&P);
printf("\n enter the number of years:");
scanf("%f",&N);
printf("\n enter the rate of interest:");
scanf("%f",&R);
SI=(P*N*R)/100;
printf(" \n Simple interest is : %6.2f",SI);
getch();
return(0);
}
Example 5:- /* Find a wheather number is a prime */
#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,a=0;
printf("\n enter the numnber:");
scanf("%d",&no);
for(i=2;i<=no/2;i++)
{
if(no%i==0)
{
a=1;
break;
}
}
if(a==1)
printf("\n not a prime");
else
printf("\ entered number is a prime num:");
getch();
}
Example 6:- #include<stdio.h>
#include<conio.h>
main()
{
float AREA,B,H;
clrscr();
printf("Enter Base & Height: ");
scanf("%f%f",&B,&H);
AREA=0.5*B*H;
printf("Area of the given is : %6.2f",AREA);
getch();
}
Example 7 :-
•Write a program to print simple interest SI = (PNR)/100 in C language
#include<stdio.h>
#include<conio.h>
main()
{
float SI,P,N,R;
clrscr();
printf("Enter Principle Amount: ");
scanf("%f%f%f",&P,&N,&R);
SI=(P*N*R)/100;
printf("Simple Interest is : %6.2f",SI);
getch();
}
Example 8: /* Display the reverse of given number */
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem=0,rev=0;
clrscr();
printf("\n enter the number:");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
num=num/10;
rev=rev*10+rem;
}
printf("\n the reverse of a number =%d",rev);
getch();
}
Example 9:- /*• Write a program to accept a number from user and print it’s square & cube in C language*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,Square,Cube;
clrscr();
printf("Enter Number: ");
scanf("%d",&n);
Square=n*n;
Cube=n*n*n;
printf("\nSquare: %d\nCube: %d",Square,Cube);
getch();
}
Example 10:-
/*•Write a program to accept a number from user and print it’s square & cube in C language*/
#include<stdio.h>
#include<conio.h>
main()
{
int s,Square,Cube;
clrscr();
printf("Enter Number: ");
scanf("%d",&s);
Square=s*s;
Cube=s*s*s;
printf("\nSquare: %d\nCube: %d",Square,Cube);
getch();
return (0);
}
Example 11:- /* Write a program to accept a number and print if the number is Positive/Negative in C language*/
#include<stdio.h>
#include<conio.h>
main()
{
int num;
clrscr();
printf("Enter number..");
scanf("%d",&num);
if(num>0)
printf("Given number is positive");
else if(num<0)
printf("Given number is negative");
else
printf("Number is Zero");
getch();
return(0);
}
Example :-12 • Write a Simple Program to print numbers 1 to n using while loop in C language
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("Enter n : ");
scanf("%d",&n);
while(i<=n)
{
printf("%d\t",i);
i++;
}
getch();
}
13:- /* Write a program to calculate the sum of n natural number given by user*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr(); //to clear the screen
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
printf("\n%d",i);
sum+=i;
}
printf("\n\nSum of first n natural numbers is:%d",sum);
getch(); //to stop the screen
}
14. C program to find the factorial of a given number
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,m,fact=1;
clrscr();
x=2;
y=1;
while(x<=10)
{
while(y<=x)
{
fact=fact*y;
y++;
} printf("fact=%dis %d\n",x,fact);
printf("\n");
x++;
}
getche();
return(0);
}
-- Write a c Program to find largest among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3,highest;
clrscr();
printf("Enter first number");
scanf("%d",&num1);
printf("Enter second number");
scanf("%d",&num2);
printf("Enter third number");
scanf("%d",&num3);
if((num1>num2)&&(num1>num3))
{
printf("FIRST NO IS HIGHEST");
}
else if((num2>num1)&&(num2>num3))
{
printf("SECOND NO IS HIGHEST");
}
else
{
printf("THIRD NUMBER IS HIGHEST");
}
}
Example 2:- Write a program in c to calculate simple Interest .
/ /Function with argument and return value
//Calculate Simple Interest
#include<stdio.h>
#include<conio.h>
float calculatesi(int amt,float rate, float no)
{
float si;
si=(amt*no*rate)/100;
return si;
}
void main()
{
int p;
float n,r,si;
clrscr();
printf("\nEnter Principal Amount, no of years, rate of interest : ");
scanf("%d%f%f",&p,&n,&r);
si=calculatesi(p,n,r);
printf("\nSI=%f",si);
getch();
}
Example 3.
/*Write a program to print area of a circle. A(circle)= 3.142 * R * R in C language*/
#include<stdio.h>
#include<conio.h>
void main()
{
float AREA, R;
clrscr();
printf("Enter redius;");
scanf("%f",&R);
AREA = 3.14*R*R;
printf("AREA of the givan is %6.2f",AREA);
getch();
}
Example 4:-
/* write a program to print simple interest SI=(PNR)/100 in C lang */
#include<stdio.h>
#include<conio.h>
main()
{
float SI,P,N,R;
clrscr();
printf("\n enter Principle Amount:");
scanf("%f",&P);
printf("\n enter the number of years:");
scanf("%f",&N);
printf("\n enter the rate of interest:");
scanf("%f",&R);
SI=(P*N*R)/100;
printf(" \n Simple interest is : %6.2f",SI);
getch();
return(0);
}
Example 5:- /* Find a wheather number is a prime */
#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,a=0;
printf("\n enter the numnber:");
scanf("%d",&no);
for(i=2;i<=no/2;i++)
{
if(no%i==0)
{
a=1;
break;
}
}
if(a==1)
printf("\n not a prime");
else
printf("\ entered number is a prime num:");
getch();
}
Example 6:- #include<stdio.h>
#include<conio.h>
main()
{
float AREA,B,H;
clrscr();
printf("Enter Base & Height: ");
scanf("%f%f",&B,&H);
AREA=0.5*B*H;
printf("Area of the given is : %6.2f",AREA);
getch();
}
Example 7 :-
•Write a program to print simple interest SI = (PNR)/100 in C language
#include<stdio.h>
#include<conio.h>
main()
{
float SI,P,N,R;
clrscr();
printf("Enter Principle Amount: ");
scanf("%f%f%f",&P,&N,&R);
SI=(P*N*R)/100;
printf("Simple Interest is : %6.2f",SI);
getch();
}
Example 8: /* Display the reverse of given number */
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem=0,rev=0;
clrscr();
printf("\n enter the number:");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
num=num/10;
rev=rev*10+rem;
}
printf("\n the reverse of a number =%d",rev);
getch();
}
Example 9:- /*• Write a program to accept a number from user and print it’s square & cube in C language*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,Square,Cube;
clrscr();
printf("Enter Number: ");
scanf("%d",&n);
Square=n*n;
Cube=n*n*n;
printf("\nSquare: %d\nCube: %d",Square,Cube);
getch();
}
Example 10:-
/*•Write a program to accept a number from user and print it’s square & cube in C language*/
#include<stdio.h>
#include<conio.h>
main()
{
int s,Square,Cube;
clrscr();
printf("Enter Number: ");
scanf("%d",&s);
Square=s*s;
Cube=s*s*s;
printf("\nSquare: %d\nCube: %d",Square,Cube);
getch();
return (0);
}
Example 11:- /* Write a program to accept a number and print if the number is Positive/Negative in C language*/
#include<stdio.h>
#include<conio.h>
main()
{
int num;
clrscr();
printf("Enter number..");
scanf("%d",&num);
if(num>0)
printf("Given number is positive");
else if(num<0)
printf("Given number is negative");
else
printf("Number is Zero");
getch();
return(0);
}
Example :-12 • Write a Simple Program to print numbers 1 to n using while loop in C language
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("Enter n : ");
scanf("%d",&n);
while(i<=n)
{
printf("%d\t",i);
i++;
}
getch();
}
13:- /* Write a program to calculate the sum of n natural number given by user*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr(); //to clear the screen
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
printf("\n%d",i);
sum+=i;
}
printf("\n\nSum of first n natural numbers is:%d",sum);
getch(); //to stop the screen
}
14. C program to find the factorial of a given number
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,m,fact=1;
clrscr();
x=2;
y=1;
while(x<=10)
{
while(y<=x)
{
fact=fact*y;
y++;
} printf("fact=%dis %d\n",x,fact);
printf("\n");
x++;
}
getche();
return(0);
}