Lab Record Download 2
Lab Record Download 2
Page No: 1
Aim:
Write a program that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by
the student.
ID: 2302221530011
Formula(s) used:
Percentage= (sum/500)*100
Algorithm:
Step 1- Start.
Step 2- Declaration of variables.
Step 3- Read the marks of 5 subjects and store it to respective variables.
Step 4- Add all 5 variables that contain marks of subjects and store them to a new variable
called sum.
Step 5- Print Total marks (sum).
Step 6- Percentage per=(sum/500)*100. (Assuming Maximum marks of each subject is 100)
Step 7- Print Percentage (per).
2023-27-F1
Step 8- Stop.
Flowchart:
Source Code:
sum-percentage.c
#include<stdio.h>
int main()
{
float a,b,c,d,e,sum,percentage;
printf("Enter marks obtained in Maths: ");
Page No: 2
scanf("%f",&a);
printf("Enter marks obtained in Physics: ");
scanf("%f",&b);
printf("Enter marks obtained in Chemistry: ");
scanf("%f",&c);
ID: 2302221530011
printf("Enter marks obtained in Computer: ");
scanf("%f",&d);
printf("Enter marks obtained in Mechanics: ");
scanf("%f",&e);
sum=a+b+c+d+e;
percentage=(sum/500)*100;
printf("Total marks = %0.0f",sum);
printf("\nPercentage = %.2f",percentage);
return 0;
}
2023-27-F1
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
User Output
Enter marks obtained in Maths:
72
Enter marks obtained in Physics:
67
Enter marks obtained in Chemistry:
54
Enter marks obtained in Computer:
77
Enter marks obtained in Mechanics:
84
Total marks = 354
Percentage = 70.80
Page No: 4
Aim:
Write a program that calculates the Simple Interest and Compound Interest. The Principal, Amount, Rate of
Interest and Time are entered through the keyboard.
ID: 2302221530011
Formula(s) used:
Algorithm:
Step 1- Start.
Step 2- Read the value of principal amount, Rate of interest and Time.
Step 3- Calculate Simple Interest SI = (Principal × Rate × Time) / 100.
Step 4- Calculate Compound Interest CI = Principal (1 + Rate/100) Time− Principal
Step 5- Print Simple interest SI and Compound interest CI.
2023-27-F1
Step 6- Stop.
Flowchart:
Source Code:
simple-compound.c
#include<stdio.h>
#include<math.h>
int main()
{
float p,r,t,ci,si;
Page No: 5
printf("Enter principal amount: ");
scanf("%f",&p);
printf("Enter time in year: ");
scanf("%f",&t);
printf("Enter rate in percent: ");
ID: 2302221530011
scanf("%f",&r);
si = (p*r*t)/100;
ci = p*(pow(1+r/100,t)-1);
printf("Simple Interest = %.2f" ,si);
printf("\nCompound Interest = %.2f",ci);
return 0;
2023-27-F1
Test Case - 1
User Output
Test Case - 2
User Output
Enter principal amount:
1000
Enter time in year:
3
Enter rate in percent:
12
Simple Interest = 360.00
Compound Interest = 404.93
Exp. Name: Write a program to calculate the area
S.No: 3 Date: 2024-05-06
and circumference of a circle.
Aim:
Page No: 6
Write a program to calculate the area and circumference of a circle.
Formula(s) used:
Area of Circle A= Pi x r x r
ID: 2302221530011
Circumference of circle C= 2 X Pi X r
Algorithm:
Step 1- Start.
Step 2- Read the value of radius r of Circle.
Step 3- Give Pi = 3.14.
Step 4- Calculate area of Circle A= Pi x r x r.
Step 5- Calculate Circumference of circle C= 2 X Pi X r.
Step 6- Print area A and Circumference C.
Step 7- Stop.
2023-27-F1
Flowchart:
areaC.c
#include<stdio.h>
#define PI 3.14
int main()
{
float r,a,cir;
printf("Enter radius = ");
scanf("%f", &r);
a=PI*r*r;
cir=2*PI*r;
printf("Area of Circle = %.2f",a);
printf("\nCircumference of Circle = %.2f\n" ,cir);
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 7
Enter radius =
5
Area of Circle = 78.50
Circumference of Circle = 31.40
ID: 2302221530011
Test Case - 2
User Output
Enter radius =
6.5
Area of Circle = 132.66
Circumference of Circle = 40.82
2023-27-F1
ITS Engineering College
Exp. Name: Write a program that accepts the
S.No: 4 temperature in Centigrade and converts into Date: 2024-05-06
Fahrenheit using the formula C/5=(F-32)/9.
Page No: 8
Aim:
Write a program that accepts the temperature in Centigrade and converts into Fahrenheit using the formula
C/5=(F-32)/9.
Formula(s) used:
ID: 2302221530011
F=(9 x C) / 5 + 32
Algorithm:
Step 1- Start.
Step 2- Read the input of temperature in Centigrade (C).
Step 3- Apply the formula F=(9 x C) / 5 + 32.
Step 4- Print the calculated temperature in Fahrenheit.
Step 5- Stop.
Flowchart:
temp.c
#include<stdio.h>
int main ()
{
float F,C;
printf("Enter temperature in Centigrade standard = ");
scanf("%f",&C);
F=(9*C)/5+32;
printf("Temperature in Fahrenheit = %0.2f\n",F);
return 0;
}
Page No: 9
Test Case - 2
User Output
ID: 2302221530011
Enter temperature in Centigrade standard =
65
Temperature in Fahrenheit = 149.00
2023-27-F1
ITS Engineering College
Exp. Name: Write a C program to swap the values
S.No: 5 Date: 2024-05-06
of two variables using a third variable.
Aim:
Page No: 10
Write a C program to swap the values of two variables using a third variable.
Formula(s) used:
Temp = Num1
ID: 2302221530011
Num1 = Num2
Num2 = Temp
Algorithm:
Step 1- Start.
Step 2- Read the values of both the numbers (i.e., Num1, Num2).
Step 3- (a) Assign value of first variable to a temporary variable (i.e., Temp = Num1),
(b) Assign value of second variable to the first variable (i.e., Num1 = Num2),
(c) Assign value of temporary variable to the second variable (i.e., Num2 = Temp).
Step 4- Print the values of both the numbers after swapping (i.e., Num1 and Num2).
Step 5- Stop.
2023-27-F1
Flowchart:
Source Code:
swap.c
#include<stdio.h>
int main()
{
int Num1,Num2,Temp;
printf("Enter two numbers = ");
scanf("%d%d",&Num1,&Num2);
Temp = Num1;
Num1 = Num2;
Num2 = Temp;
printf("After swapping Num1 = %d , Num2 = %d\n",Num1 , Num2);
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 11
Enter two numbers =
10 20
After swapping Num1 = 20 , Num2 = 10
ID: 2302221530011
Test Case - 2
User Output
Enter two numbers =
57
After swapping Num1 = 7 , Num2 = 5
2023-27-F1
ITS Engineering College
Exp. Name: Write a program that checks whether
S.No: 6 the two numbers entered by the user are equal Date: 2024-05-06
or not.
Page No: 12
Aim:
Write a program that checks whether the two numbers entered by the user are equal or not.
Algorithm:
ID: 2302221530011
Step 1- Start.
Step 2- Read two numbers (i.e., Num1, Num2).
Step 3- (a) Check if, Num1 == Num2
Print “Both the numbers are equal”
(b) else
Print “Both the numbers are not equal”
Step 4- Stop.
2023-27-F1
ITS Engineering College
Source Code:
same_or_not.c
#include<stdio.h>
int main ()
{
int Num1,Num2;
printf("Enter two numbers = ");
scanf("%d%d",&Num1,&Num2);
if (Num1==Num2)
printf("Both the numbers are equal\n");
else
printf("Both the numbers are not equal\n");
return 0;
}
User Output
Enter two numbers =
44
Both the numbers are equal
Test Case - 2
Page No: 13
User Output
Enter two numbers =
10 50
Both the numbers are not equal
ID: 2302221530011
2023-27-F1
ITS Engineering College
Exp. Name: Write a program to find the greatest
S.No: 7 Date: 2024-05-06
of three numbers.
Aim:
Page No: 14
Write a program to find the greatest of three numbers.
Algorithm:
Step 1- Start.
ID: 2302221530011
Step 2- Read the values of all the three numbers (i.e., Num1, Num2, Num3).
Step 3- (a) Check if, Num1 > Num2 and Num1> Num3
Print “Num1 is the greatest”.
(b) Check if, Num2 > Num1 and Num2> Num3
Print “Num2 is the greatest”.
(c) Otherwise, Print “Num3 is the greatest”.
Step 4- Stop.
Flowchart:
2023-27-F1
ITS Engineering College
Source Code:
greatestNumber.c
#include<stdio.h>
int main()
{
int Num1, Num2, Num3;
printf("Enter three numbers= ");
scanf("%d %d %d",&Num1,&Num2,&Num3);
if
(Num1>Num2 && Num1>Num3)
printf("%d is the greatest number\n",Num1);
if(Num2>Num1 && Num2>Num3)
printf("%d is the greatest number\n",Num2);
if
(Num3>Num2 && Num3>Num1)
printf("%d is the greatest number\n",Num3);
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 15
Enter three numbers=
345
5 is the greatest number
ID: 2302221530011
Test Case - 2
User Output
Enter three numbers=
10 50 30
50 is the greatest number
2023-27-F1
ITS Engineering College
Exp. Name: Write a program that finds whether a
S.No: 8 Date: 2024-05-06
given number is even or odd.
Aim:
Page No: 16
Write a program that finds whether a given number is even or odd.
Algorithm:
Step 1- Start.
ID: 2302221530011
Step 2- Read the values of the number.
Step 3- (a) Check if, after dividing the number by 2, remainder is 0
Print “Even number”.
(b) Otherwise, Print “Odd number”.
Step 4- Stop.
Flowchart:
2023-27-F1
ITS Engineering College
Source Code:
evenOdd.c
#include<stdio.h>
int main()
{
int num;
printf("Enter number: ");
scanf("%d",&num);
if (num%2==0)
printf("The given number is EVEN\n");
else
printf("The given number is ODD\n");
return 0;
User Output
Enter number:
31
The given number is ODD
Test Case - 2
Page No: 17
User Output
Enter number:
20
The given number is EVEN
ID: 2302221530011
2023-27-F1
ITS Engineering College
Exp. Name: Write a program that tells whether a
S.No: 9 Date: 2024-05-16
given year is a leap year or not.
Aim:
Page No: 18
Write a program that tells whether a given year is a leap year or not.
Algorithm:
Step 1- Start.
Step 2- Read the year.
ID: 2302221530011
Step 3- (a) Check if, the given year is completely divisible by 400 or it is completely divisible by4 but not
completely divisible by 100, Print “Leap Year”.
(b) Otherwise, Print “Not a leap year”. Step 4- Stop.
Flowchart:
2023-27-F1
ITS Engineering College
Source Code:
leapYear.c
#include<stdio.h>
int main()
{
int year;
printf("enter year :");
scanf("%d", &year);
if
(year%400==0)
printf("%d is a leap year\n",year);
else if (year%100==0)
printf("%d is not a leap year\n",year);
else if (year%4==0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
return 0;
}
Page No: 19
Test Case - 2
User Output
enter year :
ID: 2302221530011
1900
1900 is not a leap year
2023-27-F1
ITS Engineering College
Exp. Name: Write a program that accepts marks
S.No: 10 of five subjects and finds percentage and prints Date: 2024-06-10
grades.
Page No: 20
Aim:
Write a program that accepts marks of five subjects and finds percentage and prints grades.
according to the following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
ID: 2302221530011
60-80%-----------------Print ‘C’
Below 60%-------------Print ‘D’
Source Code:
grade.c
#include<stdio.h>
#include<math.h>
int main ()
{
float a,b,c,d,e;
float per;
2023-27-F1
printf("Enter five subjects marks: ");
scanf("%f%f%f%f%f",&a,&b,&c,&d,&e);
per=(a+b+c+d+e)/5;
printf("Percentage = %0.6f\n",per);
if
(per>=90&&per<=100)
User Output
Enter five subjects marks:
40 50 60 70 80
Percentage = 60.000000
Grade C
Test Case - 2
User Output
Enter five subjects marks:
67 56 45 34 23
Percentage = 45.000000
Grade D
Page No: 21
Test Case - 3
User Output
ID: 2302221530011
Enter five subjects marks:
78.5 92.5 85 77 80
Percentage = 82.599998
Grade B
2023-27-F1
ITS Engineering College
Exp. Name: Perform the operation using Switch
S.No: 11 Date: 2024-06-10
statement
Aim:
Page No: 22
Write a C program that takes two operands and one operator from the user, perform the operation, and
prints the result by using Switch statement.
Source Code:
switch.c
ID: 2302221530011
#include<stdio.h>
int main (){
char op;
int a,b,c;
printf("Enter your operator:");
scanf("%c",&op);
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
switch(op)
{
case'+': c=a+b;
printf("addition of two numbers is %d",c);
2023-27-F1
break;
case'-': c=a-b;
printf("subtraction of two numbers is %d",c);
break;
case'*': c=a*b;
User Output
Enter your operator:
%
Enter the values of a and b
10 5
Remainder of two numbers is 0
Test Case - 2
User Output
Enter your operator:
+
Enter the values of a and b
4 77
addition of two numbers is 81
Page No: 23
Test Case - 3
User Output
ID: 2302221530011
Enter your operator:
-
Enter the values of a and b
45 6
subtraction of two numbers is 39
Test Case - 4
User Output
Enter your operator:
/
2023-27-F1
Enter the values of a and b
10 5
Quotient of two numbers is 2
User Output
Enter your operator:
*
Enter the values of a and b
12 11
multiplication of two numbers is 132
Exp. Name: Write the code to calculate the sum
S.No: 12 Date: 2024-06-10
of first n natural numbers
Aim:
Page No: 24
Write a C program to calculate the sum of first n natural numbers and Positive integers 1,2,3 ... n are known
as natural numbers.
Source Code:
sum.c
ID: 2302221530011
#include<stdio.h>
int main ()
{
int n,c=0,i;
scanf("%d",&n);
for (i=1;
i<=n;
++i)
{
c +=i;
}
printf("Sum: %d" ,c);
2023-27-F1
return 0;
}
User Output
10
Sum: 55
Test Case - 2
User Output
66
Sum: 2211
S.No: 13 Exp. Name: Find the factorial of a given number. Date: 2024-06-30
Aim:
Write a C program to find the factorial of a given number.
Page No: 25
Source Code:
factorial.c
#include<stdio.h>
int main()
ID: 2302221530011
{
int n,i;
unsigned long long fact =1;
printf("Enter an integer: ");
scanf("%d",&n);
if(n<0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1;
i<=n;
++i)
2023-27-F1
{
fact*=i;}
printf("Factorial of %d = %llu",n,fact);
}
return 0;
User Output
Enter an integer:
12
Factorial of 12 = 479001600
Test Case - 2
User Output
Enter an integer:
-1
Error! Factorial of a negative number doesn't exist.
Exp. Name: Print sum of even and odd numbers
S.No: 14 Date: 2024-06-30
from 1 to N numbers.
Aim:
Page No: 26
Write a C program to print sum of even and odd numbers from 1 to N numbers.
Source Code:
sumOfEvenandOdd.c
ID: 2302221530011
#include<stdio.h>
int main()
{
int n,i,even,odd;
printf("Enter the value of num\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if
(i%2==0)
even=even+i;
else
odd=odd+i;
2023-27-F1
}
printf("Sum of all odd numbers = %d\n",odd);
printf("Sum of all even numbers = %d\n",even);
}
User Output
Enter the value of num
6
Sum of all odd numbers = 9
Sum of all even numbers = 12
Test Case - 2
User Output
Enter the value of num
1
Sum of all odd numbers = 1
Sum of all even numbers = 0
Exp. Name: Write a program to display Fibonacci
S.No: 15 Date: 2024-06-30
sequence up to N terms.
Aim:
Page No: 27
Write a program to display Fibonacci sequence up to N terms. Follow the given instructions while writing
the program.
VALID INPUT:
• Only one positive Integer (greater than 0) will be given as input.
ID: 2302221530011
INVALID INPUTS:
• -5
• No command line argument
• String
• Two or more command line arguments
OUTPUT:
• Print only the terms separated by a space to the STDOUT without any other additional text.
• In case of invalid input print 'ERROR' to the STDOUT without any other additional text and terminate.
For example:
2023-27-F1
The output should be:
0 1 1 2 3 5 8
Source Code:
#include<stdio.h>
int main()
{
int N,i,a=0,b=1,c;
printf("");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("%d ",a);
c=a+b;
a=b;
b=c;
}
}
User Output
7
0 1 1 2 3 5 8
6
0 1 1 2 3 5
User Output
Test Case - 2
Aim:
Page No: 29
Write a program to check whether the given number is a prime number or not.
[Hint: A prime number is a positive integer which is greater than 1 and divisible by 1 and itself only. A few
prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, etc.]
Source Code:
ID: 2302221530011
Program409.c
#include<stdio.h>
void main()
{
int a,b,d=0,i;
scanf("%d",&a);
for(i=2;i<a;i++)
{
if(a%i==0)
{
d=1;
2023-27-F1
break;
}
}
if(d==0)
{
User Output
11
Prime number
Test Case - 2
User Output
24
Not a prime number
Exp. Name: Write a program to find the sum of
S.No: 17 Date: 2024-06-30
digits of the entered number.
Aim:
Page No: 30
Write a program to find the sum of digits of the entered number.
Source Code:
sumOfdigit.c
#include<stdio.h>
ID: 2302221530011
void main()
{
int n,r,s=0;
printf("Enter a Number: ");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s=s+r;
n/=10;
}
printf("Sum of Digits of Number: %d",s);
2023-27-F1
}
User Output
Enter a Number:
30
Sum of Digits of Number: 3
Test Case - 2
User Output
Enter a Number:
7
Sum of Digits of Number: 7
Exp. Name: Write a C program to find the reverse
S.No: 18 Date: 2024-06-30
of the given number
Aim:
Page No: 31
Design a C program which reverses the given number.
Source Code:
Program407.c
ID: 2302221530011
#include<stdio.h>
int main()
{
int n,i=0;
scanf("%d",&n);
while(n>0)
{
int d=n%10;
i=i*10+d;
n=n/10;
}
printf("Reversed number= %d",i);
}
2023-27-F1
Execution Results - All test cases have succeeded!
User Output
456
Reversed number= 654
Test Case - 2
User Output
958745
Reversed number= 547859
Exp. Name: Write a C program to check
S.No: 19 Date: 2024-06-30
armstrong number
Aim:
Page No: 32
Aim: Write a program to check number entered by the user is an Armstrong number or not.
Theory:In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the
number itself. For example, 153 is an Armstrong number because
153 = 1*1*1 + 5*5*5 + 3*3*3
ID: 2302221530011
Algorithm:
1. Input the number.
2. Initialize sum=0 and temp=number.
3. Find the total number of digits in the number.
4. Repeat until (temp != 0)
5. remainder = temp % 10
6. result = result + pow(remainder,n)
7. temp = temp/10
8. if (result == number)
2023-27-F1
9. Display "Armstrong"
10. Else
11. Display "Not Armstrong"
Source Code:
#include<stdio.h>
#include<math.h>
void main()
{
int n,r,m,s=0,d=0;
scanf("%d",&n);
m=n;
while(n>0)
{
n/=10;
d++;
}
n=m;
while(n>0)
{
r=n%10;
s= s+ pow(r,d);
n/=10;
}
if (m==s)
{
printf("Armstrong");
}
else{
printf("Not an Armstrong");
}
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 33
153
Armstrong
Test Case - 2
ID: 2302221530011
User Output
354
Not an Armstrong
2023-27-F1
ITS Engineering College
Exp. Name: Write a program to convert binary
S.No: 20 Date: 2024-06-30
number to decimal number.
Aim:
Page No: 34
AIM/OBJECTIVE: Write a program to convert binary number to decimal number.
THEORY:
Program to convert the given binary number into decimal number is discussed here. For example, the
decimal equivalent of binary number
ID: 2302221530011
1111 is 15.
Algorithm:
1. Input the binary number.
2. Multiply each digit of the binary number with the power of 2 and add each multiplication result.
3. The power starts from 0 to n-1 where n is the total number of digits in the binary number.
Source Code:
binaryToDecimal.c
#include<stdio.h>
#include<math.h>
2023-27-F1
#include<conio.h>
int main()
{
int num,binary_Num,decimal_num=0,base=1,rem;
scanf("%d",&num);
User Output
11100011
Decimal equivalent = 227
Test Case - 2
User Output
10101
Decimal equivalent = 21
Exp. Name: Write the code to find the sum of the
S.No: 21 Date: 2024-07-08
elements in an array
Aim:
Page No: 35
Write a C program to find the sum of the elements in an array.
Input Format: Input consists of 2 lines. The integer present in the first line corresponds to the size of the
array. The next line of integers corresponds to the elements in the array. Assume that the maximum value of
n is 15.
ID: 2302221530011
Output Format: Refer sample output for details.
Source Code:
sumOfArray.c
#include<stdio.h>
int main()
{
int size,i,sum=0;
scanf("%d",&size);
int arr[size];
for(i=0;i<size;i++)
2023-27-F1
{
scanf("%d",&arr[i]);
sum=sum+arr[i];}
printf("%d",sum);
return 0;
User Output
5
23681
20
Test Case - 2
User Output
5
-1 -2 -3 1 2
-3
Exp. Name: Write a program that inputs two
arrays and saves sum of corresponding
S.No: 22 Date: 2024-07-08
elements of these arrays in a third array and
prints them.
Page No: 36
Aim:
Write a program that inputs two arrays and saves sum of corresponding elements of these arrays in a third
array and prints them.
Source Code:
ID: 2302221530011
arrays.c
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int arr1[n],arr2[n],sum_array[n];
for(int i=0;i<n;i++){
printf("ar1[%d]= ",i);
2023-27-F1
scanf("%d",&arr1[i]);
}
for(int i=0;i<n;i++){
printf("ar2[%d]= ",i);
scanf("%d",&arr2[i]);
User Output
3
ar1[0]=
1
ar1[1]=
2
ar1[2]=
3
ar2[0]=
1
ar2[1]=
2
ar2[2]=
3
Sum array[0] = 2
Sum array[1] = 4
Sum array[2] = 6
Page No: 37
Test Case - 2
User Output
4
ID: 2302221530011
ar1[0]=
5
ar1[1]=
6
ar1[2]=
7
ar1[3]=
4
ar2[0]=
9
ar2[1]=
2023-27-F1
2
ar2[2]=
4
ar2[3]=
8
Aim:
Page No: 38
Write a C Program to find the minimum and maximum element of the array.
Source Code:
max_min.c
#include<stdio.h>
ID: 2302221530011
int main()
{
int n,j=0, max=0,min=0;
printf("Enter size of the array : ");
scanf("%d",&n);
int arr[n];
2023-27-F1
for(int i=0; i<n;i++){
if(min>arr[i]){
(min=arr[i]);
}
else if(max<arr[i]){
User Output
Enter size of the array :
5
Enter elements in array :
1
21
3
52
36
minimum of array is : 1
maximum of array is : 52
Test Case - 2
User Output
Enter size of the array :
7
Enter elements in array :
100
Page No: 39
130
190
180
150
ID: 2302221530011
130
100
minimum of array is : 100
maximum of array is : 190
2023-27-F1
ITS Engineering College
Exp. Name: Search an element in an Array using
S.No: 24 Date: 2024-07-08
Linear Search.
Aim:
Page No: 40
Write a C Program to search an element in an array using Linear Search.
Source Code:
Linear_search.c
#include<stdio.h>
ID: 2302221530011
int main ()
{
int array[100],search, c,number ;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for(c=0;c<number;c++)
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for(c=0;c<number; c++){
if (array[c]==search)
2023-27-F1
{printf("%d is present at location %d.\n",search,c+1);
break;
}
}
if (c==number)
User Output
Enter the number of elements in array
5
Enter 5 numbers
12
23
22
10
45
Enter the number to search
22
22 is present at location 3.
Test Case - 2
User Output
Enter the number of elements in array
3
Enter 3 numbers
21
42
Page No: 41
56
Enter the number to search
1
1 is not present in array.
ID: 2302221530011
2023-27-F1
ITS Engineering College
S.No: 25 Exp. Name: Write the code Date: 2024-07-08
Aim:
Write a program to sort the elements of the array in ascending order using Bubble Sort technique.
Page No: 42
Source Code:
bubble_sort.c
#include<stdio.h>
int main()
ID: 2302221530011
{
int arr[5],flag=0,i=0,j=0;
for(i=0;i<5;i++)
{
printf("arr[%d] = ",i);
scanf("%d",&arr[i]);
}
printf("Unsorted array: \n");
for(i=0;i<5;i++){
printf("%d ",arr[i]);
}
printf("\n");
2023-27-F1
printf("Sorted array: \n");
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
User Output
arr[0] =
80
arr[1] =
60
arr[2] =
90
arr[3] =
10
arr[4] =
40
Page No: 43
Unsorted array:
80 60 90 10 40
Sorted array:
10 40 60 80 90
ID: 2302221530011
Test Case - 2
User Output
arr[0] =
21
arr[1] =
2
arr[2] =
31
arr[3] =
2023-27-F1
67
arr[4] =
45
Unsorted array:
21 2 31 67 45
Aim:
Page No: 44
Write a C Program to add and multiply two matrices of order nxn.
Source Code:
multiply_matrice.c
ID: 2302221530011
2023-27-F1
ITS Engineering College
printf("Addition not possible\nMultiplication not possible\n");
}
//display for addition
printf("Addition:\n");
Page No: 45
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
ID: 2302221530011
//Display for Multiplication
printf("Multiplication:\n");
for(i=0;i<row1;i++){
for(j=0;j<col2;j++){
printf("%d\t",d[i][j]);
}
printf("\n");
}
}
2023-27-F1
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
User Output
For 1st Matrix
Enter no of rows:
Page No: 46
2
Enter no of columns:
3
For 2nd Matrix
Enter no of rows:
ID: 2302221530011
2
Enter no of columns:
1
Enter elements of 1st matrix
2
3
4
5
6
3
2023-27-F1
Enter elements of 2nd matrix
2
3
Addition not possible
Aim:
Page No: 47
Write a C Program that finds the sum of diagonal elements of a mxn matrix.
Source Code:
sum_of_elements.c
ID: 2302221530011
#include<stdio.h>
void main()
{
int arr[10][10];
int i,j,m,n,a=0,sum=0;
printf("Enter the order of the matrix \n");
scanf("%d %d",&m,&n);
if(m==n)
{
printf("Enter the co-efficients of the matrix\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&arr[i][j]);
2023-27-F1
}
}
printf("The given matrix is \n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
User Output
Enter the order of the matrix
2
2
Enter the co-efficients of the matrix
12
12
13
13
The given matrix is
12 12 13 13The sum of the main diagonal elements is = 25
The sum of the off diagonal elements is = 25
Page No: 48
Test Case - 2
User Output
Enter the order of the matrix
2
ID: 2302221530011
3
The given order is not square matrix
2023-27-F1
ITS Engineering College
Exp. Name: Write a program to implement
S.No: 28 strlen(), strcat(), strcpy() using the concept of Date: 2024-07-09
functions.
Page No: 49
Aim:
Write a program to implement strlen(), strcat(), strcpy() using the concept of functions.
Source Code:
functions.c
ID: 2302221530011
#include<stdio.h>
#include<string.h>
int main()
{
char str1[50];
char str2[50];
printf("Str1: ");
scanf("%s",str1);
//gets(str1);
printf("Str2: ");
scanf("%s",str2);
2023-27-F1
int n1 = strlen(str1);
printf("Str1 Len: %d\n",n1);
int n2 = strlen(str2);
printf("Str2 Len: %d\n",n2);
char tmp[50];
strcpy(tmp,str1);
User Output
Str1:
Pen
Str2:
Pencil
Str1 Len: 3
Str2 Len: 6
Copied Str: Pencil
Concatenated Str: PenPencil
Test Case - 2
User Output
Str1:
Bat
Str2:
Ball
Str1 Len: 3
Page No: 50
Str2 Len: 4
Copied Str: Ball
Concatenated Str: BatBall
ID: 2302221530011
2023-27-F1
ITS Engineering College
Exp. Name: Maintain a train timetable and
S.No: 29 Date: 2024-07-10
implement the following operations.
Aim:
Page No: 51
Define a structure data type TRAIN_INFO. The type contain Train No : integer type, Train name: string,
Departure Time: aggregate type TIME, Arrival Time: aggregate type TIME, Start station: string, End station:
string. The structure type Time contains two integer members: hour and minute. Maintain a train timetable
and implement the following operations:
• List all the trains (sorted according to train number) that depart from a particular section.
ID: 2302221530011
• List all the trains that depart from a particular station at a particular time.
• List all he trains that depart from a particular station within the next one hour of a given time.
• List all the trains between a pair of start station and end station.
Source Code:
trainOperations.c
2023-27-F1
ITS Engineering College
printf("Input Train Name: ");
scanf("%s",arr[n].name);
printf("Input Start Station: ");
scanf("%s",arr[n].start);
printf("Input End Station: ");
Page No: 52
scanf("%s",arr[n].end);
printf("Input Departure Time: \n");
arr[n].atime = set();
printf("Input Arrival Time: \n");
arr[n].dtime = set();
printf("Train %d added successfully\n",arr[n].TRno);
ID: 2302221530011
n++;
}
if(a == 2){
int temp;
if(n ==0){
printf("\nError! No Train Available.\n\n");
}
printf("Input Train Number: ");
scanf("%d",&temp);
for(int i = 0;i<n;i++){
if(temp == arr[i].TRno){
2023-27-F1
while(i<=n-1)
arr[i] = arr[i+1];
}
n--;
}
}
if (a>3){
printf("Wrong Choice.\n\n");
}
}
}
void main(){
tinfo arr[50];
int m = 1;
edit(arr);
while(m){
printf("MENU\n");
printf("1.List all the trains departed from a particular
station\n");
printf("2.List all the trains departed from a particular station at
a particular time\n");
printf("3.List all the trains departed from particular station
within the next one hour of a given time\n");
printf("4.List all the trains between a pair of start station and
end station\n");
printf("5.Edit train details\n");
printf("6.Exit\n");
printf("Your choice: ");
scanf("%d",&m);
if(m == 1){
char temp[50]
;
printf("Input Details\n");
printf("Depart Station: ");
scanf("%s",temp);
for(int i = 0;i<n;i++){
Page No: 53
int test =
strcmp(arr[i].start,temp);
if(test == 0){
printf("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n",arr[i].TRno,arr[i].name,arr[i].start,arr[i].en‑
ID: 2302221530011
2023-27-F1
ITS Engineering College
d,arr[i].atime.hours,arr[i].atime.minutes,arr[i].dtime.hours,arr[i].dtime.minutes);
}
}
}
if(m == 2){
Page No: 54
char temp[50];
printf("Input Details\n");
printf("Depart Station: ");
scanf("%s",temp);
printf("Train Time: \n");
time t = set();
ID: 2302221530011
for(int i = 0;i<n;i++){
//printf("%d
%d%s",t.minutes,arr[i].atime.minutes,temp);
if(strcmp((arr[i].start),temp)==0){
if((t.hours ==arr[i].atime.hours) &&( t.minutes ==arr[i].atime.minutes)){
printf("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n",arr[i].TRno,arr[i].name,arr[i].start,arr[i].end,arr[i].atime.hours,arr[i].atime.minute
}
}
}
}
if(m == 3){
char temp[50];
2023-27-F1
printf("Input Details\n");
printf("Depart Station: ");
scanf("%s",temp);
printf("Time: \n");
time t = set();
printf("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n",arr[i].TRno,arr[i].name,arr[i].start,arr[i].end,arr[i].atime.hours,arr[i].atime.minute
}
}
if(m==4){
char temp[50];
char temp2[50];
printf("\t\t****INPUT DETAILS****\n");
printf("Depart Station: ");
scanf("%s",temp);
printf("Arrival Station: ");
scanf("%s",temp2);
for(int i = 0;i<n;i++){
if((strcmp(temp,arr[i].start) == 0) &&
(strcmp(temp2,arr[i].end) == 0)){
printf("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n",arr[i].TRno,arr[i].name,arr[i].start,arr[i].end,arr[i].atime.hours,arr[i].atime.minute
}
}
}
if(m == 5){
edit(arr);
}
if(m == 6){
m = 0;
}
}
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 55
Train Edit Menu
1.Add Train
2.Delete Train
3.Exit Train Edit Menu
Your Choice:
ID: 2302221530011
1
Train Number:
14511
Input Train Name:
Nauchandi_Express
Input Start Station:
Prayagraj
Input End Station:
Saharanpur
Input Departure Time:
Hour:
2023-27-F1
10
Minute:
15
Input Arrival Time:
Hour:
Page No: 56
2.Delete Train
3.Exit Train Edit Menu
Your Choice:
3
MENU
ID: 2302221530011
1.List all the trains departed from a particular station
2.List all the trains departed from a particular station at a particular time
3.List all the trains departed from particular station within the next one hour of
a given time
4.List all the trains between a pair of start station and end station
5.Edit train details
6.Exit
Your choice:
1
Input Details
Depart Station:
Prayagraj
2023-27-F1
14511 Nauchandi_Express Prayagraj Saharanpur 10:15
23:10
14512 Sangam_Express Prayagraj Meerut 10:31 21:17
MENU
Page No: 57
Hour:
10
Minute:
10
14511 Nauchandi_Express Prayagraj Saharanpur 10:15
ID: 2302221530011
23:10
14512 Sangam_Express Prayagraj Meerut 10:31 21:17
MENU
1.List all the trains departed from a particular station
2.List all the trains departed from a particular station at a particular time
3.List all the trains departed from particular station within the next one hour of
a given time
4.List all the trains between a pair of start station and end station
5.Edit train details
6.Exit
Your choice:
2023-27-F1
6
Aim:
Page No: 58
Write a C Programto swap two elements using the concept of pointers.
Source Code:
swap_elements.c
#include<stdio.h>
ID: 2302221530011
void swap(int *a, int *b){
int temp = *(b);
*(b) = *(a);
*(a) = temp;
printf("After swapping values in function a = %d, b = %d\n", *(a), *(b));
}
void main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("Before swapping the values in main a = %d, b = %d\n", a, b);
swap(&a,&b);
2023-27-F1
printf("After swapping values in main a = %d, b = %d\n", a, b);
}
User Output
10
20
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
Test Case - 2
User Output
50
60
Before swapping the values in main a = 50, b = 60
After swapping values in function a = 60, b = 50
After swapping values in main a = 60, b = 50
Exp. Name: Write a program to compare the
S.No: 31 contents of two files and determine whether Date: 2024-07-10
they are identical.
Page No: 59
Aim:
Write a C program to compare the contents of two files and determine whether they are identical.
Source Code:
files.c
ID: 2302221530011
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1, *fp2;
int ch1, ch2;
char fname1[5], fname2[5];
int flag = 1;
printf("Name of file1: ");
scanf("%s", fname1);
printf("Name of File2: ");
2023-27-F1
scanf("%s", fname2);
fp1 = fopen(fname1, "r");
fp2 = fopen(fname2, "r");
if (fp1 == NULL)
{
printf("Identical Files");
1.txt
Once upon a time, a farmer had a goose that laid one golden egg every day. The egg
provided enough money for the farmer and his wife to support their daily needs. The
farmer and his wife continued to be happy for a long time.
Page No: 60
2.txt
Once upon a time, a farmer had a goose that laid one golden egg every day. The egg
provided enough money for the farmer and his wife to support their daily needs. The
farmer and his wife continued to be happy for a long time.
ID: 2302221530011
3.txt
But, one day, the farmer thought to himself, “Why should we take just one egg a day?
Why can’t we take them all at once and make a lot of money?” The farmer told his
wife his idea, and she foolishly agreed.
2023-27-F1
Test Case - 1
User Output
Name of file1:
1.txt
Test Case - 2
User Output
Name of file1:
3.txt
Name of File2:
1.txt
Non-identical files
Exp. Name: Write a program to check whether a
S.No: 32 given word exists in a file. If yes, then find the Date: 2024-07-09
number of times it occurs.
Page No: 61
Aim:
Write a program to check whether a given word exists in a file. If yes, then find the number of times it
occurs.
Source Code:
ID: 2302221530011
word.c
#include<stdio.h>
#include<string.h>
void main()
{
FILE *ptr;
int word=0;
int length =225;
char search[225],path[225],line[225];
printf("Word: ");
scanf("%s",search);
2023-27-F1
printf("Name of file: ");
scanf("%s",path);
ptr=fopen(path,"r");
while(fgets(line, length, ptr))
{
char*ptr = strstr(line, search);
4.txt
Short moral stories work well at getting the child’s attention, keeping them focused
during the length of the story.
5.txt
But, one day, the farmer thought to himself, “Why should we take just one egg a day?
Why can’t we take them all at once and make a lot of money?” The farmer told his
wife his idea, and she foolishly agreed.
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 62
Word:
at
Name of file:
4.txt
Word exists.
ID: 2302221530011
Test Case - 2
User Output
Word:
is
Name of file:
4.txt
Word doesn't exist.
2023-27-F1
Test Case - 3
User Output
Word:
Test Case - 4
User Output
Word:
know
Name of file:
5.txt
Word doesn't exist.