0% found this document useful (0 votes)
66 views34 pages

Finalpps Lab (BCS-251)

PPS lab

Uploaded by

SACHIN VERMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views34 pages

Finalpps Lab (BCS-251)

PPS lab

Uploaded by

SACHIN VERMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 34

SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &

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

BCS 251 : PROGRAMMING FOR PROBLEM SOLVING LAB

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

30. WAP to swap two elements using the concept of pointers.


31. WAP to compare the contents of two files and determine whether they are same or not.
32.WAP to check whether a given word exists in a file or not. If yes then find the number of
times it occurs.
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.3 WAP to calculate the area and circumference of a circle.


#include<stdio.h>
#include<conio.h>
void main()
{
float radius, area,circum;
clrscr();
printf("\nEnter the radius of Circle : ");
scanf("%d", &radius);
area = 3.14 * radius * radius;
printf("\nArea of Circle : %f", area);
circum=2*3.14*radius;
printf("\nCircumference of Circle : %f", circum);
getch();
}

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();
}

Q.7 WAP to find the greatest of three numbers.

#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.8 WAP that finds whether a given number is even or odd.


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

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

Q.12. WAP to print the sum of all numbers up to a given number.

#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

Q.16 WAP to check whether the entered number is prime or not.


Here, we use brute force approach by testing whether n is a multiple of any integer between 2 and N/2.
This is the most basic method of checking the primality of a given integer n is called trial
division.
#include<stdio.h>
#include<conio.h>
int main() {
int num, i, isPrime=0;
printf("Enter a positive number\n");
scanf("%d",&num);
/* Check whether num is divisible by any number between 2 to (num/2)
*/
for(i = 2; i <=(num/2); ++i) {
if(num%i==0) {
isPrime=1;
break;
}
}
if(isPrime==0)
printf("%d is a Prime Number",num);
else
printf("%d is NOT a Prime Number",num);
getch();
return 0;
}

Q.17 WAP to find the sum of digits of the entered number.

#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

Q.18 WAP to find the reverse of a number.

#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.19 WAP to print Armstrong numbers from 1 to 100.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,rem,sum;
printf("Armstrong Number are:\n");
for(n=1;n<=1000;n++)
{
temp=n;
sum=0;
while(temp!=0)
{
rem = temp % 10;
sum = sum + (rem * rem * rem);
temp = temp / 10;
}
if(n==sum)
printf("%d\n",n);
}
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

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;

printf("Enter a binary number(1s and 0s) \n");


scanf("%d", &num); /* maximum five digits */
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
getch();
}

decimal number to binary number-

#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;

printf("Enter array size\n");


scanf("%d",&n);
printf("Enter array elements\n");
for(i = 0; i <n; i++)
scanf("%d",&arr[i]);
for(i = 0; i < n; i++)
sum = sum + arr[i];
printf("Sum of the array = %d\n",sum);
getch();
}

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

Q.23 WAP to find the maximum and minimum element of an array.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,max,min;
clrscr();
printf("enter the array");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];

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

Q . 24. WAP to search an element in an array using Linear search


#include <stdio.h>
#include<conio.h>
int main()
{
int a[10], i, item,n;
printf("\nEnter number of elements of an array:\n");
scanf("%d",&n);
printf("\nEnter elements: \n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem does not exist.");
getch();
}

Output:
Enter number of elements of an array:
8

Enter elements:
23578641

Enter item to search: 1

Item found at location 8

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;

printf("Enter number of elements\n");


scanf("%d", &n);
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d]>array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

Q . 26 WAP to add and multiply two matrices of order nxn.


Addition of two matrices
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3],i,j;
clrscr();
printf("\nENTER VALUES FOR MATRIX A:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRIX B:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nTHE VALUES OF MATRIX C ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("\n");
}
getch();
}

Multiplication of two matrices


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]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][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

Q . 27 WAP that Finds Sum of Diagonal Elements of Matrix

#include <stdio.h>
#include<conio.h>

void main()
{
int arr[10][10];
int i, j, m, n, sum1 = 0, sum2 = 0;

printf("Enter the order of the matix: ");


scanf("%d %d", &m, &n);

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

for (i = 0; i < m; ++i)


{
sum1 = sum1 + arr[i][i];
sum2 = sum2 + arr[i][m - i - 1];
}

printf("\nThe sum of the main diagonal: %d\n", sum1);


printf("The sum of the opposite diagonal: %d\n", sum2);

}
else
printf("The order entered is not square matrix.");
getch();

Q . 28 WAP to implement strlen(), strcat(), strcpy() using the concept of functions.

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

typedef struct TIME{


int hour;
int minute;
}TIME;

typedef struct TRAIN_INFO{


int train_no;
char train_name[35];
char start_st[35];
char end_st[35];
TIME dept_time;
TIME arr_time;
} TRAIN;

void train_edit (TRAIN *, int *);


int main (void)
{
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

int no_of_trains = 0;
int i;
int choice;
char dept_st[35];
char arr_st[35];
TIME train_time;
TRAIN train[MAX];

//Call train_edit function first time.


train_edit (train, &no_of_trains);

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

train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);


}
}
printf ("\n");
break;

//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;

//Edit train details. Call for train_edit


case 5:
printf ("\n");
train_edit (train, &no_of_trains);
break;

//Exit
case 6:
return 0;
//Wrong Choice
default:
printf ("\nError! Wrong Choice.\n\n");
}
}
return 0;
}

void train_edit (TRAIN *train, int *no_of_trains)


{
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

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;
}

//Input train details in temp variable.


printf ("\nInput Train Number: ");
scanf ("%d", &temp.train_no);

//Check if train number already exist or not.


for (i = 0; i <= *no_of_trains - 1; i++)
{
if (train[i].train_no == temp.train_no)
{
printf ("Error! Train number %d already exist. Please try again.\n\n");
temp.train_no = -1;
break;
}
}
if (temp.train_no == -1)
break;

printf ("Input Train Name: ");


fflush (stdin);
gets (temp.train_name);
printf ("Input Start Station: ");
fflush (stdin);
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

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);

//Add temp into sorted array.


train[*no_of_trains] = temp;
for (i = *no_of_trains; i >= 1; i--)
{
if (train[i - 1].train_no >= train[i].train_no)
{
temp = train[i - 1];
train[i - 1] = train[i];
train[i] = temp;
}
else
break;
}
*no_of_trains = *no_of_trains + 1;
printf ("Train %d added successfully.\n\n", train[i].train_no);
break;

//Delete Train
case 2:
//No train available.
if (*no_of_trains == 0)
{
printf ("\nError! No Train Available.\n\n");
break;
}

//Input train details.


printf ("\nInput Train Number: ");
scanf ("%d", &train_num);

//Find and delete train.


SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

for (i = 0; i <= *no_of_trains - 1; i++)


{
if (train[i].train_no == train_num)
{
while (i <= *no_of_trains - 1)
{
train[i] = train[i + 1];
i++;
}
*no_of_trains = *no_of_trains - 1;
printf ("Train %d deleted successfully.\n\n", train_num);
train_num = -1;
break;
}
}

//Train not found.


if (train_num != -1)
printf ("Error! Train %d not found\n\n", train_num);

break;

//Exit Train Edit Menu


case 3:
printf ("\n");
return;

//Wrong choice
default:
printf ("\nError! Wrong Choice.\n\n");
}
}
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

Q 30. WAP to swap two elements using concept of pointers


Solution:

#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

FILE HANDLING PROGRAMS

31) Write a program in C to create and store information in a text file.

Solution

#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[1000];
FILE *fptr;
char fname[20]="test.txt";

printf("\n\n Create a file (test.txt) and input text :\n");


printf("----------------------------------------------\n");
fptr=fopen(fname,"w");
if(fptr==NULL)
{
printf(" Error in opening file!");
exit(1);
}
printf(" Input a sentence for the file : ");
fgets(str, sizeof str, stdin);
fprintf(fptr,"%s",str);
fclose(fptr);
printf("\n The file %s created successfully...!!\n\n",fname);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly

32) Write a program in C to write multiple lines in a text file

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

You might also like