Ayush Pps FILE

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

RAJKIYA ENGINEERING COLLEGE, AMBEDKAR NAGAR

​DEPARTMENT OF APPLIED SCIENCE AND HUMANITIES (APSH) )

PROGRAMMING FOR PROBLEM SOLVING LAB (BCS-251)

(LAB FILE)

SESSION: EVEN SEM (2023-24)

Submitted To: Submitted By:


Mr. Prince Rajpoot Ayush Sharma
Assistant Professor 1st Year (Second Sem)
Section -C
Information Technology
Roll No.: 2307370130023
INDEX
S.No. Program Date Signature
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.

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

INDEX
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.
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.
1. WAP that accepts the marks of 5 subjects and finds the sum and percentage
marks otained by the student.

#include<stdio.h>
int main()
{
int hindi, eng, math, phy, chem, total;
float per;
printf("Enter the mark of Hindi:");
scanf("%d", &hindi);
printf("Enter the mark of English:");
scanf("%d", &eng);
printf("Enter the mark of Math:");
scanf("%d", &math);
printf("Enter the mark of Physics:");
scanf("%d", &phy);
printf("Enter the mark of Chemistry:");
scanf("%d", &chem);
total = hindi + eng + math + phy + chem;
printf("total
mark: %d", total); per = total
/ 5; printf("percentage:
%f", per);
return 0;
}

​OUTPUT

2. WAP that calculates the simple interest and compound interest.The


Principal,Amount,Rate of interest and time are entered through the keyoard.
#include <stdio.h>
#include <math.h>
int main()
{
float p, r, t, n, si, ca, ci;
/* p=principal r=rate t=time
n=number of time interest got com si=simple
interest ca=
ci=compound interest*/
printf("enter the principal amount= ");
scanf("%f", &p); printf("enter the
rate= "); scanf("%f", &r);
printf("enter the time(in year)= ");
scanf("%f", &t);
si = (p * r * t) / 100;
printf("simple interest %f\n", si);
ci = p * pow((1 + r / 100), t) - p;
printf("compound interest %f", ci);
return 0;
}

​OUTPUT

3. WAP to calculate the area and circumference of a circle.

#include <stdio.h>
#include <math.h>

int main()
{
float r, area, cirf;
printf("enter the redius of circle=
"); scanf("%f", &r); area = 3.14 *
r * r; printf("area of circle %f\n",
area); cirf = 2 * 3.14 * r;
printf("circumference of circle %f", cirf);
return 0;
}

​OUTPUT

4. Wap that accepts the temperature in Centigrade and convert into


Fahrenheit using the formula C/5=(F-32)/9.

#include <stdio.h>
int main()
{
float c, f;
printf("Enter Temperture in Fahrenheit: ");
scanf("%f", &f); c = (f - 32) * 5 / 9;
printf("Fahrenheit to celsius is
= %f", c); return 0;
}

​OUTPUT

5. Wap that swaps values of two variables using a third variable.


#include <stdio.h>
int main()
{ int a, b,
c;
printf("Enter Value of a\n");
scanf("%d", &a);
printf("Enter Value of b\n");
scanf("%d", &b);
c = a;
a = b;
b = c;
printf("After swaping a=%d\n", a); printf("After swaping b=%d\n",
b);
return 0;
}

​OUTPUT

6. WAP that cheak wheather the two numbers entered by the user are equal or not.
#include <stdio.h>
int main()
{
int x, y;
printf("Enter Value of x,y\n");
scanf("%d%d", &x, &y);
if (x == y)
{
printf("x and y are equal\n");
}
else
{
printf("x and y are not equal\n");
}
return 0;

}
​OUTPUT

7. WAP to find the greatest of three numbers.

#include <stdio.h>
int main()
{
int a, b, c, great;
printf("Enter three Number :");
scanf("%d%d%d", &a, &b, &c);
great = a;
if (b > great)
{
great = b;
}
if (c > great)
{
great = c;

}
printf("\n Greatest Number is %d", great);
return 0;
}

​OUTPUT
8.WAP
the find whether a given number is even or odd.

#include <stdio.h>
int main()

{
int a;
printf("Enter a
number:"); scanf("%d",
&a); if (a % 2 == 0)
{
printf("Number=%d is even", a);
}
else
{
printf("Number=%d is odd", a);
}
return
0;
}

​OUTPUT

that tells whether a given year is a leap year or not.

#include <stdio.h>
int main()
{ int
year;
9.WAP
printf("Enter Any Year: ");
scanf("%d", &year);
if (year % 4 == 0)
{
printf("Year is Leap Year");
}
else
{
printf("Year is not Leap Year");
}

return 0;
}

​OUTPUT

that accepts marks of five subjects and finds percentage and prints
grades according to the following ceriteria: Between 90-100% Print'A'
80-90% Print'B' 60-80%
Print'C'
Below 60% Print'D'.

#include <stdio.h>
int main()

{
int hindi, eng, math, phy, chem, total;
float per;
printf("Enter marks of five Subjects; \n");
scanf("%d%d%d%d%d", &hindi, &eng, &math, &phy, &chem);
10.WAP
total = hindi + eng + math + phy +
chem; printf("Total Marks : %d\n", total);
per = total / 5; printf("Percentage
:%f\n", per); if (per > 90 && per <= 100)
{
printf("\n Grade A");
} else if (per > 80 && per
<= 90)
{
printf("\n Grade B");
} else if (per > 60 && per
<= 80)
{
printf("\n Grade C");
}

else
{ printf("\n Grade D");
}

return 0;
}

OUTPUT
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>
int main()
{
float a, b, k;
char op;
printf("enter the operator(+,-,*,/):");
scanf("%c", &op);
printf("enter two number a and b:\n");
scanf("%f%f", &a, &b);

switch (op)
{
case '+':
k = a + b;
printf("Addition is %f",
k); break; case '-':
:
k = a - b;
printf("Substraction is %f",
k); break; case '*':
k = a * b;
printf("Multiplication is %f",
k); break; case '/':
}

k = a / b;
printf("Divide is %f", k);
return 0;
}

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

#include <stdio.h>
int main()
{ int a, b, sum =
1;
printf("enter:a");
scanf("%d", &a);
for (b = 0; b <= a; b++)
{
sum = sum + b;
}

printf("sum = %d", sum);


return 0;
}

​OUTPUT

13.WAP to find the factorial of a given number.

#include <stdio.h>
int main()
{
int n, i, fact = 1;
printf("Enter a
number:"); scanf("%d",
&n); for (i = 1; i <= n;
i++)
{
fact = fact * i;
}
printf("Factorial of %d=%d", n, fact);
return 0;
}

OUTPUT

14.WAP to print the Fibonacci series.

#include <stdio.h>
int main() {
int a = 0, b = 1, c, k;
printf("Enter the turms to print:");
scanf("%d", &k);
printf("%d\n%d", a, b); int j = 1;

for (j = 1; j <= k - 2; j++)


{ c = a + b;
printf("\n%d", c);
a = b; b = c;
}
return 0;
}
​OUTPUT

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

#include <stdio.h>
int main()
{
int a, i, count = 0;
printf("Enter Any Number: ");
scanf("%d", &a);
for (i = 1; i <= a; i++)
{
if (a % i == 0)
{
count++;
}
} if (count
== 2)
printf("%d is Prime Number", a);
else
printf("%d is Not Prime Number", a);

return 0;
}

OUTPUT
16.WAP to find the sum of digits of the entered number.

#include <stdio.h>
int main()
{

int n, r, sum = 0;
printf("Enter Any Number: ");
scanf("%d", &n);
while (n > 0)
{
r = n % 10;
sum = sum + r;
n = n / 10;
}
printf("Sum Of Digits:%d", sum);
return 0;
}

OUTPUT

17. WAP to find the reverse of a number.


#include <stdio.h>
int main()
{ int n, r, revs =
0;
printf("Enter number: ");
scanf("%d", &n);
while (n != 0)
{
r = n % 10;
printf("%d", r);

n = n / 10;
}
return 0;
}

​OUTPUT

18. WAP to print Armstrong numbers from 1 to 100.

#include <stdio.h>
int main()
{
int i, n, r, arm = 0, a;
for (i = 1; i <= 100; i++)
{
arm = 0;
n = i; a = n;
while (n > 0)
{ r=
n % 10; n
= n / 10;
arm = arm + (r * r * r);
}
if (arm == a)
{
printf("%d : is Armstrong Number\n", a);
}
}
return 0;
}

​OUTPUT

19. WAP to convert binary number into decimal number and vice versa.

#include <stdio.h>
int main()
{
int n, rem, d = 0, base = 1;
printf("Enter Binary Noumber: ");
scanf("%d", &n); while (n > 0)
{
rem = n % 10;
d = d + rem * base;
n = n / 10; base =
base * 2;
}
printf("Decimal Number %d", d);
return 0;
}

​OUTPUT

20.WAP that simply takes elements of the array from the user and finds the sum
of these elements.

#include <stdio.h>
int main()

{
int a[6], i, sum = 0;
printf("Enter the array Element:");
for (i = 0; i < 6; i++)
{
scanf("%d", &a[i]);
}
printf("Sum of array element:");

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


{
sum = sum + a[i];
}
printf("%d", sum);
return 0;
}

OUTPUT
21.WAP that input two array and saves sum of corresponding elements of these
arrays in a third array and prints them.

#include <stdio.h>
int main()
{
int a[6], b[6], sum[6], i;

printf("Enter the first array Element:");


for (i = 0; i < 6; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the first array Element:");
for (i = 0; i < 6; i++)
{
scanf("%d", &b[i]);
}
for (i = 0; i < 6; i++)
{
sum[i] = a[i] + b[i];
printf("\nsum: %d=%d+%d", sum[i], a[i], b[i]);
}
return 0;
}
​OUTPUT

22.WAP to find the minimum and maximum element of the array.

#include <stdio.h>
int main()
{
int a[50], size, max, min, i;
printf("Enter the size array:");
scanf("%d", &size);
printf("Enter value of array:");
for (i = 0; i < size; i++)
{
scanf("%d", &a[i]);
}
max = a[0]; for (i =
0; i < size; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
printf("Maximum value of array: %d\n", max);
min = a[0];
for (i = 0; i < size; i++)
{
if (a[i] < min)
{
min = a[i];
}
}
printf("Minimum value of array: %d", min);
return 0;
} OUTPUT

23.WAP to search an element in a array using Linear Search.

#include <stdio.h>
int main()
{
int a[5], n, i, loc = 0, s;
printf("Enter the number of elements in array :\n");
scanf("%d", &n);
printf("Enter :%d number \n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the numbar to
search:\n"); scanf("%d", &s); for (i =
0; i < n; i++)
{
if (a[i] == s)
{
loc = i + 1;
break;
}
}

if (loc > 0)
printf("%d is present at location :%d\n", s, loc);
else

printf("%d is not present in array \n", s);


return 0; }

​OUTPUT

24.WAP to sort the element of the array in ascending order using Bubble Sort
technique.

#include <stdio.h>
int main()
{
int a[10] = {2, 5, 6, 7, 1, 9, 3, 7, 4, 2},
i, j, temp; for (i = 0; i < 10; i++)
for (i = 0; i < 10; i++)
{
for (j = i; j < 10; j++)
{
if (a[i] > a[j])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
printf("Asecending order array element: ");
for (i = 0; i < 10; i++)
{
printf("%d", a[i]);
}
return 0;
}

​OUTPUT

25.WAP to add and multiply two matrices of order nxn.//

#include <stdio.h>

int main()
{
int a[3][3], b[3][3], sum[3][3], multi[3][3], i, j, n,
k;
printf("Enetr the order of
the matix: \n");

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


printf("Enter the element
of matrix A:"); for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("The matrix A is: \n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
printf("Enter the element of matrix B:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("The matrix B is: \n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", b[i][j]);
}
printf("\n");
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Addition of the matrices: \n");
for
(i = 0; i < n; i++)

{
for (j = 0; j < n; j++)
{
printf(" %d", sum[i][j]);
}

printf("\n");
}
for (i =
0; i < n; i++)

{
for (j = 0; j < n; j++)
{
multi[i][j] = 0;
for (k = 0; k < n; k++)
{
multi[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Multiplication of the Matrices: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf(" %d", multi[i][j]);
}
printf("\n");
}
return 0;
}
​OUTPUT

26.WAP that find the sum of diagonal elements of a mxn matrix.


#include <stdio.h>
int main()
{
int a[10][10], i, j, m, n, b = 0, sum = 0;
printf("Enetr the order of the matix: \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", &a[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
for (i = 0; i < m; ++i)
{
sum = sum + a[i][i];
b = b + a[i][m - i - 1];
}
printf("\nThe sum of the main diagonal elements is: = %d\n", sum);
printf("The sum of the off diagonal elements is: = %d\n", b);
}
else
printf("The given order is not square matrix\n");
return 0;
}
​ OUTPUT

27.WAP to implement strlen(),strcat(),strcpy() using the concept of Functions.

#include <stdio.h>
#include <string.h>
int main()
{

int n;

char name[10] = "AMITKUMAR";


n = strlen(name);
printf("Number of Characters is :%d\n",
n); char firstname[5] = "AMIT"; char
lastname[6] = "KUMAR"; strcat(firstname,
lastname);
printf("Your full name is :%s\n", firstname);
strcpy(firstname, lastname); printf("Your first
name is :%s", firstname); printf("\n Your last
name is :%s", lastname); return 0;
}
​OUTPUT

28. 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 (sorted 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.

#include <stdio.h>
#include <string.h> struct
stu
{

int roll;
char name[50];
} st1, st2;
int main()
{
st1.roll = 101; strcpy(st1.name,
"Amit");

st2.roll = 102;
strcpy(st2.name, "Ankit");
printf("student 1 roll : %d\n", st1.roll);
printf("student 1 name : %s\n", st1.name);

printf("student 2 roll : %d\n", st2.roll);


printf("student 2 name : %s\n", st2.name);

return 0;
}

​OUTPUT

29.WAP to sawp two elements using the concept of pointer.

#include <stdio.h>

int swap(int *x, int *y)


{

int temp;
temp = *x;
*x = *y;

*y = temp;
}

int main()
{
int a,
b;

printf("Enter the two element:\n");


scanf("%d%d", &a, &b);
printf("\n\nBefore swaping :a=%d,b=%d\n", a,
b);

swap(&a, &b);
printf("\nAfter swaping :a=%d,b=%d", a,
b); return 0; }

​OUTPUT

30. Write a program to compare the contents of two files and determine
whether they are the same or not.

#include <stdio.h>
#include <string.h>

int main()
{

FILE *f_ptr_1, *f_ptr_2;


char path[256];
char string_1[20];
char string_2[20];
printf("Input first file path:
"); scanf("%s", path);
f_ptr_1 = fopen(path, "r");
if (f_ptr_1 == NULL)
{
printf("Error! Unable to open the file.");
return 1;
}
printf("Input second file path:
"); scanf("%s", path); f_ptr_2
= fopen(path, "r"); if (f_ptr_2 ==
NULL)
{
printf("Error! Unable to open the file.");
fclose(f_ptr_1);
return 1;
}
while ((!feof(f_ptr_1)) && (!feof(f_ptr_2)))
{

fscanf(f_ptr_1, "%s", string_1);


fscanf(f_ptr_2, "%s", string_2);
if (strcmp(string_1, string_2) != 0)
{
printf("The content of files are not the
same."); fclose(f_ptr_1);
fclose(f_ptr_2); return 0;
}
}
printf("The content of the files are the same.");
fclose(f_ptr_1);
fclose(f_ptr_2);

return 0;
}

​OUTPUT

31. Write a program to remove all the blank space from the string & print it, also
count the no of characters.

#include <stdio.h>

#include <string.h>

int main()

char str[50];
int char_count;

int len;
int i, j;

printf("Input String: ");

gets(str);

len = strlen(str);
i = 0;
for (i = 0; i <= len - 1; i++)
{ if (str[i] == ' ' || str[i] ==
'\t')
{
for (j = i; j <= len - 2;
j++) str[j] = str[j + 1];
len--; i--;
}
}
str[len] = '\0';

printf("New String after removing blank space: %s", str);

printf("\nNumber of characters: %d", len);


return 0;
}

​OUTPUT

You might also like