Finalpps Lab (BCS-251)
Finalpps Lab (BCS-251)
Research,Bareilly
PPS
LAB SOLUTION
BCS-251
Semester: II Year: FIRST
Session: 2023-24
Department of Computer Science and Engineering
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
1. WAP that accepts the marks of 5 subjects and finds the sum and percentage marks
obtained by the student.
2. WAP that calculates the Simple Interest and Compound Interest. The Principal, Amount, Rate of Interest and
Time are entered through the keyboard.
3. WAP to calculate the area and circumference of a circle.
4. WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the
formula C/5=(F-32)/9.
5. WAP that swaps values of two variables using a third variable.
6. WAP that checks whether the two numbers entered by the user are equal or not.
7. WAP to find the greatest of three numbers.
8. WAP that finds whether a given number is even or odd.
9. WAP that tells whether a given year is a leap year or not.
10.WAP 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’
60-80%-----------------Print ‘C’
Below 60%-------------Print ‘D’
11. WAP that takes two operands and one operator from the user, perform the operation, and prints the result by
using Switch statement.
12. WAP to print the sum of all numbers up to a given number.
13. WAP to find the factorial of a given number.
14.WAP to print sum of even and odd numbers from 1 to N numbers.
15. WAP to print the Fibonacci series.
16.WAP to check whether the entered number is prime or not.
17. WAP to find the sum of digits of the entered number.
18.WAP to find the reverse of a number.
19.WAP to print Armstrong numbers from 1 to 100.
20.WAP to convert binary number into decimal number and vice versa.
21. WAP that simply takes elements of the array from the user and finds the sum of these elements.
22.WAP that inputs two arrays and saves sum of corresponding elements of these arrays in a third array and prints
them.
23.WAP to find the minimum and maximum element of the array.
24.WAP to search an element in a array using Linear Search.
25.WAP to sort the elements of the array in ascending order using Bubble Sort technique
26.WAP to add and multiply two matrices of order nxn.
27.WAP that finds the sum of diagonal elements of a mxn matrix.
28.WAP to implement strlen (), strcat (),strcpy () using the concept of Functions.
29.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:
a. List all the trains (sorted according to train number) that depart from a particular
section.
b. List all the trains that depart from a particular station at a particular time.
c. List all he trains that depart from a particular station within the next one hour of
a given time.
d. List all the trains between a pair of start station and end station.
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Q.1 WAP that accepts the marks of 5 subjects and finds the sum and average of marks obtained by
the student.
#include <stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5,sum,avg;
clrscr();
printf("Enter the first marks.\n");
scanf("%d", &m1);
printf("Enter the second marks.\n");
scanf("%d", &m2);
printf("Enter the third marks.\n");
scanf("%d", &m3);
printf("Enter the fourth marks.\n");
scanf("%d", &m4);
printf("Enter the fifth marks.\n");
sum=m1+m2+m3+m4+m5;
printf("the sum of the numbers are %d\n",sum);
avg=sum/5;
printf("”the average of five marks is %d\n",avg);
getch();
}
Q.2 WAP that calculate the simple interest and compound interest.
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main()
{
int p,n,r;
float si=0,ci=0;
clrscr();
printf("Enter p,n,r values\n");
scanf("%d%d%d",&p,&n,&r);
si=(p*n*r)/100.0;
ci=p*pow(1+r/100,n);
printf("simple interest is =%f\n and compound interest is=%f",si,ci);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Q.4 WAP that accepts the temperature in centigrade and converts into Fahrenheit using the
formula C/5=(F-32)/9.
# include <stdio.h>
# include <conio.h>
void main()
{
float c, f ;
clrscr() ;
printf("Enter the centigrade : ") ;
scanf("%f", &c) ;
f = ((c * 9)/5) + 32
printf("\nThe farenheit is : %f", f) ;
getch() ;
}
Q.5 WAP that swap values of two variables using a third variable.
#include <stdio.h>
#include<conio.h>
void main()
{
int x, y, temp;
clrscr();
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
getch(); }
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Q.6 WAP that checks whether the two numbers entered by the user are equal or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n;
printf(" Enter the values of m and n:");
scanf("%d%d",&m,&n);
if(m==n)
printf(" m and n are equal");
else
printf(" m and n are not equal");
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3 )
printf("%.2f is the largest number.", n1);
if( n2>=n1 && n2>=n3 )
printf("%.2f is the largest number.", n2);
if( n3>=n1 && n3>=n2 )
printf("%.2f is the largest number.", n3);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Q.9 WAP that tells whether a given year is a leap year or not.
#include<stdio.h>
#include<conio.h>
void main(){
int year;
printf("Enter any year: ");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Q.10 WAP 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’
60-80%-------------------------- Print ‘C’
Below 60%---------------------- Print ‘D’
#include <stdio.h>
#include <conio.h>
void main()
{
int phy, chem, bio, math, comp;
float per;
printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);
per = (phy + chem + bio + math + comp) / 5.0;
printf("Percentage = %.2f\n", per);
if(per >= 90)
{
printf("Grade A");
}
else if(per >= 80)
{
printf("Grade B");
}
else if(per >= 60)
{
printf("Grade C");
}
else if(per <= 60)
{
printf("Grade D");
}
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Q-11 WAP that takes two operands and one operator from the user, perform the operation, and
prints the result by using Switch statement.
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)n");
scanf("%c", &ch);
printf("Enter the values of a and bn");
scanf("%d%d", &a, &b);
switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
#include <stdio.h>
#include <conio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d",sum);
getch();
}
Q.13 WAP to find the factorial of a given number.
include<stdio.h>
#include<conio.h>
int main(){
int i,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
f=f*i;
printf("Factorial of %d is: %d",num,f);
getch();
}
Q.14 WAP to print sum of even and odd numbers from 1 to N numbers.
#include <conio.h>
int main() {
int counter, N, sum = 0;
/*
* Take a positive number as input form user
*/
printf("Enter a Positive Number\n");
scanf("%d", &N);
for(counter = 1; counter <= N; counter++) {
/* Even numbers are divisible by 2 */
if(counter%2 == 0) {
/* counter is even, add it to sum */
sum = sum + counter;
}
}
printf("Sum of all Even numbers between 1 to %d is %d", N, sum);
getch();
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
}
------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
int main() {
int counter, N, sum = 0;
/*
* Take a positive number as input form user
*/
printf("Enter a Positive Number\n");
scanf("%d", &N);
for(counter = 1; counter <= N; counter++) {
/* Odd numbers are not divisible by 2 */
if(counter%2 == 1) {
/* counter is odd, add it to sum */
sum = sum + counter;
}
}
printf("Sum of all Odd numbers between 1 to %d is %d", N, sum);
getch();
}
Q.15 WAP to print the Fibonacci series.
#include <stdio.h>
#include <conio.h>
int main()
{
int a, b, c, i, n;
printf("Enter value of n to print Fibonacci series : ");
scanf("%d", &n);
a = 0;
b = 1;
c = 0;
for(i=1; i<=n; i++)
{
printf("%d, ", c);
a=b;
b=c;
c=a+b;
}
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,sum=0;
printf("Enter any five digits number\n");
scanf("%d",&num);
while(num!=0)
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
printf("Sum of enter number is = %d\n", sum);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,rem;
printf("Enter any four digits number\n");
scanf("%d",&num);
while(num!=0)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}
printf("Reverse of enter number is = %d\n", rev);
getch();
}
Q.20 Write a program to convert binary number into decimal number and vice versa.
#include <stdio.h>
#include<conio.h>
void main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
#include<stdio.h>
#include<conio.h>
int main()
{
long int m,no=0,a=1;
int n,rem;
printf("Enter any decimal number->");
scanf("%d",&n);
m=n;
while(n!=0)
{
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf("The value %ld in binary is->",m);
printf("%ld",no);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Q.21 WAP that simply takes elements of the array from the user and finds the sum of these
elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[100], n, i, sum = 0;
Q.22 WAP that inputs two arrays and saves sum of corresponding elements of these arrays in a
third array and prints them.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,ar1[10],ar2[10],sum[10];
printf("Enter first array:-\n");
for(i=0;i<=9;i++)
{
printf("ar1[%d]=",i);
scanf("%d",&ar1[i]);
}
printf("Enter second array:-\n");
for(i=0;i<=9;i++)
{
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
printf("ar2[%d]=",i);
scanf("%d",&ar2[i]);
}
for(i=0;i<=9;i++)
{
sum[i]=ar1[i]+ar2[i];
}
printf("Sum of arrays:-");
for(i=0;i<=9;i++)
{
printf("\nsum[%d]=%d",i,sum[i]);
}
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
for(i=0;i<10;i++)
{
if(a[i]<min)
min=a[i];
}
for(i=0;i<10;i++)
{
if(a[i]>max)
max=a[i];
}
printf("\n maximum=%d minimum=%d",max,min);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Output:
Enter number of elements of an array:
8
Enter elements:
23578641
Q . 25 WAP to sort the elements of the array in ascending order using Bubble Sort technique.
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], n, c, d, swap;
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
getch();
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
#include <stdio.h>
#include<conio.h>
void main()
{
int arr[10][10];
int i, j, m, n, sum1 = 0, sum2 = 0;
if (m == n)
{
printf("Enter the elements for the matrix: \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &arr[i][j]);
}
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
}
else
printf("The order entered is not square matrix.");
getch();
1. strlen()
#include<stdio.h>
#include<string.h>
void main main()
{
char name[30]="Rahul";
int num;
num=strlen(name);
printf("\nNumber of characters=%d",num);
getch();
2. strcat();
#include<stdio.h>
#include<string.h>
void main()
{
char name[30]=”Rahul”;
strcat(name,” Singh”);
printf("name=%s",name);
getch();
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
3. strcpy();
#include<stdio.h>
#include<string.h>
int main()
{
char name[20];
strcpy(name,”Rahul”);
printf("name=%s",name);
getch();
}
Q . 29 Define a structure data type TRAIN_INFO. The type contains 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:
(i) List all the trains (stored according to train number) that depart from a particular station.
(ii) List all the trains that depart from a particular station at a particular time.
(iii) List all the trains that depart from a particular station within the next one hour of a given
time.
(iv) List all the trains between a pair of start station and end station.
Solution:
#include<stdio.h>
#include<string.h>
#define MAX 50
int no_of_trains = 0;
int i;
int choice;
char dept_st[35];
char arr_st[35];
TIME train_time;
TRAIN train[MAX];
while (1)
{
//Display Main Menu
printf ("\t\t\t****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: ");
//Input choice
scanf ("%d", &choice);
switch (choice)
{
//List all the trains departed from a particular station.
case 1:
printf ("\n\t\t****INPUT DETAILS****\n");
printf ("Depart Station: ");
fflush (stdin);
gets (dept_st);
//Print trains
for (i = 0; i <= no_of_trains - 1; i++)
{
if (strcmp (train[i].start_st, dept_st) == 0)
{
printf ("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n", train[i].train_no, train[i].train_name,
train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
}
}
printf ("\n");
break;
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
//List all the trains departed from a particular station at a particular time.
case 2:
printf ("\n\t\t****INPUT DETAILS****\n");
printf ("Depart Station: ");
fflush (stdin);
gets (dept_st);
printf ("Train Time: \n");
printf ("Hour: ");
scanf ("%d", &train_time.hour);
printf ("Minute: ");
scanf ("%d", &train_time.minute);
//Print trains
for (i = 0; i <= no_of_trains - 1; i++)
{
if (strcmp (train[i].start_st, dept_st) == 0 && train[i].dept_time.hour == train_time.hour
&& train[i].dept_time.minute == train_time.minute)
{
printf ("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n", train[i].train_no,
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
}
}
printf ("\n");
break;
//List all the trains departed from particular station within the next one hour of a given time.
case 3:
printf ("\n\t\t****INPUT DETAILS****\n");
printf ("Depart Station: ");
fflush (stdin);
gets (dept_st);
printf ("Time: \n");
printf ("Hour: ");
scanf ("%d", &train_time.hour);
printf ("Minute: ");
scanf ("%d", &train_time.minute);
//Print trains
for (i = 0; i <= no_of_trains - 1; i++)
{
if (strcmp (train[i].start_st, dept_st) == 0 && ((train[i].dept_time.hour == train_time.hour
&& train[i].dept_time.minute >= train_time.minute)
|| (train[i].dept_time.hour == train_time.hour + 1
&& train[i].dept_time.minute <= train_time.minute)))
{
printf ("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n", train[i].train_no,
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
//List all the trains between a pair of start station and end station.
case 4:
printf ("\n\t\t****INPUT DETAILS****\n");
printf ("Depart Station: ");
fflush (stdin);
gets (dept_st);
printf ("Arrival Station: ");
fflush (stdin);
gets (arr_st);
//Print trains
for (i = 0; i <= no_of_trains - 1; i++)
{
if ((strcmp (train[i].start_st, dept_st) == 0) && (strcmp (train[i].end_st, arr_st) == 0))
{
printf ("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n", train[i].train_no,
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
}
}
printf ("\n");
break;
//Exit
case 6:
return 0;
//Wrong Choice
default:
printf ("\nError! Wrong Choice.\n\n");
}
}
return 0;
}
int choice;
int i, train_num;
TRAIN temp;
while (1)
{
//Display Train Edit Menu
printf ("\t\t****TRAIN EDIT MENU****\n");
printf ("1. Add Train.\n");
printf ("2. Delete Train.\n");
printf ("3. Exit Train Edit Menu.\n");
printf ("Your Choice: ");
//Input choice
scanf ("%d", &choice);
switch (choice)
{
//Add Train
case 1:
//Already maximum trains.
if (*no_of_trains >= MAX)
{
printf ("\nError! There are already maximum trains.\n\n");
break;
}
gets (temp.start_st);
printf ("Input End Station: ");
fflush (stdin);
gets (temp.end_st);
printf ("Input Departure Time: \n");
printf ("Hour: ");
scanf ("%d", &temp.dept_time.hour);
printf ("Minute: ");
scanf ("%d", &temp.dept_time.minute);
printf ("Input Arrival Time: \n");
printf ("Hour: ");
scanf ("%d", &temp.arr_time.hour);
printf ("Minute: ");
scanf ("%d", &temp.arr_time.minute);
//Delete Train
case 2:
//No train available.
if (*no_of_trains == 0)
{
printf ("\nError! No Train Available.\n\n");
break;
}
break;
//Wrong choice
default:
printf ("\nError! Wrong Choice.\n\n");
}
}
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
#include <stdio.h>
#include <conio.h>
void swap(int *x,int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
void main()
{
int num1,num2;
printf("Enter value of num1: ");
scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);
printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);
swap(&num1,&num2);
printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Solution
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[1000];
FILE *fptr;
char fname[20]="test.txt";
Solution:
#include <stdio.h>
#include<conio.h>
int main ()
{
FILE * fptr;
int i,n;
char str[100];
char fname[20]="test.txt";
char str1;
printf("\n\n Write multiple lines in a text file and read the file :\n");
printf("------------------------------------------------------------\n");
printf(" Input the number of lines to be written : ");
scanf("%d", &n);
printf("\n :: The lines are ::\n");
fptr = fopen (fname,"w");
for(i = 0; i < n+1;i++)
{
fgets(str, sizeof str, stdin);
fputs(str, fptr);
}
fclose (fptr);
/*-------------- read the file -------------------------------------*/
fptr = fopen (fname, "r");
printf("\n The content of the file %s is :\n",fname);
str1 = fgetc(fptr);
while (str1 != EOF)
{
printf ("%c", str1);
str1 = fgetc(fptr);
}
printf("\n\n");
fclose (fptr);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly