Basic Programs in C
//1.Write a program to enter two numbers and find their sum.
#include <stdio.h>
void main()
{
int a,b,c;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
c = a+b;
printf("a+b = %d \n",c);
}
//2.Write a program to enter two numbers and perform all arithmetic
operations.
#include <stdio.h>
void main()
{
int a,b,c;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
printf("a/b = %d \n",c);
c=a%b;
printf("Remainder when a divided by b = %d \n",c);
}
//3.Write a program to enter length & breadth of a rectangle & find its
perimeter,area.
#include <stdio.h>
void main()
{
int l,b,p,a;
printf("Enter length of a rectangle(in cm):");
scanf("%d",&l);
printf("Enter breadth of a rectangle(in cm):");
scanf("%d",&b);
p=2*(l+b);
a=l*b;
printf("Perimeter of rectangle is %d cm \n",p);
printf("Area of rectangle is %d sq.cm \n",a);
}
//4.Write a program to enter radius of a circle and find its diameter,
circumference and area.
#include <stdio.h>
void main()
{
float r,d,c,a;
printf("Enter radius of a circle(in cm):");
scanf("%f",&r);
d=2*r;
c=2*3.14*r,
a=3.14*(r*r);
printf("Diameter of circle is %f cm \n",d);
printf("Circumference of circle is %f cm \n",c);
printf("Area of circle is %f sq.cm \n",a);
}
//5.Write a program to enter length in centimeter and convert it into meter
and kilometer.
#include <stdio.h>
void main()
{
float l,m,km;
printf("Enter length(in cm):");
scanf("%f",&l);
m=l/100;
km=m/1000;
printf("Length= %f cm \n",l);
printf("Length= %f m \n",m);
printf("Length= %f km \n",km);
}
//6.Write a program to enter marks of five subjects and calculate total,
average and percentage.
#include <stdio.h>
void main()
{
float tel,eng,mat,phy,chem,sum,avg,p;
printf("Each subject exam is conducted for 100 marks \n");
printf("Enter obtained marks in Telugu:");
scanf("%f",&tel);
printf("Enter obtained marks in English:");
scanf("%f",&eng);
printf("Enter obtained marks in Maths:");
scanf("%f",&mat);
printf("Enter obtained marks in Physics:");
scanf("%f",&phy);
printf("Enter obtained marks in Chemistry:");
scanf("%f",&chem);
sum=tel+eng+mat+phy+chem;
avg=sum/5;
p=(sum/500)*100;
printf("Total obtained marks: %f \n",sum);
printf("Average: %f \n",avg);
printf("Percentage: %f \n",p);
}
//7.Write a program to enter two angles of a triangle & find the third angle.
#include <stdio.h>
void main()
{
int a,b,c;
printf("Enter first angle of triangle:");
scanf("%d",&a);
printf("Enter second angle of triangle:");
scanf("%d",&b);
c=180-(a+b);
printf("Third angle of triangle is %d degrees \n",c);
}
//8.Write a program to enter base and height of a triangle and find its area.
#include <stdio.h>
void main()
{
float b,h,a;
printf("Enter base of a triangle(in cm):");
scanf("%f",&b);
printf("Enter height of a triangle(in cm):");
scanf("%f",&h);
a=0.5*b*h;
printf("Area of a triangle is %f sq.cm \n",a);
}
//9.Write a program to enter temperature in °Celsius & convert it into
°Fahrenheit.
#include <stdio.h>
void main()
{
float c,f;
printf("Enter temperature in °Celsius:");
scanf("%f",&c);
f=((9.0/5.0)*c)+32;
printf("Temperature in °Fahrenheit: %f °F \n",f);
}
//10.Write a program to enter temperature in Fahrenheit(°F) and convert it
into Celsius(°C)
#include <stdio.h>
void main()
{
float f,c;
printf("Enter temperature in °Fahrenheit:");
scanf("%f",&f);
c=((5.0/9.0)*(f-32));
printf("Temperature in °Celsius: %f °C \n",c);
}
//11.Write a program to calculate simple interest.
#include <stdio.h>
void main()
{
float p,t,r,n,si,ci;
printf("Enter Principal amount:");
scanf("%f",&p);
printf("Enter time period(in years):");
scanf("%f",&t);
printf("Enter annual interest rate:");
scanf("%f",&r);
si=(p*t*r)/100;
printf("Simple Interest: %f Rs \n",si);
}
//12a.Program for swapping of a two numbers using third variable
#include <stdio.h>
void main()
{
int a,b,c;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
printf("Before swapping: a=%d b=%d \n",a,b);
c=a;
a=b;
b=c;
printf("After swapping: a=%d b=%d \n",a,b);
}
//12b.Program for swapping of a two numbers without using third variable
#include <stdio.h>
void main()
{
int a,b;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
printf("Before swapping: a=%d b=%d \n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping: a=%d b=%d \n",a,b);
}