100% found this document useful (2 votes)
41 views

C Program

The document contains 7 code snippets in C programming language that demonstrate various programming concepts: 1. The first snippet calculates simple interest given principal, time, and interest rate. 2. The second snippet swaps two numbers using a third variable. 3. The third snippet swaps two numbers without using a third variable. 4. Further snippets demonstrate finding the area of a triangle, rectangle, and circle as well as converting days to years/months/days. The snippets cover basic programming constructs like input/output, arithmetic operations, conditional statements, loops and functions. Each snippet is followed by sample input/output to demonstrate its functionality.

Uploaded by

Muddassir kammar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
41 views

C Program

The document contains 7 code snippets in C programming language that demonstrate various programming concepts: 1. The first snippet calculates simple interest given principal, time, and interest rate. 2. The second snippet swaps two numbers using a third variable. 3. The third snippet swaps two numbers without using a third variable. 4. Further snippets demonstrate finding the area of a triangle, rectangle, and circle as well as converting days to years/months/days. The snippets cover basic programming constructs like input/output, arithmetic operations, conditional statements, loops and functions. Each snippet is followed by sample input/output to demonstrate its functionality.

Uploaded by

Muddassir kammar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1) C program to calculate simple interest

#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

2) C program to swap two numbers using third variable


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,t;
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);
t=a;
a=b; b=t;
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
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

4) C program to calculate area of triangle given 3 sides


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,s,area; clrscr();
printf(“Enter first side:”); scanf(“%f”,&a);
printf(“Enter second side:”);
scanf(“%f”,&b);
printf(“Enter third side:”); scanf(“%f”,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“Area of triangle=%f”,area);
getch();
}

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

6) C Program to find area and Circumference of a circle


#include<stdio.h>
#include<conio.h>
{
float r,area,circum;
float const pi=3.142; clrscr();
printf(“Enter the radius of circle:”);
scanf(“%f”,&r);
area=pi*r*r;
circum=2*pi*r;
printf (“Area of circle=%f \n”,area);
printf (“Circumference of circle=%f \n”,circum);
getch();
}
OUTPUT
Enter the radius of circle: 10
Area of circle=314.2
Circumference of circle=62.84

7) C Program to convert days into years, months and days


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int tot,ye,mon,days,rem:
clrscr();
printf(“enter total days”);
scanf(“%d”,&tot);
ye=tot/365;
rem=tot%365;
mon=rem/30;
days=rem%30;
printf(“Total days= %d \n”,tot);
printf(“Years=%d \n”,ye);
printf(“Months=%d \n”,mon);
printf(“Days=%d \n”,days);
getch();
}
OUTPUT
Enter total days: 7
Total days=7
Months=0
Days=7

Simple if

1) C program to find largest, smallest and second largest of three numbers.


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sm,la,sl;
clrscr();
printf(“Enter first number:”);
scanf(“%d”,&a);
printf(“Enter second number:”);
scanf(“%d”,&b);
printf(“Enter third number:”);
scanf(“%d”,&c);
la=a; if(b>la)
la=b; if(c>la)
la=c; sm=a;
if(b<m)
sm=b; if(c<sm)
sm=c;
sl=(a+b+c)-(la+sm);
printf(“First number=%d \n”,a);
printf(“Second number=%d \n”,b);
printf(“Third number=%d \n”,c);
printf(“Largest=%d \n”,la);
printf(“Second largest=%d \n”,sl);
printf(“Smallest=%d \n”,sm);
getch();
}

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

1) C program to check whether a number is even or odd


#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“Enter an integer:”); scanf(“%d”,&n);
if(n%2==0)
printf(“%d is even”,n);
else
printf(“%d is odd”,n);
getch();
}

OUTPUT
Enter an integer: 15
15 is odd

2) C program to check whether a number is prime or not


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0;
clrscr();
printf(“Enter a number:”); scanf(“%d”,&n);
for(i=1;i<=n;i++)
if((n%i)==0)
c++;
if(c==2)
printf(“%d is a prime number”,n);
else
printf(“%d is not a prime number”,n);
getch();
}
OUTPUT
Enter a number: 3
3 is a prime number

3) C program to check whether a person is eligible to vote or not


#include<stdio.h>
#include<conio.h>
void main()
{
int age; clrscr();
printf(“Enter age:”);
scanf(“%d”,&age); if(age>=18)
printf(“Eligible to vote”);
else
printf(“Not eligible to vote”);
getch();
}

OUTPUT
Enter age: 19
Eligible to vote

4) C program to relate two integers using = or > or < symbol


#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2; clrscr();
printf(“Enter two integers :”); scanf(“%d,%d”,&n1,&n2);
if(n1==n2)
printf(“Results : %d=%d”,n1,n2);
else if(n1>n2)
printf(“Results : %d>%d”,n1,n2);
else
printf(“Results : :%d<%d”,n1,n2);
getch();
}

OUTPUT
Enter two integers: 5 10
5<10

5) C program to check whether a character is lower or upper case


#include<stdio.h>
#include<conio.h>
void main()
{
char ch; clrscr();
printf(“Input an alphabet :”);
scanf(“%c”,&ch);
if((ch>='A')&&(ch<='Z'))
{
printf(“It is a upper case \n”);
}
else if((ch>='a')&&(ch<='z'))
{
printf(“It is a lower case \n”);
}
else if((ch>='0')&&(ch<='9'))
{
printf(“It is a number” \”);
}
else
{
printf(“It is a special character”);
}
getch();
}

OUTPUT
Input an alphabet: A
It is an upper case

for statement

1) C program to print number from 1 to 10


#include<stdio.h>
#include<conio.h.
void main()
{
int i; clrscr();
for(i=1;i<11;i++)
printf(“%d\n”,i);
getch();
}

OUTPUT
1
2
3
4
5
6
7
8
9
10

2) C program to display characters from A to Z


#include<stdio.h>
#include<conio.h>
void main()
{
char c; clrscr();
for(c='A';c<='Z';++c)
printf(“%c\n”,c);
getch();
}

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

3) C program to generate multiplication table


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i; clrscr();
printf(“Enter a number :”);
scanf(“%d”,&n);
for(i=1;i<=10;++i)
{
printf(“%d*%d=%d \n”,n,i,n*i);
}
getch();
}
OUTPUT
Enter a number: 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

4) C program to calculate the sum of first n natural numbers


#include<stdio.h>
#include<conio.h>
void main()
{
int n,c,sum=0; clrscr();
printf(“Enter a positive integer:”);
scanf(“%d”,&n);
for(c=1;c<=n;++c)
{
sum=sum+c;
}
printf(“Sum=%d”,sum);
getch();
}

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

You might also like