C PROGRAM 4
C PROGRAM 4
result = a + b;
printf("\n Addition of %d and %d is %d",a, b,result);
printf("\n\n");
}
2) Subtraction of second number from first
#include<stdio.h>
void main()
{
int a, b, result;
printf("\n Please enter first value:");
scanf("%d", &a);
printf("\n Please enter second value:");
scanf("%d", &b);
result = a - b;
printf("\n Subtraction of %d from %d is %d", a, b, result);
printf("\n\n");
}
3) Multiplication of two numbers
#include<stdio.h>
void main()
{
int a, b, result;
printf("\n Please enter first value:");
scanf("%d", &a);
printf("\n Please enter second value:");
scanf("%d", &b);
result = a * b;
printf("\n Multiplication of %d and %d is %d", a, b, result);
printf("\n\n");
}
4) Division of first number by second
#include<stdio.h>
void main()
{
float a, b, result;
printf("\n Please enter first value:");
scanf("%f", &a);
printf("\n Please enter second value:");
scanf("%f", &b);
result = a / b;
printf("\n Division of %.2f by %.2f is %.2f",a, b,result);
printf("\n\n");
}
I = (P * R * N)/100;
printf("\n Principal Amount: %10.2f", P);
printf("\n Rate of Interest: %10.2f", R);
printf("\n Total Duration in years: %10.2f", N)
printf("\n Calculated Interest: %10.2f", I);
printf("\n\n");
}
6) Find profit and Loss
#include<stdio.h>
void main()
{
float cp, sp;
printf("\nPlease enter cost price of the product: ");
scanf("%f", &cp);
printf("\nPlease enter selling price of the product: ");
scanf("%f", &sp);
A = PI * r * r;
printf("\nThe area of Circle with Radius %.2f is %.2f", r, A);
printf("\n\n");
}
2) Circumference of circle
#include<stdio.h>
#define PI 3.14
void main()
{
float r, c;
printf("\n Enter the Radius of Circle :");
scanf("%f", &r);
c = 2 * PI * r;
printf("\n The Circumference of a circle of Radius %.2f is %.2f", r,
c);
printf("\n\n");
}
3) Area of Square
#include<stdio.h>
void main()
{
float a , A;
printf("\n Enter the length:");
scanf("%f", &a);
A = a * a;
printf("\n The area of Square of Length %.2f is %.2f",a, A);
printf("\n\n");
}
4) Perimeter of Square
#include<stdio.h>
void main()
{
float P , a;
printf("\n Enter the length :");
scanf("%f", &a);
P = 4 * a;
printf("\n The perimeter of Square of Length %.2f is %.2f",a , P);
printf("\n\n");
}
5) Area of Rectangle
#include<stdio.h>
void main()
{
float A, L, B;
printf("\n Enter the length :");
scanf("%f",&L);
printf("\n Enter the Breadth :");
scanf("%f",&B);
A = L * B;
printf("\n The area of rectangle of length %.2f and breadth %.2f is
%.2f", L , B, A);
printf("\n\n");
}
6) Perimeter of Rectangle
#include<stdio.h>
void main()
{
float P, L, B;
printf("\n Enter the length :");
scanf("%f",&L);
printf("\n Enter the Breadth :");
scanf("%f",&B);
P = 2*(L + B);
printf("\n The area of rectangle of length %.2f and breadth %.2f is
%.2f", L , B, P);
printf("\n\n");
}
7) Area of Triangle
#include<stdio.h>
void main()
{
float A, L, B;
printf("\n Enter the length :");
scanf("%f",&L);
printf("\n Enter the Breadth :");
scanf("%f",&B);
A = (L * B)/2;
printf("\n The area of Triangle of length %.2f and breadth %.2f is
%.2f", L , B, A);
printf("\n\n");
}
8) Volume of cylinder
#include<stdio.h>
#define PI 3.14
void main()
{
float V, r, h;
printf("\n enter the radius of Cylinder :");
scanf("%f", &r);
printf("\n Enter the height of Cylinder :");
scanf("%f", &h );
V = PI * r * r * h;
printf("\n The volume of Cylinder of radius %f and height %f is %f
",r,h,V);
printf("\n\n");
}
9) Volume of Cone
#include<stdio.h>
#define PI 3.14
void main()
{
float V, r, h;
printf("\n enter the radius of Cone :");
scanf("%f", &r);
printf("\n Enter the height of Cone :");
scanf("%f", &h );
V = (PI * r * r * h)/3;
printf("\n The volume of cone of radius %f and height %f is %f
",r,h,V);
printf("\n\n");
}
10) Volume of sphere
#include<stdio.h>
#define PI 3.14
void main()
{
float V, r;
printf("\n enter the radius of Sphere :");
scanf("%f", &r);
V = (PI * r * r * r)*1.33;
printf("\n The volume of Sphere with radius %f is %f”, r, V);
printf("\n\n");
}
Conversions
1) Meters to Centimeters
#include<stdio.h>
void main()
{
float cm, m;
printf("\n Please enter the measurement in meters:");
scanf("%f", &m);
cm = m*100;
printf("\n Entered measurement in centimeters: %.2f", cm);
printf("\n\n");
}
2) Centimeters to Meters
#include<stdio.h>
void main()
{
float cm, m;
printf("\n Please enter the measurement in centimeters:");
scanf("%f", &cm);
m = cm/100;
printf("\n Entered measurement in meters: %.2f", m);
printf("\n\n");
}
3) Meters to Kilometers
#include<stdio.h>
void main()
{
float Km, m;
printf("\n Enter the measurement in meters:");
scanf("%f", &m);
Km = m / 1000;
printf("\n Entered measurement in Kilometers: %.2f", Km);
printf("\n\n");
}
4) Kilometers to meters
#include<stdio.h>
void main()
{
float Km, m;
printf("\n Enter the measurement in meters:");
scanf("%f", &m);
m = Km * 1000;
printf("\n Entered measurement in meters: %.2f", m);
printf("\n\n");
}
5) Grams to Kilograms
#include<stdio.h>
void main()
{
float Kg, g;
printf("\n Enter the weight in Grams :");
scanf("%f", &g);
Kg = g / 1000;
printf("\n Entered weight in kilograms %.2f", Kg);
printf("\n\n");
}
6) Kilograms to grams
#include<stdio.h>
void main()
{
float Kg, g;
printf("\n Enter the weight in Kilograms :");
scanf("%f", Kg&);
g = Kg * 1000;
printf("\n Entered weight in kilograms %.2f", g);
printf("\n\n");
}
7) Kilometer to miles
#include<stdio.h>
void main()
{
float km, mile;
printf("\n Please enter the measurement in kilometers:");
scanf("%f", &km);
mile = km/1.609;
printf("\n Entered measurement in miles: %.2f", mile);
printf("\n\n");
}
8) Miles to Kilometers
#include<stdio.h>
void main()
{
float km, mile;
printf("\n Please enter the measurement in miles:");
scanf("%f", &mile);
km = mile * 1.609;
printf("\n Entered measurement in kilometers: %.2f", km);
printf("\n\n"); }
9) Capital Letter to Small
#include<stdio.h>
void main()
{
char cap, small;
printf("\n Please enter the character in capital letter:");
scanf("%c", &cap);
printf("\n ASCII value of %c is %d", cap, cap);
small = cap + 32;
C = (F-32) * 5/9;
printf("\n The entered Temperature in Celsius %.2f", C);
printf("\n\n");
}
12) Celsius to Fahrenheit
#include<stdio.h>
void main()
{
float F, C;
printf("\nEnter the temperature in Celsius :");
scanf("%f", &C);
F = (C + 32) * 9/5;
printf("\n Th entered Temperature of %.2f is %.2f", C , F);
printf("\n\n");
}
if(num%2==0)
{
printf("\n Given number is Even");
}
else
{
printf("\n Given number is Odd");
}
printf("\n\n");
}
4) Check whether person is eligible to vote or not
#include<stdio.h>
void main()
{
int age;
printf("\n Please enter the age: ");
scanf("%d", &year);
if(year%4==0)
{
printf("\n Given year is Leap year");
}
else
{
printf("\n Given year is not a Leap year");
}
printf("\n\n");
}
Complex Decision Structures
1) Find grade of student based on the percentage
#include<stdio.h>
void main()
{
char grade;
float perc;
printf("\n Please enter your percentage: ");
scanf("%f", &perc);
if(perc > 85)
{
grade = 'A';
}
else if(perc > 70)
{
grade = 'B';
}
else if(perc > 55)
{
grade = 'C';
}
else if(perc > 40)
{
grade = 'D';
}
else if(perc > 32)
{
grade = 'E';
}
else
{
grade = 'F';
}
printf("\n Your Percentage: %.2f", perc);
printf("\n your grade: %c", grade);
printf("\n\n");
}
2) Find maximum number among given three numbers.
#include<stdio.h>
void main()
{
int a, b, c;
printf("\n Please enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if(a>b && a>c)
{
printf("%d is Maximum.", a);
}
else if(b>a && b>c)
{
printf("%d is Maximum.", b);
}
else
{
printf("%d is Maximum.", c);
}
printf("\n\n");
}
3) Calculate commission of salesman as per the criteria:
Sale > 20000 15%, >15000 10%, > 10000 5%, <=1000 nil
#include<stdio.h>
void main()
{
float sale;
float cmsn;
switch(choice)
{
case 1: printf("\n Addition of %d and %d is %d",num1,num2, (num1+num2));
break;
case 2: printf("\n Subtraction of %d from %d is %d", num2, num1, (num1-num2));
break;
case 3: printf("\n Multiplication of %d and %d is %d", num1, num2,
(num1*num2));
break;
case 4: printf("\n Division of %d by %d is %f",num1, num2,(num1/(float)num2));
break;
default: printf("\n You have entered invalid choice... please try again.
");
break;
}
printf("\n\n");
}
5) Check whether given number is prime or not
#include<stdio.h>
void main()
{
int num, i;
int prime=1;
printf("\nPlease enter the number:");
scanf("%d", &num);
for(i=2; i<=(num-1); i++)
{
if(num%i==0)
{
prime=0;
break;
}
}
if(prime==1)
{
printf("\n %d is a prime number", num);
}
else
{
printf("\n %d is not a prime number", num);
}
printf("\n\n"); }
Simple Loop Structures
1) Generate 1 to 10 horizontally.
#include<stdio.h>
void main()
{
int i;
printf("\n1 to 10 horizontally.\n\n");
for(i=1;i<=10;i++)
{
printf("%d \t",i);
}
printf("\n\n");
}
2) Generate 1 to 10 vertically.
#include<stdio.h>
void main()
{
int i;
printf("\n1 to 10 horizontally.\n\n");
for(i=1;i<=10;i++)
{
printf("%d \n",i);
}
printf("\n\n");
}
3) Generate table of any fixed number
#include<stdio.h>
void main()
{
int i;
printf("\n Table of 35\n");
for(i=1;i<=10;i++)
{
printf("\n\t 35 * %3d = %3d", i, 35*i);
}
printf("\n\n");
}
4) Generate Table of any entered number
#include<stdio.h>
void main()
{
int i, num;
printf("\n Please enter the number whose table you want:");
scanf("%d", &num);
for(i=1;i<=10;i++)
{
printf("\n\t %3d * %3d = %3d", num, i, num*i);
}
printf("\n\n");
}
5) Generate all Even numbers between 1 and 50
#include<stdio.h>
void main()
{
int i;
printf("\n All even numbers between 1 and 50");
for(i=1;i<=50;i++)
{
if(i%2==0)
{
printf("\n\t %3d",i);
}
}
printf("\n\n"); }
6) Generate all Odd numbers between 1 and 50
#include<stdio.h>
void main()
{
int i;
printf("\n All Odd numbers between 1 and 50");
for(i=1;i<=50;i++)
{
if(i%2!=0)
{
printf("\n\t %3d",i);
}
}
printf("\n\n");
}
7) Generate all Prime numbers between 1 and 100
#include<stdio.h>
void main()
{
int i,j;
int prime=1;
for(i=1;i<=100;i++)
{
int prime=1;
for(j=2; j<=(i/2); j++)
{
if(i%j==0)
{
prime=0;
break;
}
}
if(prime==1)
{
printf("\n\t %2d",i);
}
}
printf("\n\n");
}