PSCP LAB (End Sem Question Bank)
PSCP LAB (End Sem Question Bank)
PSCP LAB (End Sem Question Bank)
Question Bank
Q1. Write a program to compute the water tax using the ternary operator.
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
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
Output Format
Program
#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d%d", &a, &b);
c = 180 - (a + b);
printf("%d", c);
return 0;
}
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]);
}
}
Input Format
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.
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 second line of input consists of the character to find its frequency.
Output Format
Constraints
Program:
#include <stdio.h>
int main()
{
char str[1000], ch;
int count = 0;
fgets(str, sizeof(str), stdin);
scanf("%c", &ch);
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
Output Format
Constraints
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
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.
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 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.
Function Specification
#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
Output Format
The output displays the area and circumference of the circle with the given radius in separate
lines.
Constraints
#include <stdio.h>
#define PI 3.14
struct Circle{
float radius;
};
int main(){
scanf("%f",&circle.radius);
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
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".
#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;
}
No console input
Output Format
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
Output Format
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.
Input Format
Output Format
Constraints
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);
}
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:
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;
}