Test-1 Key 2
Test-1 Key 2
Test-1 Key 2
Question 1: John has n sweets which are to be distributed equally to m kids. Design the
flowchart such that it also prints number of sweets each kid gets and the remaining sweets
with john
4.5M
Question 2: Develop a C function to find the sum and average of 4 numbers using pointers as
arguments
#include<stdio.h>
void display(int *a,int *b,int *c,int *d)
{
int sum;
float avg;
sum= *a+*b+*c+*d;
avg= sum/4.0;
printf("sum of four numbers=%d\n",sum);
printf("average of four numbers=%f\n",avg);
}
int main()
{
int first, second,third,fourth, *a,*b,*c,*d, sum;
printf("Enter four numbers:\n");
scanf("%d%d%d%d", &first,&second,&third,&fourth);
display(&first,&second,&third,&fourth);
return 0;
}
input:
Enter four numbers:
10
2
11
5
output:
sum of four numbers=28
average of four numbers=7
Question-3: A library charges fine for every book returned late. For the first 5 days the fine is
Re 1/-, for 6-10 days the fine is Rs 2 and for above 10 days the fine is Rs 5. Translate the
above task to an algorithm
Step 1: Start
Step 2: Declare a variable “days”.
Step 3: Check if “days” is less than or equal to 5, if true continue or else false means go to step (5).
Step 4: Print fine is equal to 1 and go to step (10).
Step 5: Check if “days” is greater than or equal to 6 and less than or equal to 10, if true continue or
else false means go to step (7).
Step 6: Print fine is equal to 2 and go to step (10).
Step 7: Check “days” greater than or equal to 10 and less than or equal to 30, if true continue or false
means go to step (10).
Step 8: Print fine is equal to 5 and go to step (10).
Step 9: Print “Your Membership is Cancelled”.
Step 10: Stop
Question 4: Develop an algorithm and flowchart to print the first n terms of Fibonacci series
Algorithm
Step1: start
Step2: read no.of terms n value
Step3: assign f1=0 and f2=1
Step4: print f1,f2
Step5: i=1
Step6: repeat
Step7: f3=f1+f2
Step8: print f3
Step9: f1=f2
Step10: f2=f3
Step11: until i<=n-2
Step12: stop
Program:
#include<stdio.h>
int main()
{
int f1,f2,f3,n;
f1=0;
f2=1;
printf(“\nenter no.of terms”);
scanf("%d",&n);
printf("\n%d %d",f1,f2);
int i=1;
while(i<=n-2)
{
f3=f1+f2;
printf(" %d",f3);
f1=f2;
f2=f3;
i++;
}
return 0;
Question 5: You are the class representative of your section. You have records of all your
classmates in your file with the details of regno, name, section, and mobile number. Today a
new member joined in your class. Write a C function to add new student record to your file
#include<stdio.h>
void ADD();
struct student
{
int regno;
2M
char name[100];
char sec[10];
long int mob_no;
}s;
int main()
{
ADD(); 4M
return 0;
}
void ADD()
{
FILE *fp;
fp=fopen("siva.txt","a");
printf("enter Regno,name,sec,mon_no\n");
scanf("%d%s%s%ld",&s.regno,s.name,s.sec,&s.mob_no);
fprintf(fp, "%d %s %s %ld\n",s.regno,s.name,s.sec,s.mob_no);
6M
}
Question 6: Design an algorithm to check if a given number is palindrome. Translate the algorithm
to a C module
Algorithm to check if a given number is palindrome----- --------------6marks
Step 1: Start
Step2: read n;
Step 3 : initialize the numbern,reverse0,r0;
Step 4 : loop until n!=0
Step 4.1 r<-n%10
Step 4.2 reverses*10+r
Step 4.3 nn/10
Step 5: if(number==reverse)
Step 5.1 display palindrome
Else
Step 5.2 display not palindrome
Step 6 Stop
Code
return 0;
}
Question 7: Design a C module that checks whether a triangle can be formed or not considering
the angles as arguments. (Hint: In a triangle, the sum of all angles must be 180 degrees)
#include<stdio.h>
int main()
int a1,a2,a3,sum;
if(a1<=0||a2<=0||a3<=0)
else
if(sum==180)
else
}}
return(a+b+c);
Sample Input 1
60
60
60
Sample Output
Sample Input 2
60
0
60
Question-8: Given the values of first term (a), and the common difference (d), generate first
n terms in an Arithmetic Progression (AP). Design a modularized code for the specified task.
(Hint: The first n terms in AP are a, a+d, a+2d, a+3d, .. a+(n-1)d)
#include <stdio.h>
void print_ap(int a,int d,int n) 2M
{
int series;
if(n==0)
{
a=0;
d=0;
}
printf("Arithematic series upto %d terms =\t%d\t",n,a);
for(int i=1;i<n;i++){
series=a+(i*d);
printf("%d\t",series);
}
}
int main() 2M
{
int a,d,n;
printf("Enter First Term,a=");
scanf("%d",&a);
printf("Enter Common difference,d=");
scanf("%d",&d);
printf("Enter no of terms,n=");
scanf("%d",&n);
print_ap(a,d,n);
return 0;
}
Output: 0.5 M
Enter First Term,a=2
Enter Common difference,d=3
Enter no of terms,n=5
Arithematic series upto 5 terms = 2 5 8 11 14
Question-9:
A company pays car allowance to its employees based on the size of engine in the car. Those
employees whose car engines are less than or equal to 1500CC receive Rs 2/- per km and
those whose car engines are greater than 1500CC receive an additional 50 paise per km.
Develop a module that accepts the engine size and number of kilometers, calculates the car
allowance and returns the result.
#include<stdio.h>
float input();
float input() 4M
{
int eng_size;
float km,car_allw;
printf("Enter engine size and number of kilometers:");
scanf("%d %f",&eng_size,&km);
if(eng_size<=1500)
car_allw=2*km;
else
car_allw=2.50*km;
return car_allw;
}
int main() 4M
{
float allowance=input();
printf("Car Allowance=%f",allowance);
return 0;
}
Input
Enter engine size and number of kilometers: 1600 35
Output
Car Allowance=87.500000
Question 10: Develop a function that expects number of personsand their age stored in 1D
array and returns thenumber of persons in the age group of 50-60.
#include <stdio.h>
int age(int b[], int n) 4M
{
int count=0;
for(int i=0 ; i<n ; i++)
{
if(b[i]>=50 && b[i]<=60)
{
count++;
}
}
return count;
}
int main() 3M
{
int n,a[100],i;
printf("Enter array size:");
scanf("%d",&n);
printf("\nEnter array values:");
for(i=0; i<n ; i++)
{
scanf("%d",&a[i]);
}
int result=age(a,n);
printf("\nTotal person in age range 50 to 60 is: %d",result);
return 0;
}
Output: 1M
Question-11:
A) Develop a function that accepts an integer argument and prints the digits of n that divide
n. For example, if n is 122 then print 2,2,1 as 2 and 1 divide the number n. If none of the
digits divide the number, the print “No factors”. (6 Marks)
#include <stdio.h>
void find_factors(int n)
{
int r,m, flag =0;
m=n;
while(n>0)
{
r=n%10;
if ( m% r !=0) 2 marks.
{
flag=1;
break;
}
else
{
printf( “ %d \t”, r);
}
n=n/10;
}
if (flag==1) 2 marks.
printf(“ No Factores”)
}
int main()
{
int n;
printf(“Enter number”)
scanf(“%d”,&n);
find_factors( n); 2 marks.
return 0;
}
11.B) Design a module to accept a grade and declare the equivalent description Grade.
Description E Excellent V Very Good G Good A Average F Fail. (6.5 Marks)
#include <stdio.h>
void printf_grade (char g)
{
switch(g)
{
case ‘E’: printf(“Excellent \n”); 3 marks
break;
case ‘V’: printf(“Very Good\n”);
break;
case ‘G’: printf(“Good\n”);
break;
case ‘A’: printf(“Average\n”);
break;
default: printf( “ Kindly give proper Character”); 1.5 marks
}
}
int main()
{
char gd;
printf(“Enter Grade”)
scanf(“%c ”,&gd);
printf_grade( gd); 2 marks
return 0;
}
Question-12:
12 A. Design a function that accepts 2 integers n and k (n is assumed to be 5-digit number
and k is 1-digited). The function must return the number of occurrences of k in n.
#include<stdio.h>
int myfun(int n,int k);
int main() 3M
{
int n,k,count;
printf("enter n and k values");
scanf("%d%d",&n,&k);
count=myfun(n,k);
printf("%d occured %d times",k,count);
return 0;
}
int myfun(int n,int k) 3M
{
int c=0,r;
while(n!=0)
{
r=n%10;
if(r==k)
c++;
n=n/10;
}
return c;
}
Input Format:
1232 2
Output Format:
2 occurred 2 times