C Programmas

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

Program to add two numbers

#include <stdio.h>

int main()

int a,b,sum=0;

printf("Enter the numbers to be added\n");

scanf("%d %d",&a,&b);

sum=sum+a+b;

printf("%d+%d = %d",a,b,sum);

return 0;

Program to find area and perimeter of a circle

#include <stdio.h>

int main()

float r,a=1,p=1;

printf("Enter the radius\n");

scanf("%f",&r);

p=2*p*3.14*r;

a=3.14*r*r;

printf("%f is the perimeter of the circle and %f is the area",p,a);

return 0;

Program to find the lagest number among 3 integers

#include <stdio.h>
int main()
{

int a,b,c;

printf("Enter the number to be checked\n");


scanf("%d %d %d",&a,&b,&c);

if(a>b)

if(a>c)

printf("%d is greater",a);

else

printf("%d is greater ",c);

else

if(b>c)

printf("%d is greater ",b);

else

printf("%d is greater ",c);

return 0;

Program to check whether a number s prime or not

#include <stdio.h>

int main()

{
int a,i,flag=0;

printf("Enter the number to be checked\n");

scanf("%d",&a);

if(a==0||a==1)

flag=1;

for(i=2;i<=a/2;i=i+1)

if(a%i==0)

flag=1;

break;

if(flag==0)

printf("The number is prime",a);

else

printf("The number is not a prime number",a);

return 0;

Program to check whether a number is armstrong or not

#include <stdio.h>
int main()

int orginalnumber,number,rem,r,result;

printf("Enter the number to be checked\n");

scanf("%d",&orginalnumber);

number=orginalnumber;

while(number!=0)

rem=number%10;

r=rem*rem*rem;

result=result+r;

number=number/10;

if(result==orginalnumber)

printf("The number is an armstrong number\n");

else

printf("The number is not an armstrong number\n");

return 0;

Program to check if the number is odd or even

#include <stdio.h>

int main()
{

int a;

printf("Enter the number to be checked\n");


scanf("%d",&a);

if(a%2==0)

printf("The number is even\n");

else

printf("The number is odd\n");

return 0;

Program to check if a year is a leap year or not

#include <stdio.h>

int main()

int year;

printf("Enter the year to be checked\n");

scanf("%d",&year);

if((year%4==0)&&(year%100!=0)||(year%400==0))

printf("The given year is a leap year\n");

else

printf("The givn year is not a leap year\n");

return 0;
}

Reversal of a number

#include <stdio.h>

int main()

int num,rev=0,rem;

printf("Enter the munber to be reversed\n");

scanf("%d",&num);

while(num!=0)

rem=num%10;

rev=rev*10+rem;

num=num/10;

printf("The reversed number is %d\n",rev);

return 0;

Factorial of a number

#include<stdio.h>

int main()
{

int x,fact=1,n;

printf("Enter a number to find factorial: ");

scanf("%d",&n);

for(x=1;x<=n;x++)

fact=fact*x;

printf("Factorial of %d is: %d",n,fact);


return 0;

Solving quadratic equation

#include <stdio.h>

#include <math.h>

int main()

float a,b,c,rt1,rt2,sq1;

printf("Enter a,b,c\n");

scanf("%f %f %f",&a,&b,&c);

sq1=(b*b-4*a*c);

if(sq1>0)

rt1=(-b+sqrt(sq1))/(2*a);

rt2=(-b-sqrt(sq1))/(2*a);
{

printf("Solutions are %f and %f\n",rt1,rt2);

else

printf("No roots\n");

return 0;

Type of triangle

#include <stdio.h>

int main()
{

int side1,side2,side3;

printf("Enter the length of the sides\n");

scanf("%d %d %d",&side1,&side2,&side3);

if((side1==side2)&&(side2==side3)&&(side3==side1))

printf("The triangle is equilateral\n");

else if((side1==side2)||(side2==side3)||(side3==side1))

printf("The triangle is isoceles\n");

else

printf("The triangle is scalene\n");

return 0;

#include <stdio.h>
int main()
{
int org,num,sum=0,rem;
printf("Enter the number\n");
scanf("%d",&org);
num=org;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf("Sum of digits of number= %d\n",sum);
return 0;
}

You might also like