Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

1.

To print a text "Om Sri Sai Ram" in C


// Program to print a text "Om Sri Sai Ram"//
#include<stdio.h>
int main()
{
printf("Om Sri Sai Ram");
return 0;
}

Output:
Om Sri Sai Ram
Process returned 0 (0x0) execution time : 0.125 s

2.Program to find basic arithmatic operations of two numbers


// Program to find basic arithmatic operations of two numbers//
#include<stdio.h>
int main()
{
int n1,n2;
printf("Enter two numbers:");
scanf("%d,%d",&n1,&n2);
printf("The sum of the numbers %d + %d = %d",n1,n2,n1+n2);
printf("The difference of the numbers %d - %d = %d",n1,n2,n1-n2);
printf("The product of the numbers %d * %d = %d",n1,n2,n1*n2);
printf("The division of the numbers %d + %d = %d",n1,n2,n1/n2);
return 0;
}
Output:
Enter two numbers:
10
5

The sum of the numbers 10 + 5 = 15


The difference of the numbers 10 - 5 = 5
The product of the numbers 10 * 5 = 50
The division of the numbers 10 + 5 = 2
Process returned 0 (0x0) execution time : 4.547 s

3. Program to find area of square,circle and triangle


//Program to find area of square,circle and triangle//
#include<stdio.h>
int main()
{
int side,rad,base,ht,pi=3.14;
float a=0.5;
printf("\nArea of square: ");
printf("\nEnter the length of side:");
scanf("%d",&side);
printf("\nArea of square:%d ",side*side);
printf("\nArea of circle: ");
printf("\nEnter the radius:");
scanf("%d",&rad);
printf("\nArea of circle:%d",pi*rad*rad);
printf("\nArea of triangle: ");
printf("\nEnter the height:");
scanf("%d",&ht);
printf("\nEnter the base:");
scanf("%d",&base);
printf("\nArea of triangle: %f",a*base*ht);
return 0;
}
Output:
Area of square:
Enter the length of side:5
Area of square:25

Area of circle:
Enter the radius:5
Area of circle:75

Area of triangle:
Enter the height:4
Enter the base:6
Area of triangle: 12.000000
Process returned 0 (0x0) execution time : 7.891 s

4.Find whether the number is divisible by 3 or not


//Program to find whether the given number is divisible by 3 or not//
#include<stdio.h>
void main()
{
int a;
clrscr();
printf("Enter a number:\n");
scanf("%d",&a);
if(a%3==0)
printf("The number %d is divisible by 3");
else
printf("The number %d is not divisible by 3");
getch();
}
Output:
Enter a number:
98
The number 96 is not divisible by 3
Process returned 0 (0x0) execution time : 3.625 s

Enter a number:
81
The number 81 is divisible by 3
Process returned 0 (0x0) execution time : 21.979 s

5. Find whether the number is zero or not


/*Program to find whether the number is zero or not*/
#include<stdio.h>
void main()
{
int no;
clrscr();
printf("Enter a number:\n");
scanf("%d",&no);
if(no==0)
printf("The given number %d is zero",no);
else
printf("The given number %d is not zero",no);

getch();
}

Output:

Enter a number:
0
The given number 0 is zero
Process returned 0 (0x0) execution time : 2.969 s

Enter a number:
5
The given number 5 is not zero
Process returned 0 (0x0) execution time : 2.005 s

6. Find whether the number is odd or even


/* Program to find whether the number is odd or even*/
#include<stdio.h>
void main()
{
int no;
clrscr();
printf("Enter a number: \n");
scanf("%d",&no);
if(no%2==0)
printf("The number %d is even",no);
else
printf("The number %d is odd",no);
getch();
}
Output:
Enter a number:
75
The number 75 is odd
Process returned 0 (0x0) execution time : 5.047 s

Enter a number:
46
The number 46 is even
Process returned 0 (0x0) execution time : 15.891 s

7. Find whether the number is prime or not


/*Program to find whether the number is prime or not*/
#include<stdio.h>
void main()
{
int i,a,count=0;
clrscr();
printf("Enter a number:\n");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if(count==2)
printf("Prime");
else
printf("Not Prime");
getch();
}

Output:
Enter a number:
5
Prime
Process returned 0 (0x0) execution time : 4.047 s

Enter a number:
81
Not Prime
Process returned 0 (0x0) execution time : 2.469 s

8.Find Simple Interest of a given number

// Program to find Simple Interest //


#include<stdio.h>
int main()
{
int P,n,r,SI;
printf("Calculation of Simple Interest");
printf("\nEnter the Principal amount: ");
scanf("%d",&P);
printf("Enter the number of years: ");
scanf("%d",&n);
printf("Enter the rate of interest: ");
scanf("%d",&r);
SI=(P*n*r)/100;
printf("The Simple Interest: %d",SI);
return 0;
}

Output:
Calculation of Simple Interest
Enter the Principal amount: 55
Enter the number of years: 4
Enter the rate of interest: 5
The Simple Interest: 11
Process returned 0 (0x0) execution time : 9.391 s

9. Find the Sum of the numbers


// Program to find Sum of the numbers //
#include<stdio.h>
int main()
{
int sum=0,n,a[20],i;
printf("Calculation of Sum of numbers");
printf("\nEnter the number of inputs to be added:");
scanf("%d",&n);
printf("Enter the numbers to be added:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
for(i=1;i<=n;i++)
{
printf("%d+",a[i]);
}
printf("\nThe sum is: %d",sum);
return 0;
}

Output:
Calculation of Sum of numbers
Enter the number of inputs to be added:5
Enter the numbers to be added:
76
456
34
56
67
76+456+34+56+67+
The sum is: 689
Process returned 0 (0x0) execution time : 15.117 s

10. Find largest of n numbers

//Program to find largest of n numbers//


#include<stdio.h>
void main()
{
int i ,no,largest,current;
clrscr();
printf("Enter a number");
scanf("%d",&largest);
printf("Total numbers");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
printf("Enter a number:");
scanf("%d",&current);
if(current>largest)
{
largest=current;
}
}
printf("Largest=%d",largest);
getch();
}

Output:
Enter number: 45
Total numbers: 6
Enter number :45
Enter number :76
Enter number :87
Enter number :23
Enter number :098
Enter number :67
Largest=98
Process returned 0 (0x0) execution time : 12.420 s

11. Find the largest and smallest of two numbers

/*PROGRAM TO FIND LARGEST OF THREE NUMBERS*/


#include<stdio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("Enter 3 numbers:\n");
scanf("%d %d %d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
{
printf("%d is the largest\n",n1);
{
if(n2<n3)
printf("%d is the smallest\n",n2);
else
printf("%d is the smallest\n",n3);
}
}
else
{
printf("%d is the largest\n",n3);
printf("%d is the smallest\n",n2);
}
}
else
{
if(n2>n3)
{
printf("%d is the largest\n",n2);
{
if(n1<n3)
printf("%d is the smallest\n",n1);
else
printf("%d is the smallest\n",n3);
}
}
else
{
printf("%d is the largest\n",n3);
{
if(n1<n2)
printf("%d is the smallest\n",n1);
else
printf("%d is the smallest\n",n2);
}
}
}
getch();
}
Output:

Enter 3 numbers:
98
56
72
98 is the largest
56 is the smallest

12.Find the sum of n natural numbers


/*Program to find sum of n natural numbers */
#include<stdio.h>
int main()
{
int i=1,no,sum=0;
printf("Enter a number:");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
printf("%d ",i);
}
do
{
sum=sum+i;
i++;
}while(i<=no);
printf("\nSum of the natural numbers till %d is %d",no,sum);
return 0;
}

Output:
Enter a number:7
1234567
Sum of the natural numbers till 7 is 8
Process returned 0 (0x0) execution time : 2.125 s

13.Find the n th table of a given number

/*Program to find tables*/


#include<stdio.h>
void main()
{
int a,i,n;
clrscr();
printf("Enter a number:\n");
scanf("%d",&a);
for(i=1;i<=10;i++)
{
n=i*a;
printf("%d * %d= %d\n",i,a,n);
}
getch();
}

Output:
Enter a number:
25
1 * 25= 25
2 * 25= 50
3 * 25= 75
4 * 25= 100
5 * 25= 125
6 * 25= 150
7 * 25= 175
8 * 25= 200
9 * 25= 225
10 * 25= 250

Process returned 0 (0x0) execution time : 11.609 s

14. Find whether the given number is armstrong or not:

// Program to find whether the given number is armstrong or not//


#include<stdio.h>
int main()
{
int temp,num,sum=0,r;
printf("Enter a number:");
scanf("%d",&num);
temp=num;
while(temp>0)
{
r=temp%10;
sum=sum+pow(r,3);
temp=temp/10;
}
if(sum==num)
{
printf("It is a armstrong number.");
}
else{
printf("It is not a armstrong number.");
}
return 0;
}

Output:
Enter a number:153
It is a armstrong number.
Process returned 0 (0x0) execution time : 41.856 s

Enter a number:53
It is not a armstrong number.
Process returned 0 (0x0) execution time : 2.500 s

15.Find whether the given number is palindrome or not:

// Program to find whether the given number is palindrome or not//


#include<stdio.h>
void main()
{
int n,temp,sum=0,rem;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
temp=n;
while(temp>0)
{
rem=temp%10;
sum=(sum*10)+rem;
temp=temp/10;
}
if(sum==n)
printf("The number %d is Palindrome",n);
else
printf("The number %d is not a palindrome",n);
getch();
}

Output:
Enter a number:656

The number 656 is Palindrome


Process returned 0 (0x0) execution time : 7.766 s

Enter a number:766
The number 766 is not a palindrome

16. Find whether the given number is perfect or not:

// Program to find whether the given number is perfect or not//


#include<stdio.h>
int main()
{
int num,i,sum=0;
printf("Enter a number:");
scanf("%d",&num);
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum+=i;
printf("\n Divisor and current sum is %d and %d",i,sum);
}
}
if(sum==num)
{
printf("\n The number is perfect");
}
else
printf("\n The number is not perfect");
return 0;
}

Output:
Enter a number:6

Divisor and current sum is 1 and 1


Divisor and current sum is 2 and 3
Divisor and current sum is 3 and 6
The number is perfect
Process returned 0 (0x0) execution time : 3.547 s

Enter a number:36

Divisor and current sum is 1 and 1


Divisor and current sum is 2 and 3
Divisor and current sum is 3 and 6
Divisor and current sum is 4 and 10
Divisor and current sum is 6 and 16
Divisor and current sum is 9 and 25
Divisor and current sum is 12 and 37
Divisor and current sum is 18 and 55
The number is not perfect
Process returned 0 (0x0) execution time : 2.937 s

17.Find the GCD of a given number.

//Program to find the GCD of a given number//


#include<stdio.h>
void main()
{
int a,b,x,y,i,gcd;
clrscr();
printf("Enter the number:");
scanf("%d %d",&a,&b);
for(i=1;i<=a && i<=b;i++)
{
x=a%i;
y=b%i;
if(x==0 && y==0)
gcd=i;
}
printf("%d",gcd);
getch();
}

Output:
Enter the number:
27
81
The GCD of the given numbers 27 and 81 is 27
Process returned 0 (0x0) execution time : 8.047 s

18. Find the fibonacci of a given number.

//Program to find the fibonacci of a given number//


#include<stdio.h>
void main()
{
long int i,n,sum,a=1,b=0;
clrscr();
printf("Enter a number:");
scanf("%ld",&n);
printf("Fibonacci series:");
for(i=1;i<=n;i++)
{
sum=a+b;
a=b;
b=sum;
printf("%ld\t",sum);
}
getch();
}

Output:
Enter a number:10
Fibonacci series:1 1 2 3 5 8 13 21 34 55
Process returned 0 (0x0) execution time : 3.531 s

19.Swap the given two numbers


// Swap the given two numbers//

#include<stdio.h>
int main()
{
int var1,var2,temp,a,b;
printf("Enter two numbers:");
scanf("%d %d",&var1,&var2);
printf(" Before exchange \n Variable 1: %d \n Variable 2: %d",var1,var2);
temp=var1;
var1=var2;
var2=temp;
printf("\n After exchange \n Variable 1: %d \n Variable 2: %d",var1,var2);
return 0;
}

Enter two numbers:


45
76
Before exchange
Variable 1: 45
Variable 2: 76
After exchange
Variable 1: 76
Variable 2: 45
Process returned 0 (0x0) execution time : 5.500 s

20. Find the grade using sentinel loop


/* Program to find the average of given marks using while */
#include<stdio.h>
void main()
{
int mark,no=0;
float sum=0,avg;
clrscr();
printf(" Enter your marks : enter -1 at the end \n");
scanf("%d",&mark);
while(mark!=-1)
{
sum+=mark;
no++;
scanf("%d",&mark);
}
avg=sum/no;
printf("The average is %2f",avg);
getch();
}

Output:
Enter your marks : enter -1 at the end
95
96
99
100
98
-1
The average is 97.599998
Process returned 0 (0x0) execution time : 22.016 s

21.Find the power of a given number

/Program to find power//


#include<stdio.h>
void main()
{
int i,no,pow,count,pdt=1;
printf(" Enter a number:\n");
scanf("%d",&no);
printf("Enter the exponent value till it should get calculated:");
scanf("%d",&pow);
for(i=1;i<=pow;i++)
{
pdt=pdt*no;
printf("\n %d^%d=%d",no,i,pdt);
}
getch();
}

Output:
Enter a number:
5
Enter the exponent value till it should get calculated:8

5^1=5
5^2=25
5^3=125
5^4=625
5^5=3125
5^6=15625
5^7=78125
5^8=390625
Process returned 0 (0x0) execution time : 11.641 s

23.Create a Floyds triangle


//Program to create a Floyds triangle//
#include<stdio.h>
void main()
{
int i,j,k=1,n;
clrscr();
printf("Enter the number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d \t",k);
k++;
}

printf("\n");
}

getch();
}

Output:
Enter the number of rows:5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Process returned 0 (0x0) execution time : 3.359 s

24.Create a constant pyramid

// Program to create constant pyramid //


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

Output:
Enter the number of rows:4
1
111
11111
1111111

Process returned 0 (0x0) execution time : 2.726 s

25. Create a star pyramid

//Program to create a star pyramid//


#include<stdio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
{
printf(" ");
}
for(j=1;j<=(2*i)-1;j++)
{
printf("*");
}
printf("\n");

}
getch();
}

Output:
Enter the number of rows:6
*
***
*****
*******
*********
***********
Process returned 0 (0x0) execution time : 2.516 s

28. Find the area of the figures


//Program to find the area of the figures//
#include<stdio.h>
int main()
{
int choice;
int side,rad,base,ht,pi=3.14;
float a=0.5;
printf("Enter the figure for which you need to find area:\n1.Square\n2.Circle \n3.Triangle");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\nArea of square: ");
printf("\nEnter the length of side:");
scanf("%d",&side);
printf("Area of square:%d ",side*side);
break;
}

case 2:
{
printf("\nArea of circle: ");
printf("\nEnter the radius:");
scanf("%d",&rad);
printf("Area of circle:%d",pi*rad*rad);
break;
}

case 3:
{
printf("\nArea of triangle: ");
printf("\nEnter the height:");
scanf("%d",&ht);
printf("\nEnter the base:");
scanf("%d",&base);
printf("Area of triangle: %f",a*base*ht);
break;
}
default:
printf("Invalid choice");
}
return 0;
}

Output:
Enter the figure for which you need to find area:
1.Square
2.Circle
3.Triangle
1
Area of square:
Enter the length of side:5
Area of square:25
2
Area of circle:
Enter the radius:6
Area of circle:108
3
Area of triangle:
Enter the height:5
Enter the base:6
Area of triangle: 15.000000
Process returned 0 (0x0) execution time : 78.184 s
Press any key to continue
.
29. Find the electricity bill of a month
#include<stdio.h>
int main()
{
int met,x,y,a,b;
printf("Enter the meter readings:");
scanf("%d",&met);
if(met>0&&met<500)
printf("The rate is: $20");
else if(met>501&&met<1000)
{
x=met-500;
y=20+(x*0.03);
printf("The rate is: $%d",y);
}
else
{
a=met-1000;
b=35+(a*0.02);
printf("The rate is: $%d",b);

}
return 0;
}

Output:
Enter the meter readings:499
The rate is: $20
Process returned 0 (0x0) execution time : 5.994 s
30. Find GCD using Euclids Algorithm

//Program to find GCD using Euclids Algorithm//


#include<stdio.h>
void main()
{
int a,b,e;
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
e=gcd(a,b);
printf("The GCD of the given numbers %d and %d is %d",e);
getch();
}
int gcd(int c,int d)
{
if(d>c)
return(gcd(d,c));
else if(d==0)
return(c);
else
return(gcd(d,c%d));
}

Output:
Enter two numbers:
98
56
The GCD of the given numbers 98 and 56 is 14
32.Swap the given two numbers
// Program to swap the given two numbers//
#include<stdio.h>
void main()
{
int var1,var2;
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&var1,&var2);
printf(" Before exchange \n Variable 1: %d \n Variable 2: %d",var1,var2);
exchange(var1,var2);
getch();
}
exchange(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\n After exchange \n Variable 1: %d \n Variable 2: %d",a,b);
}

Output:
Enter two numbers:
56
87
Before exchange
Variable 1: 56
Variable 2: 87
After exchange
Variable 1: 87
Variable 2: 56
Process returned 0 (0x0) execution time : 3.891 s

You might also like