PSCP LAB (End Sem Question Bank)

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

NANDHA ENGINEERING COLLEGE (Autonomous), ERODE – 638 052

B.E/B.Tech DEGREE END SEMESTER EXAMINATIONS – MAY, 2023


22CSP01-PROBLEM SOLVING AND C PROGRAMMING LABORATORY

Question Bank

Q1. Write a program to compute the water tax using the ternary operator.

The water tax slab is as follows:


 If units are <100, then the cost is Rs.1 per unit.
 If 100>=units<=500, then the cost is Rs.1.5 per unit
 If units are> 500, then the cost is Rs.3 per unit.

Program:
#include<stdio.h>
#include<math.h>
int main()
{
float units=0;
scanf("%f",&units);
units=fabsf(units);
(units<=500)?((units<100)?printf("%.2f",units):printf("%.2f",units*1.5)):printf("%.2f",units*3);
return 0;
}

Q2.Bobby is attending a seminar on coding, which makes learning more interesting. During the
seminar session, he was given a task to find the frequency of a character. Help him find the frequency
of a character in a given string.
Input Format
The first line of input consists of a string.
The second line of input consists of the character to find its frequency.
Output Format
The output consists of the frequency of the character.
Refer to the sample input and output for format specifications.
Constraints
Characters are case-sensitive.
Program:
#include <stdio.h>
int main()
{
char str[1000], ch;
int count = 0;
fgets(str, sizeof(str), stdin);
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i)
{
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
Q3.Savira loves playing with numbers. She came across an interesting problem that involves
finding the sum of the digits of a given number. Can you help her solve it?

Write a program to take an integer as input and calculate the sum of its digits.

Input Format

The input consists of an integer N.

Output Format

The output displays the sum of the digits of the given integer.

Constraints

The value of 'n' will always be a non-negative integer (greater than or equal to zero).
The number of digits in 'n' will not exceed 9.

Program:

#include <stdio.h>
int main()
{
int n,r,sum=0;
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("%d",sum);
}

Q4.Mahi was teaching geometry, so she asked a student to find the third angle for the triangle. She
shared two values with the student. Help the student complete this task.

Input Format

The input consists of two integers in separate lines.

Output Format

The output prints the third angle of the triangle.

Program
#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d%d", &a, &b);
c = 180 - (a + b);
printf("%d", c);
return 0;
}

Q5. Rainfall is classified as per the table below.

Write a program to display the rainfall status based on the above information.
Program:
#include <stdio.h>
#include <stdlib.h>

int main()
{
double rainfall;
scanf("%lf",&rainfall);

if(rainfall<1)
printf("No Rain");
else if(rainfall<5)
printf("Light Rain");
else if(rainfall<10)
printf("Moderate Rain");
else
printf("Heavy Rain");
return 0;
}

Q6. Write a program that will cycle all elements of an array from one position to the left.
Input Format
The first line of the input consists of the value of n.
The next input is the array elements.
Output Format
The output prints the resultant array.
Note: There is an extra space at the end of the output.

Program:

#include <stdio.h>

int main()
{
int i,n;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
int temp = arr[0];
for(i=0;i<n;i++) {
arr[i] = arr[i+1];
}
arr[n-1]=temp;
for(i=0;i<n;i++) {
printf("%d ",arr[i]);
}
}

Q7. Write a program to get N integer inputs in an array.


Select an element from the array and count the following:
● The number of elements that are greater than the selected element
● The number of elements that are lesser than the selected element
● The number of elements that are exactly divisible by the selected element

Input Format

 The first line of input consists of the size of the array N.

 The second line of input consists of N elements of the array, separated by space.

 The third line of input consists of the selected element from the array.

Output Format

 The first line of output prints the number of elements greater than the selected element.

 The second line of output prints the number of elements less than the selected element.

 The third line of output prints the number of elements that are exactly divisible by the
selected element.

Refer to the sample output for the formatting specifications.


Constraints
2 <= N <= 30
Sample Input
12
12 23 45 56 78 89 98 87 65 54 32 21
32
Sample Output
Greater : 8
Lesser : 3
Exactly divisible : 1

Program:

#include<stdio.h>
int main()
{
int i,less=0,greater=0,input,count=0,n;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
scanf("%d",&input);
for(i=0;i<n;i++)
{
if(input<arr[i])
greater++;
}
for(i=0;i<n;i++)
{
if(input>arr[i])
less++;
}
for(i=0;i<n;i++)
{
if(arr[i]%input == 0)
count++;
}
printf("Greater : %d\n", greater);
printf("Lesser : %d\n", less);
printf("Exactly divisible : %d",count);
return 0;
}

Q8.Bobby is attending a seminar on coding, which makes learning more interesting. During the
seminar session, he was given a task to find the frequency of a character. Help him find the frequency
of a character in a given string.

Input Format

The first line of input consists of a string.

The second line of input consists of the character to find its frequency.

Output Format

The output consists of the frequency of the character.

Refer to the sample input and output for format specifications.

Constraints

Characters are case-sensitive.


Frequency of o = 2

Program:

#include <stdio.h>
int main()
{
char str[1000], ch;
int count = 0;
fgets(str, sizeof(str), stdin);
scanf("%c", &ch);

for (int i = 0; str[i] != '\0'; ++i)


{
if (ch == str[i])
++count;
}

printf("Frequency of %c = %d", ch, count);


return 0;
}

Q9.Given a string S consisting of upper- and lower-case English alphabets. change the case of each
letter in this string. That is, all the uppercase letters should be converted to lowercase, and all the
lowercase letters should be converted to uppercase. then print the resultant string to the output.

Input Format

The first line of input contains the String S.

Output Format

The output displays the resultant string on a single line.

Refer to the input and output for format specifications.

Constraints

1<=|S|<= 100, where S denotes the length of string S.


Program:
#include<stdio.h>
#include<string.h>
int main()
{
char str[101]; gets(str);
for(int i=0; i<strlen(str); i++)
{
if((int)str[i]>96)
{
printf("%c",(int)((int)str[i] - 32));
}
else if((int)str[i]<96)
{
printf("%c",(int)((int)str[i] + 32));
}
}
return 0;
}

Q10.Naren wants to add two numbers and print the result. To impress his teacher he used functions to
add the numbers. Write a program to obtain two numbers and add them using functions.
Input Format

The input consists of two numbers separated by a space.


Output Format

The output prints the sum of two numbers.


Program:
#include <stdio.h>
#include <stdlib.h>
int add(int x,int y)
{
return x+y;
}
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c=add(a,b);
printf("%d",c);
return 0;
}

Q11. Write a function to get input from the user as hours, minutes, and seconds and convert it into
decimal representation in terms of hours, minutes, and seconds.

Note: Use a suitable function


Input Format

The input consists of three integers separated by colons (:) representing hours, minutes, and seconds,
respectively.
Output Format

The output prints three decimal numbers representing the given time in terms of decimal hours,
decimal minutes, and decimal seconds, respectively.

Program:

#include <stdio.h>
#include <stdlib.h>

void convertTime(){
float hr,min,second;
scanf("%f:%f:%f", &hr,&min,&second);
float newhr = hr + (min/60.0) + (second/3600.0);
float newmin = (hr*60) + min + (second/60.0);
float newsecond = (hr*60*60)+ (min *60)+second;
printf("%f\n%f\n%f",newhr,newmin,newsecond);

}
int main()
{

convertTime();
return 0;
}
Q12.Write a program to accept 5 integers and print their average using functions and pointers.

Function Specification:

void average(int *a, int *b, int *c, int *d, int *e) This function prints the average with two precision
points.
Input Format

The input consists of five space separated integers.


Output Format

The output prints a float value representing the average of the five integers.
Sample Input Sample Output
1234 53.00

Program:

#include<stdio.h>
void average(int *a, int *b, int *c, int *d, int *e)
{
printf("%.2f", (*a+*b+*c+*d+*e)/5.0) ;
}
int main()
{
int a, b, c, d, e;
scanf("%d %d %d %d %d",&a, &b, &c, &d, &e);
average(&a, &b, &c, &d, &e);
return 0;
}

Q13. Mrs. Anitha, our favorite math teacher, wanted to teach her new batch of students to find the
sum of two numbers using pointers. In their first attempt, all her students computed the sum wrongly.
She taught them the concepts of pointers, pass by value, and pass by reference, and she again gave the
same problem to the students. All the students answered the problem correctly, and Mrs. Anitha was
happy. Write a program to accept two integers and calculate the sum of two numbers using functions
and pointers.

Note: Print the text "Sum =" inside the function.

Function Specification

void addition(int *a,int *b)

This function calculates the sum of two numbers.


Input Format

The input consists of 2 integers on separate lines.


Output Format

The output consists of the sum of the integers.


Program:

#include<stdio.h>
void addition(int *a,int *b)
{
printf("Sum = %d",*a+*b);
}
int main()
{
int a,b;
scanf("%d",&a);
scanf("%d",&b);
addition(&a,&b);
return 0;
}

Q14. Define a structure called Circle with the field radius. Write a program to read data for a
circle and display its area and circumference.

Input Format

The input consists of the radius of a circle.

Output Format

The output displays the area and circumference of the circle with the given radius in separate
lines.

Constraints

Use PI value as 3.14

#include <stdio.h>

#define PI 3.14

struct Circle{

float radius;

};

int main(){

struct Circle circle;

scanf("%f",&circle.radius);

float circumference = 2 * PI * circle.radius;

float area = PI * circle.radius * circle.radius;

printf("Circumference of the circle with radius %.2f is %.2f.\n", circle.radius, circumference);


printf("Area of the circle with radius %.2f is %.2f.", circle.radius, area);

return 0;

Q15.Write a C program to create a structure and union to store the name and salary of an
employee and print the size of both as output

Input Format

No Console input

Output Format

The output displays the size of the structure and union

Program:
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;

struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;

int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}

Q16. Write a c program to find the character "a" in the file and replace it with "z".

File name: input1.txt


Input Format
No console input.
Output Format
The output displays the desired output as mentioned in the question.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp;
fp = fopen("input1.txt","w");
fprintf(fp,"an apple a day keeps the doctor away");
fclose(fp);
FILE *fptr;
fptr = fopen("input1.txt","r");
char ch ;
char ch1 = 'a';
while((ch = getc(fptr)) != EOF) {
if(ch == ch1) {
printf("z");
}
else {
printf("%c",ch);
}
}
fclose(fptr);
return 0;
}

Q17. Write a C program to read the contents of the file.

File Name: input1.txt


Input Format

No console input
Output Format

The output displays the contents of the file.

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
fp =fopen("input1.txt","w");
fprintf(fp,"iamNeo is here to help in time of need. With the power of automation in hand,
spend your time effortlessly focusing on your core competencies. Primarily, create compelling
content with our simple user interface. This is followed by sharing the right content to the
right candidates to make sure they are learning and growing in the way you need them to. And
finally, assess their progress, with detailed reports to make the right decisions at the right
time.");
fclose(fp);

char c[1000];
FILE *fptr;
if ((fptr = fopen("input1.txt", "r")) == NULL) {
printf("Error! opening file");
exit(1);
}
fscanf(fptr, "%[^\n]", c);
printf("%s", c);
fclose(fptr);

return 0;
}

Q18.Write a C program to get input and print text using Dynamic Memory Allocation.

Input Format

The first line of input is an integer representing the text limit.

The second line of input is the text.

Output Format

The output prints the text using Dynamic Memory Allocation.

Sample Input
50
I am Mike from California, I am a computer geek.

Sample Output
I am Mike from California, I am a computer geek.

Program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
char *text;
scanf("%d",&n);
text=(char*)malloc(n*sizeof(char));
scanf(" "); /*clear input buffer*/
scanf("%[^\n]%*c", text);
printf("%s",text);

/*Free Memory*/
free(text);

return 0;
}
Q19.Write a program to calculate the sum of n numbers entered by the user using dynamic
memory allocation.

Note: Allocate the memory using calloc

Input Format

The first line of input consists of the number of array elements n.

The second line of input consists of n elements separated by space.

Output Format

The output prints the sum of array elements.

Constraints

1 <= n <= 100


Sample Input Sample Output
10
28 -11 0 61 -3 -2 4 -23 -23 46 77

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

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


{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("%d", sum);
free(ptr);
return 0;
}

Q.20 Sasikumar is a mathematics teacher, and he is preparing a question paper of his own, so
he needs to calculate the answers for the questions he prepared without any errors. As he is
busy with his work, he has no time to make it.

Write the program and trace the output for the following expressions:

a) x = a + b * 5 / 4 + c % 3 * 5, where a, b, and c are variables (inputs).


b) y = u >v ? u : v ; where u and v are variables (inputs).
c) z = ++i&& ++j && ++k; where i, j, and k are variables (inputs).

Program
#include<stdio.h>
int main()
{
int a=0,b=0,c=0,i=0,j=0,k=0,x=0,y=0,u=0,v=0,z=0;
scanf("%d%d%d%d%d%d%d%d",&a,&b,&c,&u,&v,&i,&j,&k);
x = a + b * 5 / 4 + c % 3 * 5;
printf("x=%d\n",x);
y = u >v ? u : v ;
printf("y=%d\n",y);
z = ++i&& ++j && ++k;
printf("z=%d\n",z);
return 0;
}

You might also like