Test-1 Key 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

B.

Tech - Odd Sem : Semester in Exam-I


Academic Year:2020-2021
20SC1101 - Computational Thinking for Design
Scheme of Evaluation

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 numbern,reverse0,r0;
Step 4 : loop until n!=0
Step 4.1 r<-n%10
Step 4.2 reverses*10+r
Step 4.3 nn/10
Step 5: if(number==reverse)
Step 5.1 display palindrome
Else
Step 5.2 display not palindrome
Step 6 Stop

Code

#include <stdio.h> -----6.5marks


int main()
{
int n, reverse = 0, r, number;
printf("Enter an integer: ");
scanf(“%d”, &n);
number = n;
while( n!=0 )
{
r = n%10;
reverse = reverse*10 +r;
n /= 10;
}
if (number == reverse)
printf("\n%d is a palindrome\n", number);
else
printf("\n%d is not a palindrome\n", number);

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 angle(int,int,int); /* Function prototype*/

int main()

int a1,a2,a3,sum;

printf(" Enter the three angles of a triangle\n");


scanf("%d%d%d",&a1,&a2,&a3);

if(a1<=0||a2<=0||a3<=0)

printf("The triangle cannot be formed");

else

sum=angle(a1,a2,a3); /* Function call with three sides as arguments*/

if(sum==180)

printf(" The triangle can be formed");

else

printf("The triangle cannot be formed");

}}

int angle(int a,int b, int c) /* Function Definition*/

return(a+b+c);

Sample Input 1

Enter the three angles of a triangle

60

60

60

Sample Output

The triangle can be formed

Sample Input 2

60

0
60

The triangle cannot be formed

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

Enter array size:5

Enter array values:50 3 4 90 1

Total person in age range 50 to 60 is: 1

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

12 B. Design a module to accept temperature in centigrade and display a suitable message


according to temperature state below Temp < 0 then Freezing weather Temp 0-10 then Very
Cold weather Temp 10- 20 then Cold weather Temp 20-30 then Normal in Temp Temp 30-40
then Its Hot Temp >=40 then Its Very Hot
#include<stdio.h>
void display(int temp);
int main() 3M
{
int temp;
printf("enter temperature");
scanf("%d",&temp);
display(temp);
return 0;
}
void display(int temp) 3.5M
{
if (temp<0)
printf("freezing weather");
else if (temp>=0 && temp<10)
printf("very cold weather");
else if (temp>=10 && temp<20)
printf(" cold weather");
else if (temp>=20 && temp<30)
printf("Normal weather");
else if (temp>=30 && temp<40)
printf("Its hot weather");
else if (temp>=40)
printf("Its very hot");
}
Input Format:
25
Output Format:
Normal weather

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy