Solving PSP Question
Hello Guys, today i will be showing the problems and solution of my psp class.
1: Familiarization with computer hardware and programming environment, the concept of naming the program files, storing, compilation, execution, and debugging, taking any simple C-code.
i am sorry but idk how to.
moving on
2: Develop a program to solve simple computational problems using arithmetic expressions and use of each operator leading to the simulation of a commercial calculator. (No built-in math function)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
int main()
{
char opt;
int n1, n2, n3;
float res;
printf(" Select an operator (+, -, *, /): ");
scanf("%c", &opt);
printf(" Enter the first number: ");
scanf(" %d", &n1);
printf(" Enter the second number: ");
scanf(" %d", &n2);
if (opt == '+')
{
n3 = n1 + n2;
printf(" Addition of %d and %d is: %d", n1, n2, n3);
}
else if (opt == '-')
{
n3 = n1 - n2;
printf(" Subtraction of %d and %d is: %d", n1, n2, n3);
}
else if (opt == '*')
{
n3 = n1 * n2;
printf(" Multiplication of %d and %d is: %d", n1, n2, n3);
}
else if (opt == '/')
{
if (n2 == 0)
{
printf(" \n Divisor cannot be zero. Please enter another value ");
scanf("%d", &n2);
}
res = n1 / n2;
printf(" Division of %d and %d is: %.2f", n1, n2, res);
}
else
{
printf(" \n You have entered wrong inputs ");
}
}
3: Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <math.h>
int main()
{
float x1, x2, a, b, c, d;
printf("Input the value of a, b & c: ");
scanf("%f%f%f", &a, &b, &c);
d = (b * b) - (4 * a * c);
if (d == 0)
{
printf("Both roots are equal.\n");
x1 = -b / (2.0 * a);
x2 = x1;
printf("First Root = %f", x1);
printf("second Root = %f", x2);
}
else if (d > 0)
{
printf("Both Roots are real and diffent \n");
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
printf("First Root= %f \n", x1);
printf("Second Root= %f", x2);
}
else
{
printf("Roots are imaginary \n");
}
}
4: Develop a program to find the reverse of a positive integer and check for palindrome or not. Display appropriate messages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
int main()
{
int n, reversed = 0, r, orignal;
printf("Enter an integer: ");
scanf("%d", &n);
orignal = n;
while (n != 0)
{
r = n % 10;
reversed = reversed * 10 + r;
n /= 10;
}
printf("Reversed number = %d\n", reversed);
if (orignal == reversed)
printf("%d is a palindome.", orignal);
else
printf("%d is not a palindome.", orignal);
}
5: An electricity board charges the following rates for the use of electricity: for the first 200 units 80 Paise per unit: for the next 100 units 90 Paise per unit: beyond 300 units Rs 1 per unit. All users are charged a minimum of Rs. 100 as Meter charge. If the total amount is more than Rs 400, then an additional surcharge of 15% of total amount is charged. Write a program to read the name of the user, number of units consumed and print out the charges.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <conio.h>
void main()
{
char name[20];
float unit, price, extra;
printf("Enter Consumer's Name: ");
gets(name);
printf("Enter Units of electricity used:");
scanf("%f", &unit);
if (unit <= 200)
{
price = (80 * unit) / 100 + 100;
puts(name);
printf("Units = %.2f \nRate = 80 Paise/Unit \nMeter charges = 100 \nPrice = %.2f", unit, price);
}
else if (unit > 200 && unit <= 300)
{
extra = unit - 200;
price = (80 * unit) / 100 + 90 * extra / 100 + 100;
puts(name);
printf("\nUnits = %.2f \nRate = 80 Paise/Unit for first 200 Units and 90 Paise/Unit for next 100 Units \nMeter charges = 100 \nPrice = %.2f", unit, price);
}
else if (unit > 300 && unit <= 400)
{
price = unit + 100;
puts(name);
printf("\nUnits = %.2f \nRate = 1 Rupees/Unit \nMeter charges = 100 \nPrice = %.2f", unit, price);
}
else if (unit > 400)
{
price = unit + (0.15 * unit) + 100;
puts(name);
printf("\nUnits = %.2f \nRate = 1 Rupees/Unit \nExtra charges = 15% \nMeter charges = 100 \nPrice = %.2f", unit, price);
}
}
6: Introduce 1D Array manipulation and implement a Binary search.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter value to find\n");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low + high) / 2;
while (low <= high)
{
if (array[mid] < key)
low = mid + 1;
else if (array[mid] == key)
{
printf("%d found at location %d.\n", key, mid + 1);
break;
}
else
high = mid - 1;
mid = (low + high) / 2;
}
if (low > high)
printf("Not found! %d isn't present in the list.\n", key);
return 0;
}
7: Implement using functions to check whether the given number is prime and display appropriate messages. (No built-in math function)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
int main()
{
int n, i, r = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
r = 1;
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
r = 1;
break;
}
}
if (r == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
8: Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and ensure the rules of multiplication are checked.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
int 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");
}
return 0;
}
9: Develop a Program to compute Sin(x) using Taylor series approximation. Compare your result with the built-in Library function. Print both the results with appropriate messages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <math.h>
#define PI 3.142
int main()
{
int n, i;
float deg, x, sum = 0, term = 0;
printf("Enter number of terms\n");
scanf("%d", &n);
printf("Enter the degree\n");
scanf("%f", °);
x = (deg * PI) / 180;
printf("In radians = %f \n", x);
term = x;
sum = term;
for (i = 3; i <= n; i++)
{
term = (-term * x * x) / (i * (i - 1));
sum = sum + term;
}
printf("sin(%f)=%f\n", deg, sum);
printf("Inbuild funtion sin(%f) =%f\n", deg, sin(x));
printf("User function sin(%f) = %f", deg, sum);
}
10: Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <string.h>
int main()
{
int length(char str[100]);
int compare(char s1[100], char s2[100]);
void concat(char s1[100], char s2[100]);
int option, result;
char str[100], s1[100], s2[100];
do
{
printf("1.String length \n");
printf("2.string comparision \n");
printf("3.string concatenation \n");
printf("4.quit \n");
printf("enter your choice \n");
scanf("%d", &option);
switch (option)
{
case 1:
printf("enter string \n");
scanf("%s", &str);
result = length(str);
printf("the length of string= %d\n", result);
break;
case 2:
printf("enter 1st string\n");
scanf("%s", &s1);
printf("enter 2nd string\n");
scanf("%s", &s2);
result = compare(s1, s2);
if (result == 0)
{
printf("strings are equal \n");
}
else
{
printf("strings are not equal \n");
}
break;
case 3:
printf("enter two strings\n");
scanf("%s%s", s1, s2);
concat(s1, s2);
printf("result=%s \n", s1);
break;
}
} while (option <= 3);
return 0;
}
int length(char str[100])
{
int i = 0;
while (str[i] != '\0')
i++;
return (i);
}
int compare(char s1[100], char s2[100])
{
int i = 0;
while (s1[i] != '\0')
{
if (s1[i] > s2[i])
return (1);
else if (s1[i] < s2[i])
return (-1);
i++;
}
return 0;
}
void concat(char s1[100], char s2[100])
{
int i, j;
i = 0;
while (s1[i] != '\0')
i++;
for (j = 0; s2[j] != '\0'; i++, j++)
s1[i] = s2[j];
s1[i] = '\0';
}
11: Develop a program to sort the given set of numbers using Bubble sort.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
int main()
{
int n, j, i, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
int array[n];
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (array[j] > array[j + 1])
{
swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
return 0;
}
12: Develop a program to find the square root of a given number N and executes for all possible inputs with appropriate messages. Note: Don’t use library function sqrt(n).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include<math.h>
int main()
{
int n, i = 1, dr, dd, j = 1, d;
printf("Enter the number : ");
scanf("%d", &n);
while (i * i < n)
{
i = i + 1;
}
i = i - 1;
printf("%d.", i);
dr = 2 * i;
dd = n - (i * i);
while (j <= 5)
{
dd = dd * 100;
d = 0;
while ((dr * 10 + d) * d < dd)
{
d = d + 1;
}
d = d - 1;
printf("%d",d);
dd = dd - ((dr * 10 + d) * d);
dr = dr * 10 + 2 * d;
j = j + 1;
}
}
13: Implement structures to read, write, and compute average-marks and the students scoring above and below the average marks for a class of N students
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
struct student
{
int marks;
} st[10];
void main()
{
int i, n;
float total = 0, avgmarks;
printf("\nEnter the number of students in class:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter student %d marks :", i + 1);
scanf("%d", &st[i].marks);
}
for (i = 0; i < n; i++)
{
total = total + st[i].marks;
}
avgmarks = total / n;
printf("\nAverage marks = %.2f", avgmarks);
for (i = 0; i < n; i++)
{
if (st[i].marks >= avgmarks)
{
printf("\n student %d marks = %d above average", i + 1, st[i].marks);
}
else
{
printf("\n student %d marks = %d below average", i + 1, st[i].marks);
}
}
}
14: Develop a program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of n real numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[10], n, i, sum = 0, mean;
int *ptr = arr;
printf("Enter the number of elements you want to use: ");
scanf("%d", &n);
printf("\nEnter %d Elements:", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
for (i = 0; i < n; i++)
{
sum += *ptr;
*ptr++;
}
mean = sum / n;
printf("\nSum = %d\nmean = %d", sum, mean);
}
15: Implement Recursive functions for Binary to Decimal Conversion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
int BinaryToDecimal(int n)
{
if (n == 0)
return 0;
else
return (n % 10 + 2 * BinaryToDecimal(n / 10));
}
int main()
{
int n;
printf("Enter the Binary Value:");
scanf("%d", &n);
printf("Decimal Value of Binary number is: %d", BinaryToDecimal(n));
}