A
Practical File
On
Programming in C Lab
(ESC-CSE103G)
Submitted
For
Bachelor of Technology
In
Computer Science & Engineering
At
Chaudhary Ranbir Singh State Institute of Engineering
and Technology, Silani Keso, Jhajjar (124103)
Submitted to: Submitted By:
INDEX
S.No. Practical Date Page Teacher
No. sign.
1
10
Program-1
Write a Program to calculate and display the volume of a Cuboid
having its height(h=10cm),width (w=12cm) and depth (8cm).
Solution:
#include<stdio.h>
int main(){
int height=10,width=12,depth=8;
int volume;
volume=height*width*depth;
printf("The volume of cuboid is %d\n", volume);
return 0;
}
Output:
The volume of Cuboid is 960 cmᶟ
Program-2
Write a program to calculate
(a)Simple interest
(b)Compound interest.
Solution:
(a)
#include<stdio.h>
int main(){
float principle,rate,time,SI;
printf("Enter principle:\n");
scanf("%f",&principle);
printf("Enter rate:\n");
scanf("%f",&rate);
printf("Enter time:\n");
scanf("%f",&time);
SI=(principle*rate*time)/100;
printf("Simple interest = %f", SI);
return 0;
}
Output:
Enter principle:
100000
Enter rate:
4
Enter time:
5
Simple interest = 20000.000000
(b)
#include<stdio.h>
#include<math.h>
int main(){
float principle,rate,time,compound_interest;
printf("Enter the principle:");
scanf("%f", &principle);
printf("Enter the rate:");
scanf("%f", &rate);
printf("Enter the time:");
scanf("%f", &time);
compound_interest = principle * pow((1 + rate/100), time);
printf("The compound interest is %f", compound_interest);
return 0;
}
Output:
Enter the principle:5000
Enter the rate:7.5
Enter the time:3
The compound interest is 6211.484375
Program-3
Write a program to print whether a given number is even or
odd.
Solution:
#include<stdio.h>
int main(){
int a;
printf("Enter a number:");
scanf("%d", &a);
if(a%2==0){
printf("%d is even\n", a);
}
else{
printf("%d is odd\n", a);
}
return 0;
}
Output:
(a)
Enter a number:52
52 is even
(b)
Enter a number:17
17 is odd
Program-4
Write a program to find whether a character is consonant or vowel
using switch statement.
Solution:
#include<stdio.h>
int main(){
char alphabet;
printf("Enter an alphabet:");
scanf("%c",&alphabet);
switch(alphabet)
{
case 'a':
printf("This is a vowel");
break;
case 'e':
printf("This is a vowel");
break;
case 'i':
printf("This is a vowel");
break;
case 'o':
printf("This is a vowel");
break;
case 'u':
printf("This is a vowel");
break;
default:
printf("This is a consonant");
}
return 0;
}
Output:
(a) Enter an
alphabet:y This is a
consonant
(b) Enter an alphabet:a
This is a vowel
Program-5
Write a program to compute grade of students using if else adder.
Solution:
#include<stdio.h>
int main(){
int marks;
printf("enter your marks:");
scanf("%d", &marks);
if (marks<=100 && marks>=90){
printf("your grade is A+\n");
}
else if(marks<=90 && marks>=80){
printf("your grade is A\n");
}
else if(marks<=80 && marks>=70){
printf("your grade is B+\n");
}
else if(marks<=70 && marks>=60){
printf("your grade is B\n");
}
else if(marks<=60 && marks>=50){
printf("your grade is C\n");
}
else{
printf("your grade is F\n");
}
return 0;
}
Output:
(a)enter your marks:99
your grade is A+
(b)enter your marks:87
your grade is A
(c) enter your marks:74
your grade is B+
(d)enter your marks:69
your grade is B
(e)enter your marks:52
your grade is C
(f)enter your marks:46
your grade is F
Program-6
Write a program to generate Fibonacci series.
Solution:
#include<stdio.h>
int main(){
int ele1 = 0, ele2 = 1, nextTerm, no_of_terms;
scanf("%d", &no_of_terms);
for (int i=0;i<no_of_terms;i++)
{
printf("%d", ele1);
nextTerm=ele1+ele2;
ele2=ele1;
ele1=nextTerm;
}
return 0;
}
Output:
(a)3
011
(b)5
01123
Program-7
Write a program to find the factorial of a number using recursion.
Solution:
#include <stdio.h>
int factorial(int x);
int main()
{
int a;
printf("Enter the value of a\n");
scanf("%d", &a);
printf("The value of factorial %d is %d", a, factorial(a));
return 0;
}
int factorial(int x){
int f;
if (x == 0 || x == 1)
return 1;
else
f = x* factorial(x-1);
return f;
}
Output:
(a)Enter the value of a
The value of factorial 6 is 720
(b) Enter the value of a
The value of factorial 4 is 24
Program-8
Write a program to check whether a program is Palindrome or not.
Solution:
#include<stdio.h>
int main(){
int n, reversedN=0, remainder, originalN;
printf("Enter an integer:");
scanf("%d", &n);
originalN=n;
while(n!=0){
remainder = n % 10;
reversedN = reversedN*10+remainder;
n/=10;
}
if(originalN == reversedN)
printf("%d is palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);
return 0;
}
Output:
(a)Enter an integer:878
878 is palindrome.
(b) Enter an integer:569
569 is not a
palindrome.
Program-9
Write a program to swap value of two variables using pointer.
Solution:
#include<stdio.h>
void swap(int *a, int *b);
int main(){
int x = 8, y = 13;
printf("The value of x and y before swap is %d and %d\n", x , y);
swap(&x, &y);
printf("The value of x and y after swap is %d and %d\n", x , y);
return 0;
}
void swap(int *a, int*b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Output:
The value of x and y before swap is 8 and 13
The value of x and y after swap is 13 and 8
Program-10
Write a program to store information (name, roll no.,
marks) of 5 students in structure and display it.
Solution:
#include<stdio.h>
struct student{
char firstName[50];
int rollNo;
float marks;
}s[10];
int main(){
int i;
printf("Enter information of students:\n");
for (i = 0;i<5;++i){
s[i].rollNo = i + 1;
printf("\nFor roll number%d,\n", s[i].rollNo);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
for (i = 0;i<5;++i){
printf("\nRoll number: %d\n", i+1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output:
Enter information of students:
For roll number1,
Enter first name: Rajan
Enter marks: 96
For roll number2,
Enter first name: Arun
Enter marks: 94
For roll number3,
Enter first name: Ayush
Enter marks: 90
For roll number4,
Enter first name: Kunal
Enter marks: 99
For roll number5,
Enter first name: Rajat
Enter marks: 98
Displaying Information:
Roll number: 1
First name: Rajan
Marks: 96
Roll number: 2
First name: Arun
Marks: 94
Roll number: 3
First name: Ayush
Marks: 90.0
Roll number: 4
First name: Kunal
Marks: 99.0
Roll number: 5
First name: Rajat
Marks: 98.0