cbitplacements.files.wordpress.com€¦ · web viewautomata fix final push. q1. the function...
TRANSCRIPT
AUTOMATA FIX FINAL PUSH
Q1.
The function searchBST accepts the two arguments -root ,node representing the root node of the tree and key an integer represents value to be searched respectively.It supposed to return 'T'if key is found in the binary search Tree otherwise it returns '0'
int searchBST(node *root, int key)
{
if(root==NULL)
return 0;
if(root value == key)
return 1;
if(key>=root value)
return searchBST(root right,key);
else
return searchBST(root left,key);
}
int searchBST(node *root, int key)
{
if(root==NULL)
return 0;
if(root value == key)
return 1;
if(key<=root value)
return searchBST(root right,key);
else
return searchBST(root left,key);
}
int searchBST(node *root, int key)
{
if(root==NULL)
return 0;
if(root value == key)
return 1;
if(key==root value)
return searchBST(root right,key);
else
return searchBST(root left,key);
}
Q2.
The Function searchkeyindex accepts two arguments -list_head a node representing the head the node of the linked list and key an integer representing the key value that must be searched in the linked list it is suppose to return list of integers representing the index values of key in the linked list if the key value is present in the linked list otherwise it returns list of length of 1 with the content '-1'
boundedarray* searchkeyindex(node **list_head, int key)
{
boundedarray* res = (boundedarray*)malloc(sizeof(boundedarray))
Int flag=0,index=-1,count_index=0;
node* current = *list_head;
int *digit = (int *) malloc (100*sizeof(int));
while(current next!= NULL)
{
index++;
if(currentdata == key)
{
digit[count_index]=index;
count_index++;
flag=1;
}
else
current = currentnext
}
if(flag==1)
{
resarr = (int *) malloc (sizeof(int)*count_index);
for(i=0;i
res arr[i] = digit[i];
ressize++; }
return res; }
else {
resarr = (int *) malloc (1*sizeof(int));
resarr[0]=-1;
ressize++;
return res; }
boundedarray* searchkeyindex(node **list_head, int key)
{
boundedarray* res = (boundedarray*)malloc(sizeof(boundedarray))
Int flag=0,index=-1,count_index=0;
node* current = *list_head;
int *digit = (int *) malloc (100*sizeof(int));
while(current next!= NULL)
{
index++;
if(currentdata == key)
{
digit[count_index]=index;
count_index;
flag=1;
}
else
current = currentnext
}
if(flag==1)
{
resarr = (int *) malloc (sizeof(int)*count_index);
for(i=0;i
res arr[i] = digit[i];
ressize++; }
return res; }
else {
resarr = (int *) malloc (1*sizeof(int));
resarr[0]=-1;
ressize++;
return res; }
boundedarray* searchkeyindex(node **list_head, int key)
{
boundedarray* res = (boundedarray*)malloc(sizeof(boundedarray))
Int flag=0,index=-1,count_index=0;
node* current = *list_head;
int *digit = (int *) malloc (100*sizeof(int));
while(current next!= NULL)
{
index++;
if(currentdata == key)
{
digit[count_index++]=index;
count_index; // not given
flag=1;
}
else
current = currentnext
}
if(flag==1)
{
resarr = (int *) malloc (sizeof(int)*count_index);
for(i=0;i
res arr[i] = digit[i];
ressize++; }
return res; }
else {
resarr = (int *) malloc (1*sizeof(int));
resarr[0]=-1;
ressize++;
return res; }
Q3.
The function method oddsum accept three arguements -Start,End and list .An integer representing the starting and Ending index range list of values respectively .The function oddsum is supposed to return the sum of odd numbers in given range of values.
Sample InputSample Output
1
6
1
2
3
4
5
6
9
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q4.
The function CalculateGeneralHCF accepts the two arguments len,arr.an integer represents the length of the array and list of integers respectively.It suppose to calculate the and return the maximum element of the input array.Another function calculateHCF(int a,int b)return the HCF of two input numbers a and b.
Sample InputSample Output
10
1
2
3
4
5
6
7
8
9
10
9
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q5.
The function findindex() accepts the three arguements -size,list and key,an integer represents the size of the list ,list of integers ,an integer represents the value to search from the given list respectively.The function/metod findindex is supposed to return the index of the key value if the key is found in the list else return '-1'
Sample InputSample Output
5
10
20
30
40
50
30
2
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q6.
The function Evenmultiplication accept the two arguments size and list ,an integer represents the size of the list and a list of integers .The function/method Evenmultiplication is supposed to return an integer representing multiplicative value of all even integers present.
Sample InputSample Output
4
10
11
12
13
14
120
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q7.
The function CalculateGeneralMax Accepts the 2 arguments len and arr.Integer represents the length of array and list of integers respectively.Suppose to calculate and return the maximum element in the input array.The another function CalculateMax(int a,int b) returns the maximum element in the input array.
Sample InputSample Output
5
1
2
3
4
5
5
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q8.
Debug the code such that the result is the product of maximum 2 of 3 inputs:
int multiplyNumber(int a,int b,int c)
{
int result,min,max,mid;
max=((a>b)&&(a>c))&&((b>c)&&(b>a));
min=((a
mid=((a+b+c)-(min+max));
printf("%d %d",max,min);
result=(max*mid);
return result;
}
max=a>b&&a>c&&b>c&&b>a;
min=a
mid=a+b+c-min+max;
max=((a>b)&&(a>c))||((b>c)&&(b>a));
min=((a
mid=((a+b+c)-(min+max));
max=((a>b)||(a>c))&&((b>c)||(b>a));
min=((a
mid=((a+b+c)-(min+max));
max=((a>=b)&&(a>=c))?a:((b>=c)&&(b>=a))?b:c;
min=((a<=b)&&(a<=c))?a:((b<=c)&&(b<=a))?b:c;
mid=((a+b+c)-(min+max));
Q9.
Print the following pattern of n lines
where n (26>=n>=0)
Sample InputSample Output
3
a
ab
abc
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q10.
The function Countdigits(int num) is supposed to return the remainder when the input argument num is divided by the number of digits in num.
int Countdigits(int num)
{
int count=0;
while(num!=0)
{
num=num/10;
count=count+1;
}
return (num%count);
}
Input Format
Single line (Integer value)
Output Format
Single line (Integer)
Sample InputSample Output
45
1
Sample InputSample Output
54
0
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q11.
Debug the code such that the code prints the count of repeated element in the array
Input Format
First line is a integer - number of elements in the array - N
Next N lines is the elements of the array
Output Format
Single line of Integer (count)
Sample InputSample Output
5
1
2
2
3
5
1
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q12.
Analyze the following code and debug the syntax errors.
Find the greatest of three numbers.
#include
int main()
{
int num1, num2, num3;
scanf(“%d %d %d”, &num1,&num2,&num3);
if (num1 > num2) && (num1 > num3)
{
printf(“%d”, num1);
}
elseif(num2>num3)
{
printf(“%d”, num2)
}
else
{
printf(“%d”, num3);
}
return 0;
}
Input Format
3 integers
Output Format
Greatest integer
Sample InputSample Output
30
20
10
30
Sample InputSample Output
5
6
8
8
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q13.
The function getarraysum(int *arr,int len) is supported to calculation and return the sum of elements of the input array arr of length len(len>0).
The function compiles successfully but fails to return the desired result due to logical errors
int getarraysum(int *arr,int len)
{
int sum=0'
for(i=0;i
{
sum=arr[i];
}
return sum;
Your task is to fix the logical error and run the code
Input Format
The first line of input contains no.of elements in the array.
Second line contains the elements of the array.
Output Format
Sum of the elements.
Sample InputSample Output
6
10 20 30 40 50 60
210
Sample InputSample Output
3
10 20 30
60
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q14.
Given a program to print the binary equivalent of the given number. The code has logical error in the line below.Debug the program and execute it.
return (num * 2) + 10 / binary_conversion(num / 2);
Input Format
Positive integer
Output Format
Corresponding binary number
Sample InputSample Output
4
100
Sample InputSample Output
9
1001
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q15.
Analyze the code and find the logical error in the below code.
#include
int main () {
int i, j, n ;
scanf("%d", &n);
for(i=1; i
{
for(j=i;j
{
printf("%d", i);
}
printf("\n");
}
}
Input Format
Positive integer.
Output Format
Corresponding pattern
Sample InputSample Output
5
1111
222
33
4
Sample InputSample Output
7
111111
22222
3333
444
55
6
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q16.
Given a program to print a pyramid pattern.
But the code has logical errors in it. Debug the code and execute the program.
for (c = 1; c <= row - 1; c++)
printf("*");
Input Format
Positive integer
Output Format
Corresponding pattern
Sample InputSample Output
4
*
***
*****
*******
Sample InputSample Output
6
*
***
*****
*******
*********
***********
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q17.
Given a C program to find the sum of the elements in an array.
However, the given code has syntax/logical errors. Check for the errors and execute the program such that it works for all input values
#include
#include
int main()
{
int *a,i,n,sum=0;
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
for(i=0;i
{
scanf("%d",a+i);
}
for(i=0;i
{
sum = sum *(*(a-i));
}
printf("The sum of the elements in the array is %d",sum);
return 0;
}
Input Format
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.
Output Format
Refer sample output for details.
Sample InputSample Output
5
3
2
2
1
6
The sum of the elements in the array is 14
Sample InputSample Output
3
1
2
3
The sum of the elements in the array is 6
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q18.
Given a program that asks a user for their birth year encoded as two digits (like "62") and for the current year, also encoded as two digits (like "99"). The program is to correctly write out the users age in years.
The code is given below, but it has logical errors in it. Debug the code so as to pass all the test cases.
#include
int main()
{
int curr_year,b_year,age;
scanf("%d",&b_year);
scanf("%d",&curr_year);
if(curr_year>b_year)
{
age=(curr_year) +(b_year);
printf("Your age is %d",age);
}
else if(curr_year
{
age=(curr_year+100)+b_year;
printf("Your age is %d",age);
}
return 0;
}
Input Format
Input consists of 2 integers. The first integer corresponds to the last 2 digits of the birth year. The second integer corresponds to the last 2 digits of the current year.
Output Format
Refer sample input and output for further formatting specifications.
Sample InputSample Output
27
07
Your age is 80
Sample InputSample Output
99
21
Your age is 22
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q19.
The function printTable(int num)is supposed to print the first ten multiples of the multiplication table of the input number num.
The function compiles fine but fails to return the desired result for some test cases.
Your task is to fix the program so that it passes all the test cases.
void printTable(int num)
{
int value = 0;
for(int i=1;i<10;i++)
{
value = num * i ;
printf(“%d”, value);
}
}
Input Format
Positive Integer.
Output Format
Corresponding multiples of the integer.
Sample InputSample Output
2
2
4
6
8
10
12
14
16
18
20
Sample InputSample Output
10
10
20
30
40
50
60
70
80
90
100
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q20.
The function calculateMatrixSum(int **matrix, int m, int n) accepts a two-dimensional array matrix of dimensions m, n as an input and returns the sum of odd elements whose ith and jth index are the same.(m=n=3)
The function compiles fine but fails to return the desired result for some test cases.
Your task is to debug the program so that it passes all the test cases.
int calculateMatrixSum(int **matrix, int m, int n)
{
int i, j, sum=0,row = m, column = n;
if((row > 0) && (column > 0)){
for(i=0;i
sum=0;
for(j=0;j
if(i==j){
if(matrix[i][j]/2 != 0)
sum += matrix[(i)(j);;
}
}
} return sum;
}
else
return sum;
}
Input Format
3 X 3 matrix.
Output Format
Print the corresponding output.
Sample InputSample Output
1 1 1 1 1 1 1 1 1
3
Sample InputSample Output
1 2 3 4 5 6 7 8 9
15
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q21.
Given a program to check if a string is palindrome or not. The code has logical errors in it. Debug the code and execute the program.
while (text[length] = '\0')
length++;
end = length / 1;
middle = length*2;
Input Format
String
Output Format
Corresponding output.
Sample InputSample Output
mom
Palindrome.
Sample InputSample Output
string
Not a palindrome.
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q22.
Analyze the following and debug the errors.
#include
int main()
{
int number;
//printf("Enter a number \n");
scanf("%d", &number);
if (number >= 0);
printf("%d is a positive number \n", number);
else
printf("%d is a negative number \n", number);
}
Input Format
Integer
Output Format
Corresponding output.
Sample InputSample Output
9
9 is a positive number
Sample InputSample Output
-3
-3 is a negative number
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q23.
Analyze the code below and debug the program .
#include
int main()
{
int number;
//printf("Enter an integer: ");
scanf("%d", &number);
// True if the number is perfectly divisible by 2
if(number % 2 == 0);
printf("%d is even.", number);
else
printf("%d is odd.", number);
return 0;
}
Input Format
Positive integer.
Output Format
Corresponding output.
Sample InputSample Output
15
15 is odd.
Sample InputSample Output
18
18 is even.
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q24.
Analyze the following code and complete the missing code.
#include
int add(int,int);
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c=add(a,b);
printf("%d",c);
return 0;
}
int add(int a,int b)
{
// missing code
}
Input Format
Enter two numbers
Output Format
Print the sum of two numbers
Sample InputSample Output
10
2
12
Sample InputSample Output
5
6
11
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q25.
Given a program to find if a number is positive or negative. The code has syntax errors in it.Analyze the following and debug the errors.
scanf('%d', number);
Input Format
Integer
Output Format
Corresponding output.
Sample InputSample Output
9
9 is a positive number
Sample InputSample Output
-3
-3 is a negative number
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q26.
Analyze the code and fill the missing lines inorder to pass all the test cases.
#include
#include
int computeSum(int n)
{
// missing code
}
int main()
{
int n;
scanf("%d",&n);
computeSum(n);
printf("The sum of digits in %d is %d",n,computeSum(n));
return 0;
}
Input Format
Input is a non-negative integer.
Output Format
Refer sample input and output for formatting specifications.
Sample InputSample Output
123
The sum of digits in 123 is 6
Sample InputSample Output
1240
The sum of digits in 1240 is 7
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q27.
Analyze and debug the code to execute it.
#include
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n*10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger != reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}
Input Format
Positive integer
Output Format
Corresponding output
Sample InputSample Output
505
505 is a palindrome.
Sample InputSample Output
103
103 is not a palindrome.
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q28.
Given a program to rearrange positive and negative numbers in an array. Analyze the code and complete the missing code to execute the program.
void rearrange_alternate_positions(int arr[], int n)
{
// missing code
}
Input Format
First line contains the size of array, followed by the elements of the array.
Output Format
Original array and the rearranged array.
Sample InputSample Output
5
1
-2
-3
4
-5
Original array : 1 -2 -3 4 -5
Rearranged array : -2 -3 -5 1 4
Sample InputSample Output
4
-1
-8
9
6
Original array : -1 -8 9 6
Rearranged array : -1 -8 9 6
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q29.
Analyze the code and complete the missing code.
#include
void swap(int *a, int *b);
int main()
{
int m,n;
scanf("%d %d",&m,&n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
// missing code
}
Input Format
2 integers.
Output Format
Swapped integers.
Sample InputSample Output
10
20
20
10
Sample InputSample Output
30
40
40
30
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q30.
Given a program to find the maximum element in an array using recursion. Fill the missing lines of code so that the program passes through all the test cases.
#include
#include
int max=0;
int findMaxElement(int *arr,int size)
{
// missing code
}
int main()
{
int *arr,size,i;
scanf("%d",&size);
arr=(int *)malloc(size*sizeof(int));
for(i=0;i
{
scanf("%d",(arr+i));
}
findMaxElement(arr,size);
printf("Maximum element in the array is %d",findMaxElement(arr,size));
return 0;
}
Input Format
First line of input contains size of the array.
The next lines contain the elements of the array.
Output Format
Refer sample input and output for formatting specifications.
Sample InputSample Output
4
2
3
5
6
Maximum element in the array is 6
Sample InputSample Output
5
62
35
21
45
100
Maximum element in the array is 100
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q31.
Given a program to find the average of two numbers using pointers.
The code has logical/syntax errors in the lines below. Debug the program and execute it.
sum=sum + (p+i);
Sample InputSample Output
2
5
6
5.50
Sample InputSample Output
2
25
25
25.00
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q32.
Given a program to print the following pattern.
1
1 1
1 1 2
The code has syntax/logical errors in the lines below. Debug the program and execute it.
printf('%d\t',c);
Input Format
Positive integer.
Output Format
Corresponding pattern.
Sample InputSample Output
3
1
11
112
Sample InputSample Output
5
1
11
112
1123
11235
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q33.
Given a program to check if a number is perfect number or not. The code has logical error in it. Debug the program and execute it.
rem = number/ i;
if (rem != 0)
{
sum = sum++;
}
Input Format
Positive integer
Output Format
Corresponding output
Sample InputSample Output
25
Entered Number is not a perfect number
Sample InputSample Output
6
Entered Number is perfect number
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q34.
Given a program that determines the price of a movie ticket. The program asks for the customer's age and for the time on a 24-hour clock (where noon is 12.00 and 4:30 PM is 16.30). The show timings are 10.15, 13.30, 18.00 and 22.00. The normal adult ticket price is 8.00,howevertheadultmatineepriceis8.00,howevertheadultmatineepriceis5.00. Adults are those over 13 years. The normal children's ticket price is 4.00,howeverthechildren′smatineepriceis4.00,howeverthechildren′smatineepriceis2.00.
The code below has logical errors. Debug the code so as to pass through all the test cases.
#include
int main()
{
int age;
float time,time1;
scanf("%d",&age);
scanf("%f",&time);
time1=13.30;
if(age>13)
{
if(time!=time1)
{
printf("The ticket price is $5.00");
}
else
{
printf("The ticket price is $8.00");
}
}
else if(age<=13)
{
if(time!=time1)
{
printf("The ticket price is $2.00");
}
else
{
printf("The ticket price is $4.00");
}
}
return 0;
}
Input Format
The first input is an integer which corresponds to the age. The second integer is a float which corresponds to the show timing.
Output Format
Refer sample input and output for further formatting specifications.
Sample InputSample Output
10
10.15
The ticket price is $4.00
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q35.
A train of compartments are reserved only with the seat numbers like digits 4 and 7 such as 4, 7, 44, 47, 74,...... 44744,.. etc. Given a ticket K, we need to find K-th seat number in the train. The code is given below, fill the missing code such that it passes through all the test cases.
#include
int main()
{
int i,j,k,t,n,a[10000];
scanf("%d",&t);
for(j=0;j
{ scanf("%d",&n);
a[1]=4;
a[2]=7;
for (int i=3; i<=n; i++)
{
// missing code
}
printf("%d\n",a[n]);
}
}
Input Format
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer k.
Output Format
Print in a new line the Kth number in the series.
Sample InputSample Output
3
1
2
3
4
7
44
Sample InputSample Output
5
1
3
5
7
9
4
44
74
444
474
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q36.
The function checkGrade(int score) is supposed to return a students grade when the students test score is passed to it as an argument(o<=sore<=100)
Given a particular score a grade is calculated as per the following table
Score Grade
1.score>=90 A
2 .76<=score<=90 B
3 .61<=score<=75 C
4 score<=60 D
The function compiles successfully but fails to return the desired result due to logical errors.
Your task is to debug the program to pass all the test cases.
char checkGrade(int score)
{
if(score<=60)
return ‘D’;
else if((61<=score)||(score<=75))
return ‘C’;
else if((76<=score)||(score<=90))
return ‘B’;
else
return ‘A’;
}
Input Format
Positive integer
Output Format
Corresponding output
Sample InputSample Output
95
A
Sample InputSample Output
90
B
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q37.
Given a program to find the numbers divisible by 5 in the given range. Also find the sum of the numbers. But the code has syntax errors in it.Debug the code and execute the program.
if (i % 5 == 0)
{
printf('%3d ', &i);
count++;
sum = sum + i;
}
Input Format
Range to find out the numbers divisible by 5
Output Format
First line contains the numbers divisible by 5 in the given range.
Second line contains the sum of numbers divisible by 5.
Sample InputSample Output
1
20
5 10 15
30
Sample InputSample Output
21
40
25 30 35
90
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q38.
The seats are arranged in the lab in a rectangular fashion with m rows and n columns. The students are seated in the lab according to their roll numbers in row wise order.
Given the roll number of a student, the code is given to find whether the student is seated in the first row or in the 1st or last column. But the code has syntax errors. Debug the code without changing the logic.
#include
int main()
{
int rows,columns,rnum,d,e;
scanf("%d",&rows);
scanf("%d",&columns);
scanf("%d",&rnum);
d=rows*columns;
e=d-rows;
if((rnum%rows==1)||(rnum<=rows)||(rnum>e)
printf("yes");
}
else
{
printf("no");
}
return 0;
}}
Input Format
Refer sample input and output.
Output Format
The output consists of a string --- “yes” or “no”.
Sample InputSample Output
5
10
41
yes
Sample InputSample Output
5
10
12
no
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q39.
Given a program to convert a number from binary to octal.The code has logical errors in it.Debug the code and execute it.
remainder = binarynum *10;
octalnum = octalnum + remainder / j;
j = j * 2;
binarynum = binarynum - 10;
Input Format
Binary number
Output Format
Octal number
Sample InputSample Output
1101
Equivalent octal value: 15
Sample InputSample Output
1001
Equivalent octal value: 11
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q40.
Given a program to put the even and odd elements of array in 2 separate arrays. The code has logical errors in the lines below. Debug the code and execute the program.
if (ARR[i] /2 == 0)
{
EAR[j] = ARR[i];
j--;
}
else
{
OAR[k] = ARR[i];
k++;
}
Input Format
First line of input contains size of the array.
Next lines contain the elements of array.
Sample InputSample Output
4
11
12
13
14
The odd elements are
11
13
The even elements are
12
14
Sample InputSample Output
5
2
4
3
1
6
The odd elements are
3
1
The even elements are
2
4
6
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q41.
Roy bought a new bike last Sunday. He needs to decide the number plate number now. Though he likes 0’s and 1’s most out of number system, he wants his number plate to start with 1. Lets help him in finding out how many unique permutations start with 1 when he is given with N 0’s and M 1’s.
The code is given below. Complete the missing lines of code to execute the program.Try printing the answer modulo (109+7).
#include
#define MOD 1000000007
long long fact(int n,int m)
{
// missing code
}
int main(void) {
// your code goes here
int t,N,M;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&N,&M);
long long ans=fact(N,M-1);
printf("%lld\n",ans);
}
return 0;
}
Input Format
First line contains T, the number of test cases.
Each test case consists of N and M separated by a space.
Output Format
For each test case, print the answer modulo (109+7).
Sample InputSample Output
2
1 2
2 1
2
1
Sample InputSample Output
2
1 1
2 3
1
6
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q42.
Me and my friend always play a game. For every integer X I give, my friend has to find the smallest positive integer Y such that X * Y ( X multiplied by Y) contains only 4s and 0s and starts with one or more 4s followed by zero or more 0s. For example, 404 is an invalid number but 4400, 440, and 444 are valid numbers.
If a is the number of 4s and b is the number of 0s, can you print the value of (2 x a) + b ?
The code is given below, but it has logical errors in it.Debug and execute the code.
#include
#include
#include
#include
int main() {
int t,x,i,ans,out=0,multiple=0,j,k,power,n4,n0;
scanf("%d", &t);
for(;t>0;t-=1) {
scanf("%d",&x);
power = 1;
multiple=0;
out=0;
k=1;
n0=1;
while(1) {
for(i=power,j=1;j<=k;i=i/10,j+=1) {
multiple += (4*i);
if((multiple%x)!=0) {
out=1;
break;
}
}
if(out==1) {
break;
}
power/=10;
k+=1;
multiple = 0;
n0+=1;
}
ans = (2*j) + n0-j;
printf("%d\n",ans);
}
return 0;
}
Input Format
The first line of input contains a single integer T, the number of test cases.
T lines follow, each line containing the integer X as stated above.
Output Format
For every X, print the output (2 x a) + b in a newline as stated in the problem statement.
Sample InputSample Output
2
2
10
2
3
Sample InputSample Output
3
4
4
10
2
2
3
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q43.
Analyze the following code and fill the missing lines to find if an element is present in the array or not.
#include
#include
int *createArray(int);
void readElement(int *,int);
void Array(int *,int);
int main()
{
int *a,n;
scanf("%d",&n);
a=createArray(n);
readElement(a,n);
Array(a,n);
return 0;
}
int *createArray(int n)
{
int *a;
a=(int *)malloc(n*sizeof(int));
return a;
}
void readElement(int *a,int n)
{
int i;
for(i=0;i
{
scanf("%d",a+i);
}
}
void Array(int *a,int n)
{
// missing lines
}
Input Format
Input consists of n+2 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. The last integer corresponds to ‘a’, the element to be searched.
Output Format
Refer sample output for details.
Sample InputSample Output
5
12
23
34
45
56
12
12 is present in the array
Sample InputSample Output
6
1
2
3
4
5
6
7
7 is not present in the array
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q44.
Analyze the code and complete the missing code to print the zigzag pattern of array.
#include
void swap(int *x, int *y)
{
// missing code
}
int main()
{
int n;
//printf(“\nEnter the number of elements : ” );
scanf("%d", &n);
int arr[n], i;
//printf(“\nInput the array elements : “);
for(i=0; i
scanf("%d", &arr[i]);
zig_zag_fashion(arr, n);
for (int i=0; i
printf("%d", arr[i]);
printf("\n");
return 0;
}
Input Format
First line of input contains size of the array.
Second line contains the elements of the array
Output Format
Corresponding pattern.
Sample InputSample Output
6
1
2
3
4
5
6
132546
Sample InputSample Output
5
5
4
3
2
1
45231
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q45.
Given a code to print the sum of 'n' natural numbers. The code has logical errors in the lines below. Debug the code and execute it.
for(i=1; i <= n; ++i);
{
sum -= i;
}
Input Format
Positive integer
Output Format
Corresponding output
Sample InputSample Output
22
253
Sample InputSample Output
14
105
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q46.
Given a program to replace a substring in a string. Complete the missing code and execute the program.
void replaceSubstring(char [],char[],char[])
{
// missing code
}
Input Format
First line of input contains a string.
Second line contains the substring of the given string.
Third line contains a new string which should be replaced with the substring.
Output Format
Corresponding output.
Sample InputSample Output
schoolbus
bus
van
schoolvan
Sample InputSample Output
housetown
house
home
hometown
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q47.
Given a program to find the index of equilibrium element in the given array. In an array equilibrium element is the one where the sum of all the elements to the left side is equal to the sum of all the elements in the right side. Complete the missing lines of code and execute the program.
int findEquilibriumIndex(int a[], int n)
{
// missing code
}
Input Format
First line contains the size of the array.
Next lines contain the elements of array.
Output Format
Corresponding output.
Sample InputSample Output
6
1
2
3
4
3
3
4
Sample InputSample Output
3
2
1
2
1
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q48.
Given a program to find the factors of a number. The program has logical errors in the lines below. debug the program and execute it.
for(i=1; i <= number; ++i);
{
if (number*i = 0)
{
printf("%d ",i);
}
}
Input Format
Positive integer
Output Format
Factors of the number
Sample InputSample Output
12
Factors of 12 are: 1 2 3 4 6 12
Sample InputSample Output
5
Factors of 5 are: 1 5
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q49.
Given a program to find the second smallest element of the array. But the code has logical/syntax errors in it. Debug the program and execute the code.
if(arr[i] < second_min &arr[i] == min)
Input Format
First line contains the size of the array.
The next lines are followed by the elements of the array.
Output Format
Second smallest element
Sample InputSample Output
3
12
3
13
12
Sample InputSample Output
5
10
12
13
14
11
11
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q50.
Given a program to find the nth term in the Fibonacci series using recursion.
Fill the missing code so as to pass through all the test cases.
Note that the first 2 terms in the Fibonacci Series are 0 and 1.
#include
#include
int fib(int n)
{
// missing code
}
int main()
{
int n;
scanf("%d",&n);
fib(n);
printf("The term %d in the fibonocci series is %d",n,fib(n));
return 0;
}
Input Format
Input consists of an integer.
Output Format
Refer sample input and output for formatting specifications.
Sample InputSample Output
5
The term 5 in the fibonocci series is 3
Sample InputSample Output
3
The term 3 in the fibonocci series is 1
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q51.
Given a program to find the product of 2 numbers using recursion. The code has logical errors in the line below. Debug the code an execute the program.
return (a * product(a, b +1));
Input Format
2 positive integers.
Output Format
Product of two numbers
Sample InputSample Output
4
5
20
Sample InputSample Output
10
30
300
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q52.
Given a program to find the frequency of each element of array . But the code below has syntax errors in it. Debug the program and execute it.
scanf(“%d”, size);
Input Format
First line of input contains size of the array.
Next lines contain the elements of the array.
Output Format
Corresponding output.
Sample InputSample Output
5
1
1
2
3
4
1 occurs 2 times
2 occurs 1 times
3 occurs 1 times
4 occurs 1 times
Sample InputSample Output
3
1
1
1
1 occurs 3 times
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q53.
Nikki has a row of N white tiles indexed from 1 to N. This time, she's painting them blue!
The code is given to find the number of ways Nikki can paint certain tiles in green so that the indices of the blue tiles form an Arithmetic Progression. As this value can be quite large, your answer must be modulo 109 + 7.
Note: Nikki must paint at least 1 tile.
The code below has syntax errors. Debug the program so as to pass through all the test cases.
#include
#include
#include
#include
int main() {
int t;
scanf("%d",&t);
for(int index = 0;index < t;index++){
int N;
scanf("%d",&N);
int count = N;
for(int i = 0;i < N;i++);{
for(int j = i+1;j < N;j++);{
int d = j - i;
int tot = (N - i - 1)/d;
count += tot;
}
}
printf("%d\n",count);
}
return 0;
}
Input Format
The first line contains a single integer, T , denoting the number of test cases.
Each test case consists of a single line containing an integer, N, denoting the length of row of tiles.
Output Format
On a new line for each test case, print the number of ways Nikki can paint her white tiles blue so that the indices of the blue tiles form an Arithmetic Progression. Because this number can be quite large, your answer must be modulo (109 + 7).
Sample InputSample Output
3
1
2
3
1
3
7
Sample InputSample Output
5
3
6
9
8
7
7
33
86
65
48
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q54.
The function patternPrint(int n) supposed to print n number of lines in the following pattern
For n=4 the pattern should be:
1
1 1
1 1 1
1 1 1 1
The function complies successfully but fails to return the desired results due to logical errors Your task is to debug the program to pass all the test cases
Input Format
Integer
Output Format
Corresponding pattern
Sample InputSample Output
4
1
11
111
1111
Sample InputSample Output
5
1
11
111
1111
11111
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q55.
Analyze the code and find out the syntax error without changing the logic.
#include
int main()
{
int a,b;
scanf("%d",&a);
scanf("%d",&b);
if(a==b)
{
printf("%d is equal to %d",a,b)
}
else if(a>b)
{
printf("%d is greater than %d",a,b);
}
if(a
{
printf("%d is less than %d",a,b);
}
}
Input Format
Input consists of 2 integers.
Output Format
Refer sample input and output for formatting specifications.
The first two lines corresponds to the input and the third line corresponds the output.
Sample InputSample Output
6
9
6 is less than 9
Sample InputSample Output
100
2
100 is greater than 2
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q56.
In these days kids are introduced to computers at a very early age and in some schools the dictation test is conducted using computers. The teachers found it a bit difficult to evaluate these tests and they requested the school management to lessen their burden by automating this task. The 12th class students are learning C programming and they took up the task of automating the dictation evaluation. Here is the code for evaluation, but it has logical/syntax errors. Check the errors and debug the code.
#include
#include
int main()
{
char a[50],b[50];
scanf("%s",a);
scanf("%s",b);
if(strcpy(a,b)!=0)
{
printf("It is correct");
}
else
{
printf("It is wrong");
}
return 0;
}
Input Format
Input consists of a two strings. Assume that the maximum length of the string is 50 and it contains only alphabets.
Output Format
Refer sample input and output for formatting specifications.
Sample InputSample Output
hello
hello
It is correct
Sample InputSample Output
super
superb
It is wrong
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q57.
Given a program to find Second largest Number in an Array . The code has logical errors in the lines below. Debug the code and execute the program.
else if(arr[i] < second ||arr[i] > first)
Input Format
First line of input is the size of the array.
The next lines are followed by the elements of the array.
Output Format
Corresponding output.
Sample InputSample Output
5
1
2
3
4
5
4
Sample InputSample Output
4
6
7
3
9
7
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q58.
#include
int main()
{
int number;
//printf("Enter a number \n");
scanf("%d", &number);
if (number >= 0)
printf("%d is a positive number \n", number);
else
printf("%d is a negative number \n", number);
}
Input Format
Input consists of a single character.
Output Format
Output consists of a string --- “Vowel” / “Consonant” / “Not an alphabet”
Refer sample input and output for formatting specifications.
Sample InputSample Output
a
Vowel
Sample InputSample Output
Z
Consonant
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q59.
The code below has syntax errors. Debug the program to print the given pattern.(Refer to sample output)
#include
int main()
{
int i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++);
{
if(j%2==1)
printf("1 ")
else
printf("0 ");
}
printf("\n");
}
return 0;
}
Input Format
Input consists of a single integer.
Output Format
Refer sample output. There is a trailing space at the end of each line.
Sample InputSample Output
5
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
Sample InputSample Output
4
1
1 0
1 0 1
1 0 1 0
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q60.
Analyze the code below and debug the syntax errors.
Find the factorial of a given number.
#include
int main()
{
long int fact = 1, n, i;
scanf(“%d”, &n);
for(i =1; i <= n; i++)
{
fact = fact * i;
}
printf(“%d”, fact);
return 0;
}
Input Format
Positive Integer
Output Format
Factorial
Sample InputSample Output
5
120
Sample InputSample Output
8
40320
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Answer Key & Solution
Section 1 - Automata Fix
Q1
int searchBST(node *root, int key)
{
if(root==NULL)
return 0;
if(root value == key)
return 1;
if(key>=root value)
return searchBST(root right,key);
else
return searchBST(root left,key);
}
Solution
No Solution
Q2
boundedarray* searchkeyindex(node **list_head, int key)
{
boundedarray* res = (boundedarray*)malloc(sizeof(boundedarray))
Int flag=0,index=-1,count_index=0;
node* current = *list_head;
int *digit = (int *) malloc (100*sizeof(int));
while(current next!= NULL)
{
index++;
if(currentdata == key)
{
digit[count_index]=index;
count_index++;
flag=1;
}
else
current = currentnext
}
if(flag==1)
{
resarr = (int *) malloc (sizeof(int)*count_index);
for(i=0;i
res arr[i] = digit[i];
ressize++; }
return res; }
else {
resarr = (int *) malloc (1*sizeof(int));
resarr[0]=-1;
ressize++;
return res; }
Solution
No Solution
Q3Test CaseInputOutput
6
10
6
7
8
9
10
16
Weightage - 100Sample InputSample Output
1
6
1
2
3
4
5
6
9
Solution
Header
#include
int oddsum(int start, int end, int *list)
{
//write your code here
int sum=0;
int i=start;
while(i
{
if(list[i]%2!=0)
{
sum+=list[i];
}
i++;
}
return sum;
Footer
}
int main(void) {
int list[100],start,end,i,a;
scanf("%d",&start);
scanf("%d",&end);
for(i=start;i<=end;i++)
{
scanf("%d",&list[i]);
}
printf("%d",oddsum(start,end,list));
return 0;
}
Q4Test CaseInputOutput
5
20
15
20
15
10
10
Weightage - 100Sample InputSample Output
10
1
2
3
4
5
6
7
8
9
10
9
Solution
Header
#include
int calculateHCF(int a, int b)
{
if(a==0)
return b;
return calculateHCF(b%a,a); }
int calculateGeneralHCF(int len, int *arr)
{
//write your code here
int result;
for (int i=0;i
result = calculateHCF(arr[i],arr[i+=1]);
return result;
Footer
}
int main(void) {
int list[100],size,i,a;
scanf("%d",&size);
for(i=1;i<=size;i++)
{
scanf("%d",&list[i]);
}
printf("%d",calculateGeneralHCF(size,list));
return 0;
}
Q5Test CaseInputOutput
3
4
6
8
5
-1
Weightage - 100Sample InputSample Output
5
10
20
30
40
50
30
2
Solution
Header
#include
int findindex(int size, int* list, int key)
{
//write your code here
for(int i=0;i
{
if(list[i]==key)
{
return i;
}}
return -1;
Footer
}
int main(void) {
int list[100],size,key,i,a;
scanf("%d",&size);
for(i=0;i
{
scanf("%d",&list[i]);
}
scanf("%d",&key);
printf("%d",findindex(size,list,key));
return 0;
}
Q6Test CaseInputOutput
3
12
14
15
168
Weightage - 100Sample InputSample Output
4
10
11
12
13
14
120
Solution
Header
#include
int evenmulti(int size, int *list)
{
//write your code here
int em=1;
for(int i=0;i
{
if(list[i]%2 ==0)
{
em*=list[i];
}
}
return em;
Footer
}
int main(void) {
int list[100],size,key,i,a;
scanf("%d",&size);
for(i=0;i
{
scanf("%d",&list[i]);
}
printf("%d",evenmulti(size,list));
return 0;
}
Q7Test CaseInputOutput
5
1
10
20
30
40
40
Weightage - 100Sample InputSample Output
5
1
2
3
4
5
5
Solution
Header
#include
int calculatemax(int a, int b)
{
if(a>b)
return a;
else
return b;
}
int calculateGeneralmax(int len, int *arr)
{
//write your code here
int maximum = arr[0];
for (int c = 0; c <=len ; c++)
{
maximum = calculatemax(maximum,arr[c]);
}
return maximum;
Footer
}
int main(void) {
int list[100],size,i,a;
scanf("%d",&size);
for(i=1;i<=size;i++)
{
scanf("%d",&list[i]);
}
printf("%d",calculateGeneralmax(size,list));
return 0;
}
Q8
max=((a>=b)&&(a>=c))?a:((b>=c)&&(b>=a))?b:c;
min=((a<=b)&&(a<=c))?a:((b<=c)&&(b<=a))?b:c;
mid=((a+b+c)-(min+max));
Solution
#include
int multiplyNumber(int a,int b,int c);
int main()
{
int x,y,z,s;
scanf("%d%d%d",&x,&y,&z);
printf("%d",multiplyNumber(x,y,z));
}
int multiplyNumber(int a,int b,int c)
{
int result,min,max,mid;
max=((a>=b)&&(a>=c))?a:((b>=c)&&(b>=a))?b:c;
min=((a<=b)&&(a<=c))?a:((b<=c)&&(b<=a))?b:c;
mid=((a+b+c)-(min+max));
//printf("%d %d",max,min);
result=(max*mid);
return result;
}
Q9Test CaseInputOutput
5
a
ab
abc
abcd
abcde
Weightage - 50InputOutput
2
a
ab
Weightage - 50Sample InputSample Output
3
a
ab
abc
Solution
Header
#include
void printCharacterPattern(int n);
int main()
{
int z;
scanf("%d",&z);
printCharacterPattern(z);
}
void printCharacterPattern(int n)
{
int i,j,value=1;
char ch='a';
char print;
for(i=0;i
{
print=ch;
for(j=0;j<=i;j++)
{
printf("%c",print++);
}
printf("\n");
}
Footer
}
Q10Test CaseInputOutput
456
0
Weightage - 50InputOutput
46598
3
Weightage - 50Sample InputSample Output
45
1
Sample InputSample Output
54
0
Solution
Header
#include
int Countdigits(int num);
int main() {
int n,c;
scanf("%d",&n);
c=Countdigits(n);
printf("%d",c);
return 0;
}
int Countdigits(int num)
{
int count=0,tem;
tem=num;
while(num!=0)
{
num=num/10;
count=count+1;
}
return tem%count;
Footer
}
Q11Test CaseInputOutput
2
1
1
0
Weightage - 25InputOutput
5
4
4
4
4
4
4
Weightage - 50InputOutput
4
1
2
2
3
1
Weightage - 25Sample InputSample Output
5
1
2
2
3
5
1
Solution
Header
#include
int sameElementCount(int *arr,int len);
int main() {
int len,a[50],i,c=0;
scanf("%d",&len);
for(i=0;i
{
scanf("%d",&a[i]);
}
c=sameElementCount(a,len);
printf("%d",c);
return 0;
}
int sameElementCount(int *arr,int len)
{
int i,count=0;
for(i=0;i
{
if((arr[i]%2==0)&&(arr[i]==arr[i+1]))
count++;
}
return count;
Footer
}
Q12Test CaseInputOutput
100
200
300
300
Weightage - 40InputOutput
25
75
50
75
Weightage - 20InputOutput
40
80
60
80
Weightage - 20InputOutput
5
15
10
15
Weightage - 10InputOutput
1
2
3
3
Weightage - 10Sample InputSample Output
30
20
10
30
Sample InputSample Output
5
6
8
8
Solution
#include
int main()
{
int num1, num2, num3;
scanf("%d %d %d", &num1,&num2,&num3);
if ((num1 > num2) && (num1 > num3))
{
printf("%d", num1);
}
else if(num2>num3)
{
printf("%d", num2) ;
}
else
{
printf("%d", num3);
}
return 0;
}
Q13Test CaseInputOutput
5
10 10 10 10 10
50
Weightage - 10InputOutput
3
11 22 33
66
Weightage - 10InputOutput
5
120 240 360 480 600
1800
Weightage - 20InputOutput
6
25 50 75 100 125 150
525
Weightage - 20InputOutput
6
1470 2580 3690 7410 8520 9630
33300
Weightage - 40Sample InputSample Output
6
10 20 30 40 50 60
210
Sample InputSample Output
3
10 20 30
60
Solution
#include
int getarraysum(int *arr,int len)
{
int sum=0;
int i;
for(i=0;i
{
sum=sum+arr[i];
}
return sum;
}
main()
{
int arr[5];
int len;
scanf("%d",&len);
for(int i=0;i
{
scanf("%d",&arr[i]);
}
printf("%d",getarraysum(arr,len));
}
Q14Test CaseInputOutput
100
1100100
Weightage - 40InputOutput
50
110010
Weightage - 20InputOutput
10
1010
Weightage - 10InputOutput
25
11001
Weightage - 20InputOutput
14
1110
Weightage - 10Sample InputSample Output
4
100
Sample InputSample Output
9
1001
Solution
Header
#include
int binary_conversion(int);
int main()
{
int num, bin;
//printf("Enter a decimal number: ");
scanf("%d", &num);
bin = binary_conversion(num);
printf(" %d\n", bin);
}
int binary_conversion(int num)
{
if (num == 0)
{
return 0;
}
else
{
return (num % 2) + 10 * binary_conversion(num / 2);
Footer
}
}
Q15Test CaseInputOutput
25
111111111111111111111111
22222222222222222222222
3333333333333333333333
444444444444444444444
55555555555555555555
6666666666666666666
777777777777777777
88888888888888888
9999999999999999
101010101010101010101010101010
1111111111111111111111111111
12121212121212121212121212
131313131313131313131313
1414141414141414141414
15151515151515151515
161616161616161616
1717171717171717
18181818181818
191919191919
2020202020
21212121
222222
2323
24
Weightage - 40InputOutput
15
11111111111111
2222222222222
333333333333
44444444444
5555555555
666666666
77777777
8888888
999999
1010101010
11111111
121212
1313
14
Weightage - 20InputOutput
17
1111111111111111
222222222222222
33333333333333
4444444444444
555555555555
66666666666
7777777777
888888888
99999999
10101010101010
111111111111
1212121212
13131313
141414
1515
16
Weightage - 20InputOutput
10
111111111
22222222
3333333
444444
55555
6666
777
88
9
Weightage - 10InputOutput
3
11
2
Weightage - 10Sample InputSample Output
5
1111
222
33
4
Sample InputSample Output
7
111111
22222
3333
444
55
6
Solution
#include
int main () {
int i, j, n ;
scanf("%d",&n);
for(i=1; i
{
for(j=i;j
{
printf("%d", i);
}
printf("\n");
}
}
Q16Test CaseInputOutput
10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
Weightage - 40InputOutput
8
*
***
*****
*******
*********
***********
*************
***************
Weightage - 20InputOutput
7
*
***
*****
*******
*********
***********
*************
Weightage - 20InputOutput
3
*
***
*****
Weightage - 10InputOutput
5
*
***
*****
*******
*********
Weightage - 10Sample InputSample Output
4
*
***
*****
*******
Sample InputSample Output
6
*
***
*****
*******
*********
***********
Solution
Header
#include
int main()
{
int row, c, n, s;
//printf("Enter the number of rows in pyramid of stars you wish to see\n");
scanf("%d", &n);
s = n;
for (row = 1; row <= n; row++) // Loop to print rows
{
for (c = 1; c < s; c++) // Loop to print spaces in a row
printf(" ");
s--;
for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row
printf("*");
Footer
printf("\n");
}
return 0;
}
Q17Test CaseInputOutput
6
9
8
7
6
5
4
The sum of the elements in the array is 39
Weightage - 10InputOutput
6
100
200
150
250
300
350
The sum of the elements in the array is 1350
Weightage - 10InputOutput
5
32
25
14
98
74
The sum of the elements in the array is 243
Weightage - 10InputOutput
10
10
20
30
40
50
60
70
80
90
100
The sum of the elements in the array is 550
Weightage - 10InputOutput
4
12
24
36
48
The sum of the elements in the array is 120
Weightage - 10InputOutput
7
3
6
9
12
15
18
21
The sum of the elements in the array is 84
Weightage - 10InputOutput
2
0
1
The sum of the elements in the array is 1
Weightage - 10InputOutput
0
1
2
The sum of the elements in the array is 0
Weightage - 10InputOutput
3
50
100
150
The sum of the elements in the array is 300
Weightage - 10InputOutput
5
22
44
66
88
112
The sum of the elements in the array is 332
Weightage - 10Sample InputSample Output
5
3
2
2
1
6
The sum of the elements in the array is 14
Sample InputSample Output
3
1
2
3
The sum of the elements in the array is 6
Solution
#include
#include
int main()
{
int *a,i,n,sum=0;
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
for(i=0;i
{
scanf("%d",a+i);
}
for(i=0;i
{
sum = sum +(*(a+i));
}
printf("The sum of the elements in the array is %d",sum);
return 0;
}
Q18Test CaseInputOutput
68
58
Your age is 90
Weightage - 40InputOutput
75
10
Your age is 35
Weightage - 20InputOutput
65
10
Your age is 45
Weightage - 20InputOutput
97
12
Your age is 15
Weightage - 10InputOutput
88
15
Your age is 27
Weightage - 10Sample InputSample Output
27
07
Your age is 80
Sample InputSample Output
99
21
Your age is 22
Solution
#include
int main()
{
int curr_year,b_year,age;
scanf("%d",&b_year);
scanf("%d",&curr_year);
if(curr_year>b_year)
{
age=(curr_year) - (b_year);
printf("Your age is %d",age);
}
else if(curr_year
{
age=(curr_year+100)-b_year;
printf("Your age is %d",age);
}
return 0;
}
Q19Test CaseInputOutput
0
0
0
0
0
0
0
0
0
0
0
Weightage - 10InputOutput
5
5
10
15
20
25
30
35
40
45
50
Weightage - 10InputOutput
20
20
40
60
80
100
120
140
160
180
200
Weightage - 20InputOutput
30
30
60
90
120
150
180
210
240
270
300
Weightage - 20InputOutput
55
55
110
165
220
275
330
385
440
495
550
Weightage - 40Sample InputSample Output
2
2
4
6
8
10
12
14
16
18
20
Sample InputSample Output
10
10
20
30
40
50
60
70
80
90
100
Solution
#include "stdio.h"
void printTable(int n);
int main(){
int n;
scanf("%d",&n);
printTable(n);
}
void printTable(int num)
{
int i,value = 0;
for(i=1;i<=10;i++)
{
value = num * i ;
printf("%d\n", value);
}
}
Q20Test CaseInputOutput
1 2 3 1 2 3 1 2 3
4
Weightage - 40InputOutput
3 3 3 3 3 3 3 3 3
9
Weightage - 20InputOutput
2 5 8 3 6 9 1 4 7
7
Weightage - 20InputOutput
1 2 3 4 5 6 7 8 9
15
Weightage - 10InputOutput
3 2 1 6 5 4 9 8 7
15
Weightage - 10Sample InputSample Output
1 1 1 1 1 1 1 1 1
3
Sample InputSample Output
1 2 3 4 5 6 7 8 9
15
Solution
#include
#include
int calculatematrixsum(int **matrix,int m,int n);
int main(){
int m=3,n=3;
int **matrix;
int i;
matrix=(int **)malloc(sizeof(int));
for( i=0;i<3;i++){
matrix[i]=(int *)malloc(sizeof(int));}
int j;
for(i=0;i<3;i++){
for( j=0;j<3;j++){
scanf("%d",&matrix[i][j]);}}
int s=calculatematrixsum(matrix,m,n);
printf("%d",s);
}
int calculatematrixsum(int **matrix,int m,int n)
{
int i,j,sum=0,row=m,column=n;
if((row>0)&&(column>0)){
int sum=0;
for(i=0;i
for(j=0;j
{
if(i==j)
{
if(matrix[i][j]%2!=0)
sum+=matrix[i][j];
//printf("%d",sum);
}
}
}
return sum;
}
else
return sum;
}
Q21Test CaseInputOutput
indiana
Not a palindrome.
Weightage - 40InputOutput
mtr
Not a palindrome.
Weightage - 10InputOutput
hard
Not a palindrome.
Weightage - 10InputOutput
madam
Palindrome.
Weightage - 20InputOutput
rar
Palindrome.
Weightage - 20Sample InputSample Output
mom
Palindrome.
Sample InputSample Output
string
Not a palindrome.
Solution
Header
#include
int main()
{
char text[100];
int begin, middle, end, length = 0;
scanf("%s",&text);
while (text[length] != '\0')
length++;
end = length - 1;
middle = length/2;
Footer
for (begin = 0; begin < middle; begin++)
{
if (text[begin] != text[end])
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if (begin == middle)
printf("Palindrome.\n");
return 0;
}
Q22Test CaseInputOutput
100
100 is a positive number
Weightage - 40InputOutput
-30
-30 is a negative number
Weightage - 20InputOutput
-50
-50 is a negative number
Weightage - 20InputOutput
10
10 is a positive number
Weightage - 10InputOutput
3
3 is a positive number
Weightage - 10Sample InputSample Output
9
9 is a positive number
Sample InputSample Output
-3
-3 is a negative number
Solution
#include
int main()
{
int number;
//printf("Enter a number \n");
scanf("%d", &number);
if (number >= 0)
printf("%d is a positive number \n", number);
else
printf("%d is a negative number \n", number);
}
Q23Test CaseInputOutput
11000
11000 is even.
Weightage - 40InputOutput
582
582 is even.
Weightage - 20InputOutput
369
369 is odd.
Weightage - 20InputOutput
20
20 is even.
Weightage - 10InputOutput
30
30 is even.
Weightage - 10Sample InputSample Output
15
15 is odd.
Sample InputSample Output
18
18 is even.
Solution
#include
int main()
{
int number;
//printf("Enter an integer: ");
scanf("%d", &number);
// True if the number is perfectly divisible by 2
if(number % 2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);
return 0;
}
Q24Test CaseInputOutput
1200
1156
2356
Weightage - 40InputOutput
89
98
187
Weightage - 20InputOutput
33
56
89
Weightage - 20InputOutput
12
15
27
Weightage - 10InputOutput
41
14
55
Weightage - 10Sample InputSample Output
10
2
12
Sample InputSample Output
5
6
11
Solution
#include
int add(int,int);
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c=add(a,b);
printf("%d",c);
return 0;
}
int add(int a,int b)
{
int sum;
sum=a+b;
return sum;
}
Q25Test CaseInputOutput
100
100 is a positive number
Weightage - 40InputOutput
-30
-30 is a negative number
Weightage - 20InputOutput
-50
-50 is a negative number
Weightage - 20InputOutput
10
10 is a positive number
Weightage - 10InputOutput
3
3 is a positive number
Weightage - 10Sample InputSample Output
9
9 is a positive number
Sample InputSample Output
-3
-3 is a negative number
Solution
Header
#include
int main()
{
int number;
//printf("Enter a number \n");
scanf("%d", &number);
Footer
if (number >= 0)
printf("%d is a positive number \n", number);
else
printf("%d is a negative number \n", number);
}
Q26Test CaseInputOutput
145698
The sum of digits in 145698 is 33
Weightage - 10InputOutput
741235
The sum of digits in 741235 is 22
Weightage - 10InputOutput
145
The sum of digits in 145 is 10
Weightage - 10InputOutput
20
The sum of digits in 20 is 2
Weightage - 10InputOutput
520
The sum of digits in 520 is 7
Weightage - 10InputOutput
851
The sum of digits in 851 is 14
Weightage - 10InputOutput
369
The sum of digits in 369 is 18
Weightage - 10InputOutput
581
The sum of digits in 581 is 14
Weightage - 10InputOutput
654
The sum of digits in 654 is 15
Weightage - 10InputOutput
753
The sum of digits in 753 is 15
Weightage - 10Sample InputSample Output
123
The sum of digits in 123 is 6
Sample InputSample Output
1240
The sum of digits in 1240 is 7
Solution
#include
#include
int computeSum(int n)
{
int s;
if(n%10==n)
{
return n;
}
s=n%10;
return s+computeSum(n/10);
}
int main()
{
int n;
scanf("%d",&n);
computeSum(n);
printf("The sum of digits in %d is %d",n,computeSum(n));
return 0;
}
Q27Test CaseInputOutput
321123
321123 is a palindrome.
Weightage - 40InputOutput
2552
2552 is a palindrome.
Weightage - 20InputOutput
52225
52225 is a palindrome.
Weightage - 20InputOutput
20
20 is not a palindrome.
Weightage - 10InputOutput
102
102 is not a palindrome.
Weightage - 10Sample InputSample Output
505
505 is a palindrome.
Sample InputSample Output
103
103 is not a palindrome.
Solution
#include
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
//printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}
Q28Test CaseInputOutput
20
1
-2
3
4
5
-6
7
-8
9
10
-11
Original array : 1 -2 3 4 5 -6 7 -8 9 10 -11 0 0 0 0 0 0 0 1180438888 32617
Rearranged array : -2 -6 -8 -11 0 0 0 0 0 0 0 1 3 4 5 7 9 10 1180438888 32617
Weightage - 40InputOutput
15
1
2
-3
-4
5
6
-7
8
9
-10
11
12
13
-14
-15
Original array : 1 2 -3 -4 5 6 -7 8 9 -10 11 12 13 -14 -15
Rearranged array : -3 -4 -7 -10 -14 -15 1 2 5 6 8 9 11 12 13
Weightage - 20InputOutput
12
12
11
10
-9
-8
-7
6
5
-4
3
2
1
Original array : 12 11 10 -9 -8 -7 6 5 -4 3 2 1
Rearranged array : -9 -8 -7 -4 12 11 10 6 5 3 2 1
Weightage - 20InputOutput
5
1
2
-3
-4
5
Original array : 1 2 -3 -4 5
Rearranged array : -3 -4 1 2 5
Weightage - 10InputOutput
3
1
-2
3
Original array : 1 -2 3
Rearranged array : -2 1 3
Weightage - 10Sample InputSample Output
5
1
-2
-3
4
-5
Original array : 1 -2 -3 4 -5
Rearranged array : -2 -3 -5 1 4
Sample InputSample Output
4
-1
-8
9
6
Original array : -1 -8 9 6
Rearranged array : -1 -8 9 6
Solution
Header
#include
#include
void rearrange_alternate_positions(int arr[], int n)
{
int temp, j;
for(int i = 1; i < n; i++)
{
temp = arr[i];
if (temp > 0)
continue;
j = i - 1;
while ( arr[j] > 0 && j >= 0)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
Footer
}
int main()
{
int n,i;
//printf("\nEnter the number of elements : ");
scanf("%d",&n);
int arr[n];
//printf("\nInput the array elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
printf("\nOriginal array : ");
for(i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
rearrange_alternate_positions(arr, n);
printf("\nRearranged array : ");
for(i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
Q29Test CaseInputOutput
100
200
200
100
Weightage - 40InputOutput
25
85
85
25
Weightage - 20InputOutput
40
60
60
40
Weightage - 20InputOutput
3
6
6
3
Weightage - 10InputOutput
4
1
1
4
Weightage - 10Sample InputSample Output
10
20
20
10
Sample InputSample Output
30
40
40
30
Solution
#include
void swap(int *a, int *b);
int main()
{
int m,n;
scanf("%d %d",&m,&n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n%d \n%d", *a, *b);
}
Q30Test CaseInputOutput
10
99
98
100
110
65
452
78
84
96
120
Maximum element in the array is 452
Weightage - 40InputOutput
6
32
42
52
62
72
92
Maximum element in the array is 92
Weightage - 20InputOutput
7
110
210
310
810
410
510
610
Maximum element in the array is 810
Weightage - 20InputOutput
6
12
15
22
10
9
8
Maximum element in the array is 22
Weightage - 10InputOutput
4
100
1000
10
100
Maximum element in the array is 1000
Weightage - 10Sample InputSample Output
4
2
3
5
6
Maximum element in the array is 6
Sample InputSample Output
5
62
35
21
45
100
Maximum element in the array is 100
Solution
#include
#include
int max=0;
int findMaxElement(int *arr,int size)
{
if(size==0)
{
return max;
}
if(*(arr+(size-1))>max)
{
max=(*(arr+(size-1)));
//printf("max %d\n",max);
}
else
{
max=max;
//printf("max %d\n",max);
}
return findMaxElement(arr,(size-1));
}
int main()
{
int *arr,size,i;
scanf("%d",&size);
arr=(int *)malloc(size*sizeof(int));
for(i=0;i
{
scanf("%d",(arr+i));
}
findMaxElement(arr,size);
printf("Maximum element in the array is %d",findMaxElement(arr,size));
return 0;
}
Q31Test CaseInputOutput
2
2000
2500
2250.00
Weightage - 40InputOutput
2
120
160
140.00
Weightage - 20InputOutput
2
369
254
311.50
Weightage - 20InputOutput
2
10
15
12.50
Weightage - 10InputOutput
2
7
9
8.00
Weightage - 10Sample InputSample Output
2
5
6
5.50
Sample InputSample Output
2
25
25
25.00
Solution
Header
#include
#include
int main()
{
int n,*p,sum=0,i;
float avg;
scanf("%d",&n);
p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMEMORY ALLOCATION UNSUCCCESSFUL");
}
for(i=0;i
{
scanf("%d",(p+i));
}
for(i=0;i
sum=sum+*(p+i);
Footer
avg=(float)sum/n;
printf("\n %0.2f",avg);
}
Q32Test CaseInputOutput
10
1
11
112
1123
11235
112358
11235813
1123581321
112358132134
11235813213455
Weightage - 20InputOutput
12
1
11
112
1123
11235
112358
11235813
1123581321
112358132134
11235813213455
1123581321345589
1123581321345589144
Weightage - 20InputOutput
15
1
11
112
1123
11235
112358
11235813
1123581321
112358132134
11235813213455
1123581321345589
1123581321345589144
1123581321345589144233
1123581321345589144233377
1123581321345589144233377610
Weightage - 40InputOutput
6
1
11
112
1123
11235
112358
Weightage - 10InputOutput
2
1
11
Weightage - 10Sample InputSample Output
3
1
11
112
Sample InputSample Output
5
1
11
112
1123
11235
Solution
Header
#include
#include
int main(){
int a=0,b=1,i,c,n,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=0;
b=1;
printf("%d\t",b);
for(j=1;j
{
c=a+b;
printf("%d\t",c);
Footer
a=b;
b=c;
}
printf("\n");
}
return 0;
}
Q33Test CaseInputOutput
100
Entered Number is not a perfect number
Weightage - 40InputOutput
28
Entered Number is perfect number
Weightage - 20InputOutput
58
Entered Number is not a perfect number
Weightage - 20InputOutput
10
Entered Number is not a perfect number
Weightage - 10InputOutput
4
Entered Number is not a perfect number
Weightage - 10Sample InputSample Output
25
Entered Number is not a perfect number
Sample InputSample Output
6
Entered Number is perfect number
Solution
Header
#include
int main()
{
int number, rem, sum = 0, i;
//("Enter a Number\n");
scanf("%d", &number);
for (i = 1; i <= (number - 1); i++)
{
rem = number % i;
if (rem == 0)
{
sum = sum + i;
}
Footer
}
if (sum == number)
printf("Entered Number is perfect number");
else
printf("Entered Number is not a perfect number");
return 0;
}
Q34Test CaseInputOutput
89
18.00
The ticket price is $8.00
Weightage - 40InputOutput
12
13.30
The ticket price is $2.00
Weightage - 10InputOutput
13
18.00
The ticket price is $4.00
Weightage - 10InputOutput
55
22.00
The ticket price is $8.00
Weightage - 20InputOutput
35
10.15
The ticket price is $8.00
Weightage - 20Sample InputSample Output
10
10.15
The ticket price is $4.00
Solution
#include
int main()
{
int age;
float time,time1;
scanf("%d",&age);
scanf("%f",&time);
time1=13.30;
if(age>13)
{
if(time==time1)
{
printf("The ticket price is $5.00");
}
else
{
printf("The ticket price is $8.00");
}
}
else if(age<=13)
{
if(time==time1)
{
printf("The ticket price is $2.00");
}
else
{
printf("The ticket price is $4.00");
}
}
return 0;
}
Q35Test CaseInputOutput
12
1
2
3
4
5
6
7
8
9
10
11
4
7
44
47
74
77
444
447
474
477
744
744
Weightage - 40InputOutput
9
3
6
9
2
5
8
1
4
7
44
77
474
7
74
447
4
47
444
Weightage - 20InputOutput
7
10
14
15
16
17
18
19
477
777
4444
4447
4474
4477
4744
Weightage - 20InputOutput
4
0
1
2
3
0
4
7
44
Weightage - 10InputOutput
3
2
4
6
7
47
77
Weightage - 10Sample InputSample Output
3
1
2
3
4
7
44
Sample InputSample Output
5
1
3
5
7
9
4
44
74
444
474
Solution
#include
int main()
{
int i,j,k,t,n,a[10000];
scanf("%d",&t);
for(j=0;j
{
scanf("%d