LuvBhatia 2310990477 G6
LuvBhatia 2310990477 G6
Practical File
Of
Problem Solving using
C Programming
23CS003
Submitted
in partial fulfillment for the award of the degree
of
BACHELEOR OF ENGINEERING
in
CHITKARA UNIVERSITY
June, 2023
INDEX
Program 1:
Write a Program to show the use to input (Scanf)/output (Printf)
statements and block structure of C-program by highlighting the features
of "stdio.h
Solution:
#include <stdio.h>
int main()
{ int x;
scanf("%d",&x);
printf("the number that you entered is: %d",x);
}
Output:
Program 2:
Write a program to add two numbers and display the sum.
Solution:
int main()
{ int x,y;
printf("enter the numbers");
scanf("%d %d",&x,&y); printf("%d
+ %d = %d",x,y,x+y);
}
Output:
Program 3:
Write a program to calculate the area and the circumference of a circle
by using radius as the input provided by the user
Solution:
#include <stdio.h>
int main(){
int r;
printf("enter the radius of the circle: ");
scanf("%d",&r); int cir=2*(22/7)*r; int
area=(22/7)*(r*r); printf("the area of the circle is:
%d\n",area); printf("the circumference of the
circle is: %d",cir);
}
Output:
Program 4:
Write a Program to perform addition, subtraction, division and
multiplication of two numbers given as input by the user.
Solution:
#include <stdio.h>
int main(){
int x;
int y;
printf("enter the first number: ");
scanf("%d",&x);
printf("enter the second number: ");
scanf("%d",&y); printf("%d + %d
= %d\n",x,x,x+y); printf("%d * %d
= %d\n",x,y,x*y); printf("%d / %d
= %d\n",x,y,x/y); printf("%d - %d
= %d",x,y,x-y);
}
Output:
Program 5a:
Write a program to evaluate each of the following equations.
V = u + at.
Solution:
#include <stdio.h>
scanf("%d %d %d",&u,&a,&t);
int v=u+(a*t);
printf("%d=%d+(%d*%d)",v,u,a,t);
}
Output:
Program 5b:
S = ut+1/2at^2
Solution :
#include <stdio.h>
int main()
{ int u,t,a;
printf("enter u,a,t: ");
scanf("%d %d %d",&u,&a,&t);
int s=(u*t)+(1/2*a*t*t);
printf("%d=%d*%d + 1/2*%d*%d^2",s,u,t,a,t);
Output :
Program 5c:
T=2*a+√b+9c
Solution:
#include <stdio.h>
#include <math.h>
Output:
Program 5d:
d. H=(p^2 + b^2)^(1/2)
Solution :
#include <stdio.h>
#include <math.h>
int main(){ int
b,p;
printf("enter p,b: ");
scanf("%d %d",&p,&b);
int h=sqrt((b*b)+(p*p));
printf("%d = (%d^2 + %d^2)^(1/2)",h,b,p);
}
Output:
Program 6a:
Write a program to swap two variable: a)
By using temporary variable
Solution : #include
<stdio.h> int
main(){
int a;
int b;
int t;
scanf("%d %d",&a,&b);
t=a;
a=b;
b=t;
printf("a is %d\nb is %d",a,b);
}
Output:
Program 6b:
Write a program to swap two variable: b)
without using temp
Solution : #include
<stdio.h> int
main(){
int a,b;
scanf("%d %d",&a,&b);
a=a+b; b=a-b; a=a-b;
printf("a is %d\nb in %d",a,b);
}
Output:
Program 7a:
Write a Program to find the greatest among three numbers using:
a) Conditional Operator
Solution :
#include <stdio.h>
int main(){ int
a,b,c;
scanf("%d %d %d",&a,&b,&c);
int r; r=(a>b)?(a>c?a:c):(b>c?
b:c);
printf("the greatest integer is: %d",r);
}
Output :
Program 7b:
Write a Program to find the greatest among three numbers using:
b) using if-else
Solution:
#include <stdio.h>
int main(){ int
a,b,c;
scanf("%d %d %d",&a,&b,&c);
int r;
if(a>b){
if(a>c){
r=a; }
else{
r=c;
} }
else{ if(b>
c){ r=b; }
else{
r=c;
}
}
printf("the greatest integer is: %d",r);
}
Output:
Program 8a:
Write the following programs using switch case statement:
a) To check that an input alphabet is vowel or consonant
Solution : #include
<stdio.h> int
main(){
char x;
scanf("%c",&x);
int a=0; switch
(x) { case
'a': a++;
break; case
'e': a++;
break; case 'i':
a++; break;
Output:
Program 8b:
Write the following programs using switch case statement:
b) To check whether a number is positive, negative or zero
Solution : #include
<stdio.h>
int main(){
int a;
scanf("%d",&a); switch(a){
case -100000 ... -1: printf("the
number is negative");
break; case 1 ... 1000000:
printf("the number is positive");
break; default:
printf("the number is 0");
break;
}
}
Output: