Professional Documents
Culture Documents
me/campusdrive
https://t.me/cognizant_exam
1) PRINT THE FOLLOWING PATTERN
11
111
1111
#include<stdio.h>
int print=1,i,j;
for(i=0;i<num;i++)
for(j=0;j<=i;j++)
Telegram: https://t.me/campusdrive
{
printf("%d",print);
printf("\n");
CORRECTION :
NO CORRECTION REQUIRED
The function/method checkBirthDay compiles successfully but fails to return the desired
result for some test cases. Your task is to fix the code so that it passes all the test cases.
if((strcmp(month,"July")) || (day==5))
return 1;
else
return 0;
Telegram: https://t.me/campusdrive
Correction Required:
if(!(strcmp(month,"July")) &&(day==5))
return 1;
else
return 0;
Reason:
The function strcmp return 0 if strings are equal .So we need to keep ! .Also we need to
use && instead of || as both the conditions need to be satisfied
#include <stdio.h>
int count=0;
for(int i=0;i<size-1;i++)
if((inputlist[i]%2==0) &&(inputlist[i]==inputlist[i++]))
Telegram: https://t.me/campusdrive
count++;
return count;
CORRECTION:
#include <stdio.h>
int count=0;
for(int i=0;i<size-1;i++)
if((inputlist[i]%2==0) &&(inputlist[i]==inputlist[i+1]))
count++;
return count;
REASON:
4) COUNTOCCURENCES
Telegram: https://t.me/campusdrive
GIVEN CODE
int i=0,count=0;
while(i<len)
if(arr[i]==value)
count+=1 ;
return count;
CORRECTION
int i=0,count=0;
while(i<len)
if(arr[i]==value)
count+=1 ;
i++;
return count;
5)
res[0]=!(arr[0]==0);
for(int i=1;i<len;i++)
res[i]=!(arr[i]==arr[i-1]);
for(int i=0;i<len;i++)
printf("%d ",res[i]);
Reason:
Also if elements are equal 0 must be printed but not 1 so must include not symbol
6)
Sol:
int result,min,max,mid;
max=(numA>numB?(numA>numC?numA:numC):(numB>numC?numB:numC));
min=(numA<numB?(numA<numC?numA:numC):(numB<numC?numB:numC));
mid=numA+numB+numC-(max+min);
return result;
GIVEN CODE
CORRECTION
Telegram: https://t.me/campusdrive
8)
sol:
int i,j;
for(i=1;i<=num;i++)
for(j=1;j<=2 i;j++)
printf("%d ",i);
printf("\n");
9) MAX REPLACE:
GIVEN CODE:
CORRECTION:
Telegram: https://t.me/campusdrive
CORRECTION
11)
Telegram: https://t.me/campusdrive
Sol:
void printFibonacci(int num)
int i;
long sum=0;
long num1=0;
long num2=1;
for(i=1;i<=num;++i)
printf("%ld ",num1);
sum=num1+num2;
num2=num1;
num1=sum;
12)
Telegram: https://t.me/campusdrive
Sol:
void reverseHalfArray(int size,int inputList)
int i,temp;
for(i=0;i<size/2;i++)
temp=inputList[size-i-1];
inputList[size-i-1]=inputList[(size/2)+i];
inputList[(size/2)+i]=temp;
13)
Sol:
Telegram: https://t.me/campusdrive
int p=num;
int count=0;
while(num!=0)
num=num/10;
count++;
return(p%count);
14)
Telegram: https://t.me/campusdrive
Sol:
void arrayReverse(int len,int arr)
int i,temp,originalLen=len;
for(i=0;i<originalLen/2;i++)
temp=arr[len-1-i];
arr[len-1-i]=arr[i];
arr[i]=temp;
reason:
The array can be reversed by swapping from first and last upto middle
15)d
Telegram: https://t.me/campusdrive
int i=0,j=0,temp=0;
for(i=0;i<len;i++)
for(j=i+1;j<len;j++)
if(arr[i]>arr[j])
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
return arr;
int brr1=sortArray(len1,arr1);
int brr2=sortArray(len2,arr2);
int a=(brr1[len1-1]>brr2[len2-1]?brr1[len1-1]:brr2[len2-1]);
return a;
Reason:
Telegram: https://t.me/campusdrive
to find max among
two arrays sort the two arrays and take last elements of two arrays and return
maximum of those two elements
Telegram: https://t.me/campusdrive
1) Check for syntax error/ logical
error and correct the error to get the desired output.
int main()
int n;
scanf("%d", &n);
unsigned int i = n;
while(i >= 0)
printf("%dn", i);
i--;
return 0;
Error - Logical error unsigned int i = n; unsigned integer ranges from 0 to 65535, which
will be taken in the cyclic order.So i-- will keep repeating in a cyclic way. The loop will
never be terminated. So it should be written as int i = n;
scanf("%d", &n);
Telegram: https://t.me/campusdrive
for(i =1; i <= n; i++)
fact = fact i;
printf("%d", fact);
return 0;
}
Input: 20
Output: -2102132736
The fact and n are declare d as long int, so in scanf and printf%ld should be used
in place of %d
1111
222
33
void main()
int i, j, n;
scanf("%d", &n);
{
Telegram: https://t.me/campusdrive
printf("%d", i);
printf("\n");
}
Input: 3
Output:
111
222
333
The inner for loop has to be written in this way: for(j = i-1; j<n+1; j++)
int main()
printf("%d", num1);
elseif(num2>num3)
printf("%d", num2)
Telegram: https://t.me/campusdrive
}
else
printf("%d", num3);
return 0;
5) Fix the error, recompile and match against the output provided.
int main(void)
return 0;
Corrected program:
int main(void)
return 0;
}
Telegram: https://t.me/campusdrive
6) Code
voidbinarytodecimal(number)
void main()
int num;
scanf("%d", &num);
printf(%d, binarytodecimal(num);
Answer
voidbinarytodecimal(number)
while(number > 0)
base = base * 2;
Telegram: https://t.me/campusdrive
}
return dval;
7) Print the prime numbers from an array up to given value n by using existing function.
int main()
scanf("%d", &n);
if(isprime(m))
arr[size++]= m;
printf("%dn", arr[i]);
return 0;
}
Telegram: https://t.me/campusdrive
Answer:
int i;
int isprime = 1;
if(num % i == 0)
isprime = 0;
break;
return isprime;
8) Find the syntax error in the below code without modifying the logic.
#include <stdio.h>
int main()
float x = 1.1;
switch (x)
break;
return 0;
Answer
#include <stdio.h>
int main()
int x = 1;
switch (x)
break;
break;
return 0;
The expression used in the switch must be an integral type (int, char, and enum). Any
other type of expression is not allowed.
void main () {
int i, j, n = 5;
Telegram: https://t.me/campusdrive
for(i=1; i<n; i++)
for(j=i;j<n;j++);
printf("%d", i);
printf("\n");
Solution:
void main () {
int i, j, n = 5;
for(j=i;j<n;j++)
printf("%d", i);
printf("\n");
we use a semicolon in C statement to tell the compiler where's the end of our statement.
Second for loop executes one time.
Telegram: https://t.me/campusdrive
10 )Complete the below code by
reusing the existing function.
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.
Return Value:
2) In case there is more than one equilibrium element, return the element with least index
value
You are required to complete the given code by reusing the existing function. You can
click on Compile & run anytime to check the compilation/execution status of the program
you can use printf to debug your code. The submitted code should be
logically/syntactically correct and pass all the test cases.
We do not expect you to modify the approach or incorporate any additional library
methods.
Test Case:
a[] = {1,2,3,4,3,3}. 4 is the equilibrium element since its left side sum (1+2+3) is equal to its
right side sum (3+3)
#include <stdio.h>
int sum = 0, i;
sum += a[i];
Telegram: https://t.me/campusdrive
}
return sum;
int sum = 0, i;
sum += a[i];
return sum;
int main() {
//code
int a[10], n, i;
scanf("%d", &n);
scanf("%d", &a[i]);
if(equiIndex != -1) {
printf("%d", a[equiIndex]);
return 0;
Solution:
int sum = 0, i;
sum += a[i];
return sum;
int sum = 0, i;
Telegram: https://t.me/campusdrive
for(i = idx + 1; i < n; i++)
sum += a[i];
return sum;
int i;
return i;
return -1;
int main() {
//code
int a[10], n, i;
scanf("%d", &n);
Telegram: https://t.me/campusdrive
// get the
i<n; i++)
scanf("%d", &a[i]);
if(equiIndex != -1) {
printf("%d", a[equiIndex]);
return 0;
12) Check for syntax error/ logical error and correct the error to get the desired output.
The function sortString modifies the input list by sorting its elements depending upon
the length of the array, i.e; if the length of the array is even, then the elements are
arranged in the ascending order, and if the length of the array is odd, then the elements
are arranged in the descending order
The function sortString accepts two arguments – len representing the length of the string,
and arr a list of characters, representing the input list respectively.
The function sortString compiles successfully but fails to get the desired results for some
test cases due to logical errors. Your task is to fix the code, so that it passess all the test
cases
Incorrect Code
{
Telegram: https://t.me/campusdrive
int i, max,
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
else
for(i=0;i<len;i++)
max=arr[i];
location = i;
Telegram: https://t.me/campusdrive
for(j=i;j<len;j++)
if(max>arr[j])//error in this line
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
Correct Code
if(len%2 == 0)
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max>arr[j])
max=arr[j];
Telegram: https://t.me/campusdrive
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
else
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max<arr[j])
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
Telegram: https://t.me/campusdrive
}
13) Check for syntax error/ logical error and correct the error to get the desired output.
The function sortString modifies the input list by sorting its elements depending upon
the length of the array, i.e; if the length of the array is even, then the elements are
arranged in the ascending order, and if the length of the array is odd, then the elements
are arranged in the descending order
The function sortString accepts two arguments – len representing the length of the string,
and arr a list of characters, representing the input list respectively
The function sortString compiles successfully but fails to get the desired results for some
test cases due to logical errors. Your task is to fix the code, so that it passess all the test
cases
Incorrect Code
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
max=arr[j];
Telegram: https://t.me/campusdrive
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
else
for(i=0;i<len;i++)
max=arr[i];
location = i;
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
Telegram: https://t.me/campusdrive
}
Correct Code
if(len%2 == 0)
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max>arr[j])
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
else
{
Telegram: https://t.me/campusdrive
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max<arr[j])
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
14) Check for syntax error/ logical error and correct the error to get the desired output.
The function replaceElements is modifying the input list in such a way – if the sum of all
the elements of the input list is odd, then all the elements of the input list are supposed to
be replaced by 1s, and in case if the sum of all the elements of the input list is even, then
the elements should be replaced by 0s.
For example, given the input list [1,2,3,4,5], the function will modify the input list like [1,
1, 1, 1, 1]
Telegram: https://t.me/campusdrive
The function
replaceElements accepts two arguments – size an integer representing the size of the
given input list and arr, a list of integers representing the input list.
The function replaceElements compiles successfully but fails to get the desired result for
some test cases due to incorrect implementation of the function. Your task is to fix the
code so that it passes all the test cases
int i,j;
int sum=0;
for (i=0;i<size;i++)
sum+=arr[i];
i=0;
while(i<size)
arr[i] = 0;
i += 1;
else
j=1;
Telegram: https://t.me/campusdrive
while(j<size)
arr[j]=1;
j+=1;
Corrected Code
int i,j;
int sum=0;
for (i=0;i<size;i++)
sum+=arr[i];
if(sum % 2 == 0)
i=0;
while(i<size)
arr[i] = 0;
i += 1;
}
Telegram: https://t.me/campusdrive
else
j=1;
while(j<size)
arr[j]=1;
j+=1;
15) Check for syntax error/ logical error and correct the error to get the desired output.
Test Case : 1
n : 10
Output
0 2 4 6 8 10
Test Case : 2
n:9
Output
1 3 5 79
Incorrect Code
#include <stdio.h>
int main()
int n, i;
Telegram: https://t.me/campusdrive
printf("n : ");
scanf("%d", n);
if(n%2=0)
for(i=0;i<n:i=i+2)
printf("%d ",i);
else
for(i=1;i<n;i=i+2)
printf("%d ",i);
return 0;
Corrected Code
#include <stdio.h>
int main()
int n, i;
printf("n : ");
scanf("%d",&n);
Telegram: https://t.me/campusdrive
if(n%2==0)
for(i=0;i<=n;i=i+2)
printf("%d ",i);
else
for(i=1;i<=n;i=i+2)
printf("%d ",i);
return 0;
17) Check whether the below program print the below pattern
Input
Output
Telegram: https://t.me/campusdrive
1
22
333
4444
4444
333
22
Incorrect Code
#include <stdio.h>
int main()
int i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
for(j=1;j<=i;j=j+1)
printf("%d",i);
printf("\n");
for(i=n;i>=1;i=i-1)
{
Telegram: https://t.me/campusdrive
for(j=1;j<=i;j=j-1)
printf("%d",i);
return 0;
Corrected Code
#include <stdio.h>
int main()
int i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
for(j=1;j<=i;j=j+1)
printf("%d",i);
printf("\n");
for(i=n;i>=1;i=i-1)
{
Telegram: https://t.me/campusdrive
for(j=1;j<=i;j=j+1)
printf("%d",i);
printf("\n");
return 0;
20) Check for syntax/logical error and correct the code for desired output.
In the code you need to find the greatest among three numbers.
Incorrect Code
#include <stdio.h>
int main()
int a, b, c, max_num;
printf("First: ");
scanf("%d",&a);
printf("Second: ");
scanf("%d",&b);
printf("Third: ");
scanf("%d",&c);
return 0;
Corrected Code
#include <stdio.h>
int main()
int a, b, c, max_num;
printf("First: ");
scanf("%d",&a);
printf("Second: ");
scanf("%d",&b);
printf("Third: ");
scanf("%d",&c);
return 0;
21) Print the prime numbers from an array up to given value n by using existing function.
int main()
scanf(“%d”, &n);
if(isprime(m))
arr[size++]= m;
printf(“%d\n”, arr[i]);
return 0;
Corrected Code
int i;
int isprime = 1;
{
Telegram: https://t.me/campusdrive
if(num % i == 0)
isprime = 0;
break;
return isprime;
22) Instructions: You are required to write the code. The code should be
logically/syntactically correct.
Problem: Write a program in C to display the table of a number and print the sum of all
the multiples in it.
Test Cases:
TestCase 1:
Input:
275
TestCase 2:
Input:
12
660
#include <stdio.h>
int main()
Corrected Code
#include <stdio.h>
int main()
scanf("%d",&n);
value = n i;
printf("%d \t",value);
sum=sum+value;
printf("\nsum : %d",sum);
return 0;
}
Telegram: https://t.me/campusdrive
23)You have to find the security key from the data transmitted.
Input
Output
Print an integer representing the security key for the given data.
Example
Input
578378923
Output
783
Explanation
The repeated digits in the data are 7, 8 and 3. So, the security key is 783
#include <stdio.h>
#include <string.h>
int main()
char a[50];
scanf("%s",a);
strlen(a);
for(i=0;i<len;i++)
for(j=i+1;j<len;j++)
Telegram: https://t.me/campusdrive
{
if(a[i]=a[j])
printf("%c",a[i]);
break;
return 0;
Corrected Code
#include <stdio.h>
#include <string.h>
int main()
char a[50];
scanf("%s",a);
len = strlen(a);
for(i=0;i<len;i++)
for(j=i+1;j<len;j++)
if(a[i]==a[j])
{
Telegram: https://t.me/campusdrive
printf("%c",a[i]);
break;
return 0;
25) Question :
function/method compiles successfully but fails to return the desired result for some/all
cases due to incorrect implementation. Your task is to correct the code so that it passes all
test cases.
int count = 0;
if(arr[i]==value)
count++;
}
Telegram: https://t.me/campusdrive
return sum;
Corrected Code
int count = 0;
if(arr[i]==value)
count++;
return count;
26) Question
Series : 0, 1, 1, 2, 3, 5, 8, 13
Example
Input
Output
33
Telegram: https://t.me/campusdrive
Explanation:
Incorrect Code
#include <stdio.h>
int main()
int a=0,b=1,c,n,i=2,sum=0;
printf("n : ");
scanf("%d",&n);
while(i<=n)
c=a+b;
a=b;
b=c;
sum=sum+c;
i++;
printf("%d",sum);
return 0;
Corrected Code
#include <stdio.h>
int main()
{
Telegram: https://t.me/campusdrive
int
a=0,b=1,c,n,i=2,sum=1;
printf("n : ");
scanf("%d",&n);
while(i<n)
c=a+b;
a=b;
b=c;
sum=sum+c;
i++;
printf("%d",sum);
return 0;