C Program
C Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p,t,r,si;
clrscr();
printf(“Enter the principal amount:”);
scanf(“%f”,&p);
printf(“Enter time:”);
scanf(“%f”,&t);
printf(“Enter rate of interest:”); scanf(“%f”,&r);
si=(p*t*r)/100;
printf(“Simple interest=%f ”,si);
getch();
}
OUTPUT
Enter the principal amount: 1200
Enter time: 2
Enter rate: 2
Simple Interest: 48.000
OUTPUT
Input first number: 2
Input second number: 3
Values before swap
Value of first number=2
Value of second number=3
Values after swap
Value of first number=2
Value of second number=3
3) C program to swap two numbers without third variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b; clrscr();
printf(“Input first number:”);
scanf(“%d”,&a);
printf(“Input second number:”);
scanf(“%d”,&b);
printf(“Values before swap \n”);
printf(“Value of first number=%d \n”,a);
printf(“Value of second number=%d \n”,b);
a=a+b;
b=a-b;
a=a-b;
printf(“Values after swap \n”);
printf(“Value of first number=%d \n”,a);
printf(“Value of second number=%d\n”,b);
getch();
}
OUTPUT
Input first number: 2
Input second number: 3
Values before swap
Value of first number=2
Value of second number=3
Values after swap
Value of first number=2
Value of second number=3
OUTPUT
Enter first side: 10
Enter second side: 10
Enter third side: 10
Area of triangle= 43.3012
5) C program to find area and perimeter of a rectangle
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float l,b,area,perimeter;
clrscr();
printf(“Enter the length:”); scanf(“%f”,&1);
printf(Enter the breadth:”); scanf(“%f”,&b);
area=1*b;
perimeter=2*(1+b);
printf(“Area of rectangle=%f \n”,area);
printf(“Perimeter of rectangle=%f \n”,perimeter);
getch();
}
OUTPUT
Enter the length: 3
Enter the breadth: 2
Area of rectangle: 6.000
Perimeter of rectangle: 10.000
Simple if
OUTPUT
Enter first number: 3
Enter second number: 13
Enter third number: 7
First number=3
Second number=13
Third number=7
Largest=13
Second largest=7
Smallest=3
2) C program to find largest of three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
double n1,n2,n3; clrscr();
printf(“Enter first number:”);
scanf(“%lf”,&n1);
printf(“Enter second number:”);
scanf(“%lf”,&n2);
printf(“Enter third number:”);
scanf(“%lf”,n3);
if(n1>=n2 && n1>=n3)
printf(“%lf is the largest number”,n1);
if(n2>=n1 && n2>=n3)
printf(“%lf is the largest number”,n2);
if(n3>=n1 && n3>=n2)
printf(“%lf is the largest number”,n3); getch();
}
OUTPUT
Enter first number: 3
Enter second number: 13
Enter third number: 7
13 is the largest number
if else / if else if
OUTPUT
Enter an integer: 15
15 is odd
OUTPUT
Enter age: 19
Eligible to vote
OUTPUT
Enter two integers: 5 10
5<10
OUTPUT
Input an alphabet: A
It is an upper case
for statement
OUTPUT
1
2
3
4
5
6
7
8
9
10
OUTPUT
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
OUTPUT
Enter a positive integer: 5
Sum=15
5) C program to find the factorial of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1; clrscr();
printf(“Enter a number :”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“Factorial of %d =%d”,n,fact);
getch();
}
OUTPUT
Enter a number: 4
Factorial of 4 = 24