SlideShare a Scribd company logo
1 of 25
Download to read offline
1st question
Lisa always forgets her birthday which is on the 5 th July In order to help her, we have a class BirthDay h
aving a method checkBirthDay(String month,int day) which takes day and month as inputs and return Yes
if it is her birthday else return No The method compiles fine but fails to return the desired result for some
cases Your task is to fix the code so that it passes all test cases.
#include<stdio.h>
#include<string.h>
int checkBirthday(char* month,int day){}
int main()
{
char inp[10];
scanf("%s",inp);
int day;
scanf("%d",&day);
if(checkBirthday(inp,day)==1)
printf("Yes");
else
printf("No");
return 0 ;
}
#include<stdio.h>
#include<string.h>
int checkBirthday(char* month,int day)
{
if(strcmp(month,"july") == 0 && (day -5) == 0)
return 1;
else
return 0;
-----------------------------------------------------------------------------------------
2 question
Find the syntax error in the below code without modifying the logic.
#include<stdio.h>
int main()
{
int x = 1;
switch (1.1)
{
case 1: printf("Choice is 1");
break;
default: printf("Invalid choice");
break;
}
}
-----------------------------------------------------------------------------------
2 ans
#include<stdio.h>
int main()
{
int x = 1;
switch (x)
{
case 1: printf("Choice is 1");
break;
default: printf("Invalid choice");
break;
}
}
-----------------------------------------------------------------------------------------
3rd question
Given two integers A and B. The task is to count how many numbers in the interval [ A, B ] have an odd n
umber of divisors.
#include<stdio.h>
int OddDivCount(int a, int b)
{
//write your code here
}
int main()
{
int a, b;
printf("%d",OddDivCount(a,b));
return 0;
}
-----------------------------------------------------------------------------------------
3 ans
#include<stdio.h>
int OddDivCount(int a, int b)
{
// variable to odd divisor count
int res = 0;
// iterate from a to b and count their
// number of divisors
for (int i = a; i <= b; ++i) {
// variable to divisor count
int divCount = 0;
for (int j = 1; j <= i; ++j) {
if (i % j == 0) {
++divCount;
}
}
// if count of divisor is odd
// then increase res by 1
if (divCount % 2) {
++res;
}
}
return res;
}
// Driver code
int main()
{
int a, b;
scanf("%d%d",&a,&b);
printf("%d",OddDivCount(a,b));
return 0;
}
-----------------------------------------------------------------------------------------
4th question
Print "yes" if the given number is a prime number else "No"
#include<stdio.h>
int isprime(int num)
{
//write your code here
}
int main()
{
int n;
scanf("%d",&n);
if(isprime(n)){
printf("Yes");
}
else{
printf("No");
}
}
-----------------------------------------------------------------------------------------
4th ans
#include<stdio.h>
int isprime(int num)
{
int i;
int isprime = 1;
for(i = 2; i <= num / 2; i++)
{
if(num % i == 0)
{
isprime = 0;
break;
}
}
return isprime;
}
int main()
{
int n;
scanf("%d",&n);
if(isprime(n)){
printf("Yes");
}
else{
printf("No");
}
}
-----------------------------------------------------------------------------------------
5th question
swap all the odd bits into even bits and vice versa. Complete swapBits(unsigned int x){}
to give the desired output.
#include <stdio.h>
unsigned int swapBits(unsigned int x)
{
//write your code here
}
int main()
{
unsigned int x;
scanf("%u",&x);
printf("%u ", swapBits(x));
return 0;
}
-----------------------------------------------------------------------------------------
5 ans
#include <stdio.h>
unsigned int swapBits(unsigned int x)
{
unsigned int even_bits = x & 0xAAAAAAAA;
unsigned int odd_bits = x & 0x55555555;
even_bits >>= 1;
odd_bits <<= 1;
return (even_bits | odd_bits);
}
int main()
{
unsigned int x;
scanf("%u",&x);
printf("%u ", swapBits(x));
return 0;
}
-----------------------------------------------------------------------------------------
6th question
6.
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.
#include<stdio.h>
int getarraysum(int *arr,int len)
{
int sum = 0, i;
for( i=1;i<=len;i=i+1)
{
sum -= arr[i];
}
return sum;
}
// Driver Program
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("%d", getarraysum(arr, n));
return 0;
}
-----------------------------------------------------------------------------------------
6 ans
#include<stdio.h>
int getarraysum(int *arr,int len)
{
int sum = 0, i;
for( i=0;i<len;i=i+1)
{
sum += arr[i];
}
return sum;
}
// Driver Program
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("%d", getarraysum(arr, n));
return 0;
}
--------------------------------------------------------------------------------------
7th question
The snippet below is supposed to find the maximum of three integers and return maximum of the three int
egers. Complete the function max(num1,num2,num3) , to get the desired output
#include<stdio.h>
int main()
{
int num1, num2, num3;
scanf("%d %d %d", &num1,&num2,&num3);
//write your code here
}
----------------------------------------------------------------------------------------
#include<stdio.h>
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) && (num2>num1))
{
printf("%d", num2);
}
else
{
printf("%d", num3);
}
return 0;
}
----------------------------------------------------------------------------------------
8th question
8.
Mr. Jason has captured your friend and has a collar around his neck. He has locked the collar with a give
n locking key". Now it can only be opened with an unlocking key. Your friend sees the locking key but he
does not know how to find unlocking key.You can calculate the unlocking key if you have the locking key,
because the unlocking key will be the smallest (in magnitude) permutation of the digits of the locking key
and will never start with zero. Help your friend write an algorithm that outputs the unlocking key by taking
key as an input.
----------------------------------------------------------------------------------------
8ans
#include<stdio.h>
int unlock(int num)
{
int arr[10],index=0,n,result=0,temp;
int itr;
while(num)
{
arr[index]=num%10;
index++;
num=num/10;
}
n=index;
for(itr=1;itr<=n-1;itr++)
{
for(index=1;index<n;index++)
{
if(arr[index-1] > arr[index])
{
int temp=arr[index];
arr[index]=arr[index-1];
arr[index-1]=temp;
}
}
}
index=0;
while(arr[index]==0)
index++;
temp=arr[index];
arr[index]=arr[0];
arr[0]=temp;
for(index=0;index<n;index++)
{
result=result*10+arr[index];
}
return result;
}
int main()
{
int num,res;
scanf("%d",&num);
res=unlock(num);
printf("%d",res);
return 0;
}
----------------------------------------------------------------------------------------
9th question
The function/method rotateList accepts three arguments as inputs - an integer size, an integer k and a no
de list_head representing size of the list, the rotation index value and the head node of the linked list, resp
ectively. It is supposed to rotate the linked list in the counterclockwise direction from the kth node The fu
nction/method 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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *start;
void displaySLL()
{
NODE *tptr;
for(tptr=start;tptr==NULL;tptr=tptr->next)
printf("%d ",tptr->data);
printf("n"); }
void insertData(int givenData) {
struct node *newnode;
NODE *tptr,*prev;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=givenData;
newnode->next=NULL;
if(start!=NULL)
{
start=newnode;
}
else
{
for(tptr=start;tptr!=NULL;prev=tptr,tptr=tptr->next);
prev->next=newnode;
} }
void rotate(int n,int k,NODE *head) {
NODE *tptr,*prev,*safeNext,*safePrev;
int count;
tptr=head;
for(count=1;count<k;count++)
{
prev=tptr;
tptr=tptr->next;
}
safePrev=NULL;
while(tptr!=NULL)
{
safeNext=tptr->next;
tptr->next=safePrev;
safePrev=tptr;
tptr=safeNext;
}
prev->next=safePrev;
}
int main() {
int n,k,index,num;
scanf("%d %d",&n,&k);
for(index=0;index<n;index++)
{
scanf("%d",&num);
insertData(num);
}
rotate(n,k,start);
displaySLL();
return 0;
}
----------------------------------------------------------------------------------------
9 ans
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *start;
void displaySLL()
{
NODE *tptr;
for(tptr=start;tptr!=NULL;tptr=tptr->next)
printf("%d ",tptr->data);
printf("n"); }
void insertData(int givenData) {
struct node *newnode;
NODE *tptr,*prev;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=givenData;
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
}
else
{
for(tptr=start;tptr!=NULL;prev=tptr,tptr=tptr->next);
prev->next=newnode;
} }
void rotate(int n,int k,NODE *head) {
NODE *tptr,*prev,*safeNext,*safePrev;
int count;
tptr=head;
for(count=1;count<k;count++)
{
prev=tptr;
tptr=tptr->next;
}
safePrev=NULL;
while(tptr!=NULL)
{
safeNext=tptr->next;
tptr->next=safePrev;
safePrev=tptr;
tptr=safeNext;
}
prev->next=safePrev;
}
int main() {
int n,k,index,num;
scanf("%d %d",&n,&k);
for(index=0;index<n;index++)
{
scanf("%d",&num);
insertData(num);
}
rotate(n,k,start);
displaySLL();
return 0;
}
----------------------------------------------------------------------------------------
10th question
Given two integers A and B. The task is to count how many numbers in the interval [ A, B ] have an odd n
umber of divisors. Complete logic is provided in the function OddDivCount(int a,int b) ,find the logical error
and fix it.
#include int OddDivCount(int a, int b)
{
int res = 0;
for (int i = a; i <= b; ++i)
{
int divCount = 0;
for (int j = 1; j <= b; ++j)
{
if (i % j == 0)
{
++divCount;
} }
if (divCount % 2)
{
++res;
} }
return res;
}
int main()
{
int a, b;
printf("%d",OddDivCount(a,b));
return 0;
}
----------------------------------------------------------------------------------------
10 ans
#include<stdio.h>
int OddDivCount(int a, int b)
{
int res = 0;
for (int i = a; i <= b; ++i) {
int divCount = 0;
for (int j = 1; j <= i; ++j) {
if (i % j == 0) {
++divCount;
}
}
if (divCount % 2) {
++res;
}
}
return res;
}
int main()
{
int a, b;
scanf("%d%d",&a,&b);
printf("%d",OddDivCount(a,b));
return 0;
}
----------------------------------------------------------------------------------------
11th question
Check whether the below program print the below pattern. If not rectify the program in the editor below. In
put: 3
Output:
1111
222
33
void main()
{
int i, j, n;
scanf(%d, &n);
for(i = 1; i<n;i++)
{
for(j = 1; j<n;j++)
{
printf(%d, i);
}
printf(n);
}
----------------------------------------------------------------------------------------
11 ans
#include<stdio.h>
int main()
{
int i, j, n;
scanf("%d", &n);
for(i = 1; i<=n; i++)
{
for(j = i-1; j<=n; j++)
{
printf("%d", i);
}
printf("n");
}
}
----------------------------------------------------------------------------------------
12th question
A ternary search tree is defined as a ternary tree in which a search, proceed character by character, com
pares the current character in the search string with the character at the current node if the search charac
ter is lexicographically larger , the search goes to the right child. When the search character is equal, the
search goes to the middle child and proceeds to the next character in the search string. For example, tern
ary search tree for strings cat, cats, up, bug. The function/method insertIntoTernaryTree accepts three arg
uments tree representing a ternary tree, inputStr, a String representing the index of the current character
in inputStr. The function/method is supposed to return the ternary tree node after inserting the word into th
e tree. Complete the function insertIntoTernaryTree.
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
struct Node
{
char data;
unsigned isEndOfString: 1;
struct Node *left, *eq, *right;
};
struct Node* newNode(char data)
{
struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
return temp;
}
void insert(struct Node** root, char *word)
{
// write code
}
void traverseTSTUtil(struct Node* root, char* buffer, int depth)
{
if (root)
{
traverseTSTUtil(root->left, buffer, depth);
buffer[depth] = root->data;
if (root->isEndOfString)
{
buffer[depth+1] = ’0’;
printf( "%sn", buffer);
}
traverseTSTUtil(root->eq, buffer, depth + 1);
traverseTSTUtil(root->right, buffer, depth);
}
}
void traverseTST(struct Node* root)
{
char buffer[MAX];
traverseTSTUtil(root, buffer, 0);
}
int searchTST(struct Node *root, char *word)
{
if (!root)
return 0;
if (*word < (root)->data)
return searchTST(root->left, word);
else if (*word > (root)->data)
return searchTST(root->right, word);
else
{
if (*(word+1) == ’0’)
return root->isEndOfString;
return searchTST(root->eq, word+1);
}
}
int main()
{
struct Node *root = NULL;
insert(&root, "cat");
insert(&root, "cats");
insert(&root, "up");
insert(&root, "bug");
printf("Following is traversal of ternary search treen");
traverseTST(root);
printf("nFollowing are search results for cats, bu and cat respectivelyn");
searchTST(root, "cats")? printf("Foundn"): printf("Not Foundn");
searchTST(root, "bu")? printf("Foundn"): printf("Not Foundn");
searchTST(root, "cat")? printf("Foundn"): printf("Not Foundn");
return 0;
}
----------------------------------------------------------------------------------------
12 ans
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
struct Node
{
char data;
unsigned isEndOfString: 1;
struct Node *left, *eq, *right;
};
struct Node* newNode(char data)
{
struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
return temp;
}
void insert(struct Node** root, char *word)
{
if (!(*root))
*root = newNode(*word);
if ((*word) < (*root)->data)
insert(&( (*root)->left ), word);
else if ((*word) > (*root)->data)
insert(&( (*root)->right ), word);
else
{
if (*(word+1))
insert(&( (*root)->eq ), word+1);
else
(*root)->isEndOfString = 1;
}
}
void traverseTSTUtil(struct Node* root, char* buffer, int depth)
{
if (root)
{
traverseTSTUtil(root->left, buffer, depth);
buffer[depth] = root->data;
if (root->isEndOfString)
{
buffer[depth+1] = ’0’;
printf( "%sn", buffer);
}
traverseTSTUtil(root->eq, buffer, depth + 1);
traverseTSTUtil(root->right, buffer, depth);
}
}
void traverseTST(struct Node* root)
{
char buffer[MAX];
traverseTSTUtil(root, buffer, 0);
}
int searchTST(struct Node *root, char *word)
{
if (!root)
return 0;
if (*word < (root)->data)
return searchTST(root->left, word);
else if (*word > (root)->data)
return searchTST(root->right, word);
else
{
if (*(word+1) == ’0’)
return root->isEndOfString;
return searchTST(root->eq, word+1);
}
}
int main()
{
struct Node *root = NULL;
insert(&root, "cat");
insert(&root, "cats");
insert(&root, "up");
insert(&root, "bug");
printf("Following is traversal of ternary search treen");
traverseTST(root);
printf("nFollowing are search results for cats, bu and cat respectivelyn");
searchTST(root, "cats")? printf("Foundn"): printf("Not Foundn");
searchTST(root, "bu")? printf("Foundn"): printf("Not Foundn");
searchTST(root, "cat")? printf("Foundn"): printf("Not Foundn");
return 0;
}
----------------------------------------------------------------------------------------
13th question
The function patternPrint(int n) supposed to print n numberof lines in the following pattern
For n=4 the pattern should be:
1
1 1
1 1 1
1 1 1 1
Complete the function patternPrint(int n) to get the desired output
PROGRAM
#include<stdio.h>
void patternPrint(int num)
{ // write here
}
int main()
{
int n;
scanf("%d",&n);
patternPrint(n);
}
----------------------------------------------------------------------------------------
13 ans
#include<stdio.h>
void patternPrint(int num)
{
for(int i=0;i<num;i++){
for(int j=0;j<=i;j++){
printf("1");
}
printf("n");
}
}
int main()
{
int n;
scanf("%d",&n);
patternPrint(n);
}
----------------------------------------------------------------------------------------
14th question
Correct Answer
patternPrint(n);
}
14.
The method countElement(int arr[],int n) of class ElementCount is supposed to return the number of elem
ents in the input array arr which are greater than twice the input number n .Find the logical error in the fun
ction countElement(int arr[], int n) and fix it.
#include<stdio.h>
int countElement(int arr[],int ele,int len)
{
int count=0;
for(int i=0;i<len;i++)
{
if(arr[i]>ele)
{
count++;
}
}
return count;
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d ",&arr[i]);
}
int val;
scanf("%d",&val);
printf("%d",countElement(arr,val,n));
}
----------------------------------------------------------------------------------------
14 ans
#include<stdio.h>
int countElement(int arr[],int ele,int len)
{
int count=0;
for(int i=0;i<len;i++)
{
if(arr[i]>2*ele)
{
count++;
}
}
return count;
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d ",&arr[i]);
}
int val;
scanf("%d",&val);
printf("%d",countElement(arr,val,n));
}
----------------------------------------------------------------------------------------
15th question
The function printcolor(int num) is supposed to point names of accordind to the given input number num
When the values of num equal 1,2,3,4 the function points red,black,white,green respectively.for any other
values num it should print No Color . Find the logical error in the function printcolor(int num) and fix it.
#include<stdio.h>
void printcolor(int num) {
switch(num) {
case 1:
printf("Red");
case 2:
printf("Black");
case 3 :
printf("White");
case 4:
printf("Green");
default:
printf("No color");
}}
int main() {
int num;
scanf("%d",&num);
printcolor(num); }
----------------------------------------------------------------------------------------
15 ans
#include<stdio.h>
void printcolor(int num) {
switch(num) {
case 1:
printf("Red");
break;
case 2:
printf("Black");
break;
case 3 :
printf("White");
break;
case 4:
printf("Green");
break;
default:
printf("No color");
break; }}
int main() {
int num;
scanf("%d",&num);
printcolor(num);
}
16th question
#include<stdio.h>
int* maxReplace(int *arr, int len)
{
int i, max;
if(len>0)
{
//
max=arr[0];
for(i=0;i<len;i++)
{
// int dummy;
if(max<arr[i])
max=arr[i];
}
// dummy = 100;
}
for(i=0;i<len;i++)
arr[i]=max;
return arr;
}
int main()
{
int size, ind;
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++){
scanf("%d",&arr[i]);
}
maxReplace(arr, size);
for(ind = 0; ind < size; ind++)
printf("%d ", arr[ind]);
}
17th question
#include <stdio.h>
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Driver program to test above function
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}
18th question
#include<stdio.h>
int findSum(int n)
{
int sum = 0;
for (int x=1; x<=n; x++)
sum = sum + x;
return sum;
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",findSum(n));
return 0;
}
19th question
#include<stdio.h>
int countOccurences(int arr[],int val,int n)
{
int count=0;
for(int i=0;i<n;i++)
{
if(arr[i]==val){
count++;
}
}
return count;
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d ",&arr[i]);
}
int val;
scanf("%d",&val);
printf("%d",countOccurences(arr,val,n));
}
20 question
#include <stdio.h>
int power(int x, unsigned int y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
int order(int x)
{
int n = 0;
while (x) {
n++;
x = x / 10;
}
return n;
}
int isArmstrong(int x)
{
int n = order(x);
int temp = x, sum = 0;
while (temp) {
int r = temp % 10;
sum += power(r, n);
temp = temp / 10;
}
if (sum == x)
return 1;
else
return 0;
}
int main()
{
int x;
scanf("%d",&x);
if (isArmstrong(x) == 1)
printf("Truen");
else
printf("Falsen");
return 0;
}
21 question
#include<stdio.h>
int * sortArray(int *arr, int length)
{
int x=0,y=0,n=length;
for(x=0;x<n;x++)
{
int index_of_min = x;
for(y=x;y<n;y++)
{
if(arr[index_of_min]>arr[y])
{
index_of_min=y;
}
}
int temp=arr[x];
arr[x]=arr[index_of_min];
arr[index_of_min]=temp;
}
return arr;
}
void minElement(int arr1[],int arr2[],int size)
{
int index;
sortArray(arr1, size);
sortArray(arr2, size);
int min=0;
if(arr1[0]>arr2[0]){
min=arr2[0];
}
else{
min=arr1[0];
}
printf("%d",min);
}
int main()
{
int size;
scanf("%d",&size);
int arr1[size],arr2[size];
for(int i=0;i<size;i++)
{
scanf("%d",&arr1[i]);
}
for(int i=0;i<size;i++)
{
scanf("%d",&arr2[i]);
}
minElement(arr1,arr2,size);
return 0;
}
22 question
#include <stdio.h>
void printPattern(int r)
{
r = r / 2;
int count = r;
for (int i = 0; i < r; i++) {
for (int j = r; j > i; j--) {
if (j != r) {
printf("*%d",count);
}
else {
printf("%d",count);
}
}
count--;
printf("n");
}
count++;
for (int i = 0; i < r; i++)
{
for (int j = 0; j <= i; j++)
{
if (j != 0)
{
printf("*%d",count);
}
else {
printf("%d",count);
}
}
count++;
printf("n");
}
}
int main()
{
int n;
scanf("%d",&n);
printPattern(n);
return 0;
}
23 question
#include<stdio.h>
#include<string.h>
#include<math.h>
void encodeString(char arr[],char alpha[],int num[]){
int front=-1,rear=-1;
int len=strlen(arr);
alpha[++rear]=arr[0];
num[rear]=1;
for(int i=1;i<len;i++){
if(arr[i]==alpha[rear]){
num[rear]++;
}
else{
alpha[++rear]=arr[i];
num[rear]=1;
}
}
while(front!=rear){
++front;
printf("%c%d",alpha[front],num[front]);
}
}
int main(){
char arr[1000];
scanf("%s",arr);
char alpha[1000];
int num[1000];
encodeString(arr,alpha,num);
}
24question
#include <stdio.h>
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int calculateLCM(int a, int b)
{
return (a*b)/gcd(a, b);
}
int calculateGeneralLCM( int arr[],int n){
int x = calculateLCM(arr[0],arr[1]);
for(int i=2;i<n;i++){
x = calculateLCM(x,arr[i]);
}
return x;
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("%d",calculateGeneralLCM(arr,n));
return 0;
}
25 question
#include<stdio.h>
void checkGrades(int marks){
if(marks>=91){
printf("A");
}
else if(marks>=76 && marks<=90){
printf("B");
}
else if(marks>=61 && marks<=75){
printf("C");
}
else{
printf("D");
}
}
int main()
{
int marks;
scanf("%d",&marks);
checkGrades(marks);
}
26 question
#include<stdio.h>
int * descendingSortArray(int *arr, int len)
{
int small, pos, i, j, temp;
for(i = 0; i <= len-1 ; i++)
{
for(j = i; j < len; j++)
{
temp = 0;
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
int main()
{
int index, size;
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++){
scanf("%d",&arr[i]);
}
descendingSortArray(arr, size);
for(index = 0; index < size; index++)
printf("%d " , arr[index]);
return 0;
}

More Related Content

Similar to sodapdf-converted into ppt presentation(1).pdf (20)

DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
C Programs.pptx
C Programs.pptxC Programs.pptx
C Programs.pptx
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
7 functions
7  functions7  functions
7 functions
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Cpl
CplCpl
Cpl
 
Lab Question
Lab QuestionLab Question
Lab Question
 
C
CC
C
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
C programming
C programmingC programming
C programming
 

Recently uploaded

VAPI CALL GIRL 92628/71154 VAPI CALL GIR
VAPI CALL GIRL 92628/71154 VAPI CALL GIRVAPI CALL GIRL 92628/71154 VAPI CALL GIR
VAPI CALL GIRL 92628/71154 VAPI CALL GIRNiteshKumar82226
 
Call Girls In {Laxmi Nagar Delhi} 9667938988 Indian Russian High Profile Girl...
Call Girls In {Laxmi Nagar Delhi} 9667938988 Indian Russian High Profile Girl...Call Girls In {Laxmi Nagar Delhi} 9667938988 Indian Russian High Profile Girl...
Call Girls In {Laxmi Nagar Delhi} 9667938988 Indian Russian High Profile Girl...aakahthapa70
 
NAGPUR CALL GIRL 92628*71154 NAGPUR CALL
NAGPUR CALL GIRL 92628*71154 NAGPUR CALLNAGPUR CALL GIRL 92628*71154 NAGPUR CALL
NAGPUR CALL GIRL 92628*71154 NAGPUR CALLNiteshKumar82226
 
Call Now ☎9870417354|| Call Girls in Noida Sector 12 Escort Service Noida N.C.R.
Call Now ☎9870417354|| Call Girls in Noida Sector 12 Escort Service Noida N.C.R.Call Now ☎9870417354|| Call Girls in Noida Sector 12 Escort Service Noida N.C.R.
Call Now ☎9870417354|| Call Girls in Noida Sector 12 Escort Service Noida N.C.R.riyadelhic riyadelhic
 
Call Girls In {Green Park Delhi} 9667938988 Indian Russian High Profile Girls...
Call Girls In {Green Park Delhi} 9667938988 Indian Russian High Profile Girls...Call Girls In {Green Park Delhi} 9667938988 Indian Russian High Profile Girls...
Call Girls In {Green Park Delhi} 9667938988 Indian Russian High Profile Girls...aakahthapa70
 
BHOPAL CALL GIRL 92628*71154 BHOPAL CALL
BHOPAL CALL GIRL 92628*71154 BHOPAL CALLBHOPAL CALL GIRL 92628*71154 BHOPAL CALL
BHOPAL CALL GIRL 92628*71154 BHOPAL CALLNiteshKumar82226
 
Call Girls In Naraina (Delhi) +91-9667422720 Escorts Service
Call Girls In Naraina (Delhi) +91-9667422720 Escorts ServiceCall Girls In Naraina (Delhi) +91-9667422720 Escorts Service
Call Girls In Naraina (Delhi) +91-9667422720 Escorts ServiceLipikasharma29
 
Call Girls In {{Connaught Place Delhi}}96679@38988 Indian Russian High Profil...
Call Girls In {{Connaught Place Delhi}}96679@38988 Indian Russian High Profil...Call Girls In {{Connaught Place Delhi}}96679@38988 Indian Russian High Profil...
Call Girls In {{Connaught Place Delhi}}96679@38988 Indian Russian High Profil...aakahthapa70
 
Russian Call Girls in Goa %(9316020077)# Russian Call Girls in Goa By Russi...
Russian Call Girls  in Goa %(9316020077)# Russian Call Girls  in Goa By Russi...Russian Call Girls  in Goa %(9316020077)# Russian Call Girls  in Goa By Russi...
Russian Call Girls in Goa %(9316020077)# Russian Call Girls in Goa By Russi...Goa Call Girls Service Goa escort agency
 
Call Girls In {{Laxmi Nagar Delhi}} 9667938988 Indian Russian High Profile Es...
Call Girls In {{Laxmi Nagar Delhi}} 9667938988 Indian Russian High Profile Es...Call Girls In {{Laxmi Nagar Delhi}} 9667938988 Indian Russian High Profile Es...
Call Girls In {{Laxmi Nagar Delhi}} 9667938988 Indian Russian High Profile Es...aakahthapa70
 
Best VIP Call Girl Noida Sector 48 Call Me: 8700611579
Best VIP Call Girl Noida Sector 48 Call Me: 8700611579Best VIP Call Girl Noida Sector 48 Call Me: 8700611579
Best VIP Call Girl Noida Sector 48 Call Me: 8700611579diyaspanoida
 
RAJKOT CALL GIRLS 92628/71154 RAJKOT CAL
RAJKOT CALL GIRLS 92628/71154 RAJKOT CALRAJKOT CALL GIRLS 92628/71154 RAJKOT CAL
RAJKOT CALL GIRLS 92628/71154 RAJKOT CALNiteshKumar82226
 
Call Now ☎9870417354|| Call Girls in Dwarka Escort Service Delhi N.C.R.
Call Now ☎9870417354|| Call Girls in Dwarka Escort Service Delhi N.C.R.Call Now ☎9870417354|| Call Girls in Dwarka Escort Service Delhi N.C.R.
Call Now ☎9870417354|| Call Girls in Dwarka Escort Service Delhi N.C.R.riyadelhic riyadelhic
 
MYSORE CALL GIRLS ESCORT SER 92628/71154
MYSORE CALL GIRLS ESCORT SER 92628/71154MYSORE CALL GIRLS ESCORT SER 92628/71154
MYSORE CALL GIRLS ESCORT SER 92628/71154NiteshKumar82226
 
Call Girl Rohini ❤️7065000506 Pooja@ Rohini Call Girls Near Me ❤️♀️@ Sexy Cal...
Call Girl Rohini ❤️7065000506 Pooja@ Rohini Call Girls Near Me ❤️♀️@ Sexy Cal...Call Girl Rohini ❤️7065000506 Pooja@ Rohini Call Girls Near Me ❤️♀️@ Sexy Cal...
Call Girl Rohini ❤️7065000506 Pooja@ Rohini Call Girls Near Me ❤️♀️@ Sexy Cal...Sheetaleventcompany
 
Hot Vip Call Girls Service In Sector 149,9818099198 Young Female Escorts Serv...
Hot Vip Call Girls Service In Sector 149,9818099198 Young Female Escorts Serv...Hot Vip Call Girls Service In Sector 149,9818099198 Young Female Escorts Serv...
Hot Vip Call Girls Service In Sector 149,9818099198 Young Female Escorts Serv...riyaescorts54
 
CALL GIRLS 9999288940 women seeking men Locanto No Advance North Goa
CALL GIRLS 9999288940 women seeking men Locanto No Advance North GoaCALL GIRLS 9999288940 women seeking men Locanto No Advance North Goa
CALL GIRLS 9999288940 women seeking men Locanto No Advance North Goadelhincr993
 
9891550660 Call Girls In Noida Sector 62 Short 1500 Night 6000
9891550660 Call Girls In Noida Sector 62 Short 1500 Night 60009891550660 Call Girls In Noida Sector 62 Short 1500 Night 6000
9891550660 Call Girls In Noida Sector 62 Short 1500 Night 6000teencall080
 
Best VIP Call Girls Noida Sector 23 Call Me: 8700611579
Best VIP Call Girls Noida Sector 23 Call Me: 8700611579Best VIP Call Girls Noida Sector 23 Call Me: 8700611579
Best VIP Call Girls Noida Sector 23 Call Me: 8700611579diyaspanoida
 

Recently uploaded (20)

VAPI CALL GIRL 92628/71154 VAPI CALL GIR
VAPI CALL GIRL 92628/71154 VAPI CALL GIRVAPI CALL GIRL 92628/71154 VAPI CALL GIR
VAPI CALL GIRL 92628/71154 VAPI CALL GIR
 
Call Girls In {Laxmi Nagar Delhi} 9667938988 Indian Russian High Profile Girl...
Call Girls In {Laxmi Nagar Delhi} 9667938988 Indian Russian High Profile Girl...Call Girls In {Laxmi Nagar Delhi} 9667938988 Indian Russian High Profile Girl...
Call Girls In {Laxmi Nagar Delhi} 9667938988 Indian Russian High Profile Girl...
 
NAGPUR CALL GIRL 92628*71154 NAGPUR CALL
NAGPUR CALL GIRL 92628*71154 NAGPUR CALLNAGPUR CALL GIRL 92628*71154 NAGPUR CALL
NAGPUR CALL GIRL 92628*71154 NAGPUR CALL
 
Call Now ☎9870417354|| Call Girls in Noida Sector 12 Escort Service Noida N.C.R.
Call Now ☎9870417354|| Call Girls in Noida Sector 12 Escort Service Noida N.C.R.Call Now ☎9870417354|| Call Girls in Noida Sector 12 Escort Service Noida N.C.R.
Call Now ☎9870417354|| Call Girls in Noida Sector 12 Escort Service Noida N.C.R.
 
Call Girls In {Green Park Delhi} 9667938988 Indian Russian High Profile Girls...
Call Girls In {Green Park Delhi} 9667938988 Indian Russian High Profile Girls...Call Girls In {Green Park Delhi} 9667938988 Indian Russian High Profile Girls...
Call Girls In {Green Park Delhi} 9667938988 Indian Russian High Profile Girls...
 
BHOPAL CALL GIRL 92628*71154 BHOPAL CALL
BHOPAL CALL GIRL 92628*71154 BHOPAL CALLBHOPAL CALL GIRL 92628*71154 BHOPAL CALL
BHOPAL CALL GIRL 92628*71154 BHOPAL CALL
 
Call Girls In Naraina (Delhi) +91-9667422720 Escorts Service
Call Girls In Naraina (Delhi) +91-9667422720 Escorts ServiceCall Girls In Naraina (Delhi) +91-9667422720 Escorts Service
Call Girls In Naraina (Delhi) +91-9667422720 Escorts Service
 
Call Girls In {{Connaught Place Delhi}}96679@38988 Indian Russian High Profil...
Call Girls In {{Connaught Place Delhi}}96679@38988 Indian Russian High Profil...Call Girls In {{Connaught Place Delhi}}96679@38988 Indian Russian High Profil...
Call Girls In {{Connaught Place Delhi}}96679@38988 Indian Russian High Profil...
 
Russian Call Girls in Goa %(9316020077)# Russian Call Girls in Goa By Russi...
Russian Call Girls  in Goa %(9316020077)# Russian Call Girls  in Goa By Russi...Russian Call Girls  in Goa %(9316020077)# Russian Call Girls  in Goa By Russi...
Russian Call Girls in Goa %(9316020077)# Russian Call Girls in Goa By Russi...
 
Call Girls In {{Laxmi Nagar Delhi}} 9667938988 Indian Russian High Profile Es...
Call Girls In {{Laxmi Nagar Delhi}} 9667938988 Indian Russian High Profile Es...Call Girls In {{Laxmi Nagar Delhi}} 9667938988 Indian Russian High Profile Es...
Call Girls In {{Laxmi Nagar Delhi}} 9667938988 Indian Russian High Profile Es...
 
Best VIP Call Girl Noida Sector 48 Call Me: 8700611579
Best VIP Call Girl Noida Sector 48 Call Me: 8700611579Best VIP Call Girl Noida Sector 48 Call Me: 8700611579
Best VIP Call Girl Noida Sector 48 Call Me: 8700611579
 
RAJKOT CALL GIRLS 92628/71154 RAJKOT CAL
RAJKOT CALL GIRLS 92628/71154 RAJKOT CALRAJKOT CALL GIRLS 92628/71154 RAJKOT CAL
RAJKOT CALL GIRLS 92628/71154 RAJKOT CAL
 
Call Now ☎9870417354|| Call Girls in Dwarka Escort Service Delhi N.C.R.
Call Now ☎9870417354|| Call Girls in Dwarka Escort Service Delhi N.C.R.Call Now ☎9870417354|| Call Girls in Dwarka Escort Service Delhi N.C.R.
Call Now ☎9870417354|| Call Girls in Dwarka Escort Service Delhi N.C.R.
 
MYSORE CALL GIRLS ESCORT SER 92628/71154
MYSORE CALL GIRLS ESCORT SER 92628/71154MYSORE CALL GIRLS ESCORT SER 92628/71154
MYSORE CALL GIRLS ESCORT SER 92628/71154
 
Call Girl Rohini ❤️7065000506 Pooja@ Rohini Call Girls Near Me ❤️♀️@ Sexy Cal...
Call Girl Rohini ❤️7065000506 Pooja@ Rohini Call Girls Near Me ❤️♀️@ Sexy Cal...Call Girl Rohini ❤️7065000506 Pooja@ Rohini Call Girls Near Me ❤️♀️@ Sexy Cal...
Call Girl Rohini ❤️7065000506 Pooja@ Rohini Call Girls Near Me ❤️♀️@ Sexy Cal...
 
Hot Vip Call Girls Service In Sector 149,9818099198 Young Female Escorts Serv...
Hot Vip Call Girls Service In Sector 149,9818099198 Young Female Escorts Serv...Hot Vip Call Girls Service In Sector 149,9818099198 Young Female Escorts Serv...
Hot Vip Call Girls Service In Sector 149,9818099198 Young Female Escorts Serv...
 
9953056974 Call Girls In Ashok Nagar, Escorts (Delhi) NCR.
9953056974 Call Girls In Ashok Nagar, Escorts (Delhi) NCR.9953056974 Call Girls In Ashok Nagar, Escorts (Delhi) NCR.
9953056974 Call Girls In Ashok Nagar, Escorts (Delhi) NCR.
 
CALL GIRLS 9999288940 women seeking men Locanto No Advance North Goa
CALL GIRLS 9999288940 women seeking men Locanto No Advance North GoaCALL GIRLS 9999288940 women seeking men Locanto No Advance North Goa
CALL GIRLS 9999288940 women seeking men Locanto No Advance North Goa
 
9891550660 Call Girls In Noida Sector 62 Short 1500 Night 6000
9891550660 Call Girls In Noida Sector 62 Short 1500 Night 60009891550660 Call Girls In Noida Sector 62 Short 1500 Night 6000
9891550660 Call Girls In Noida Sector 62 Short 1500 Night 6000
 
Best VIP Call Girls Noida Sector 23 Call Me: 8700611579
Best VIP Call Girls Noida Sector 23 Call Me: 8700611579Best VIP Call Girls Noida Sector 23 Call Me: 8700611579
Best VIP Call Girls Noida Sector 23 Call Me: 8700611579
 

sodapdf-converted into ppt presentation(1).pdf

  • 1. 1st question Lisa always forgets her birthday which is on the 5 th July In order to help her, we have a class BirthDay h aving a method checkBirthDay(String month,int day) which takes day and month as inputs and return Yes if it is her birthday else return No The method compiles fine but fails to return the desired result for some cases Your task is to fix the code so that it passes all test cases. #include<stdio.h> #include<string.h> int checkBirthday(char* month,int day){} int main() { char inp[10]; scanf("%s",inp); int day; scanf("%d",&day); if(checkBirthday(inp,day)==1) printf("Yes"); else printf("No"); return 0 ; } #include<stdio.h> #include<string.h> int checkBirthday(char* month,int day) { if(strcmp(month,"july") == 0 && (day -5) == 0) return 1; else return 0; ----------------------------------------------------------------------------------------- 2 question Find the syntax error in the below code without modifying the logic. #include<stdio.h> int main() { int x = 1; switch (1.1) { case 1: printf("Choice is 1"); break; default: printf("Invalid choice"); break; } } ----------------------------------------------------------------------------------- 2 ans #include<stdio.h> int main() { int x = 1; switch (x) {
  • 2. case 1: printf("Choice is 1"); break; default: printf("Invalid choice"); break; } } ----------------------------------------------------------------------------------------- 3rd question Given two integers A and B. The task is to count how many numbers in the interval [ A, B ] have an odd n umber of divisors. #include<stdio.h> int OddDivCount(int a, int b) { //write your code here } int main() { int a, b; printf("%d",OddDivCount(a,b)); return 0; } ----------------------------------------------------------------------------------------- 3 ans #include<stdio.h> int OddDivCount(int a, int b) { // variable to odd divisor count int res = 0; // iterate from a to b and count their // number of divisors for (int i = a; i <= b; ++i) { // variable to divisor count int divCount = 0; for (int j = 1; j <= i; ++j) { if (i % j == 0) { ++divCount; } } // if count of divisor is odd // then increase res by 1 if (divCount % 2) { ++res; } } return res; } // Driver code int main() { int a, b; scanf("%d%d",&a,&b); printf("%d",OddDivCount(a,b));
  • 3. return 0; } ----------------------------------------------------------------------------------------- 4th question Print "yes" if the given number is a prime number else "No" #include<stdio.h> int isprime(int num) { //write your code here } int main() { int n; scanf("%d",&n); if(isprime(n)){ printf("Yes"); } else{ printf("No"); } } ----------------------------------------------------------------------------------------- 4th ans #include<stdio.h> int isprime(int num) { int i; int isprime = 1; for(i = 2; i <= num / 2; i++) { if(num % i == 0) { isprime = 0; break; } } return isprime; } int main() { int n; scanf("%d",&n); if(isprime(n)){ printf("Yes"); } else{ printf("No"); } } ----------------------------------------------------------------------------------------- 5th question swap all the odd bits into even bits and vice versa. Complete swapBits(unsigned int x){}
  • 4. to give the desired output. #include <stdio.h> unsigned int swapBits(unsigned int x) { //write your code here } int main() { unsigned int x; scanf("%u",&x); printf("%u ", swapBits(x)); return 0; } ----------------------------------------------------------------------------------------- 5 ans #include <stdio.h> unsigned int swapBits(unsigned int x) { unsigned int even_bits = x & 0xAAAAAAAA; unsigned int odd_bits = x & 0x55555555; even_bits >>= 1; odd_bits <<= 1; return (even_bits | odd_bits); } int main() { unsigned int x; scanf("%u",&x); printf("%u ", swapBits(x)); return 0; } ----------------------------------------------------------------------------------------- 6th question 6. 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. #include<stdio.h> int getarraysum(int *arr,int len) { int sum = 0, i; for( i=1;i<=len;i=i+1) { sum -= arr[i]; } return sum; } // Driver Program
  • 5. int main() { int n; scanf("%d",&n); int arr[n]; for(int i=0;i<n;i++){ scanf("%d",&arr[i]); } printf("%d", getarraysum(arr, n)); return 0; } ----------------------------------------------------------------------------------------- 6 ans #include<stdio.h> int getarraysum(int *arr,int len) { int sum = 0, i; for( i=0;i<len;i=i+1) { sum += arr[i]; } return sum; } // Driver Program int main() { int n; scanf("%d",&n); int arr[n]; for(int i=0;i<n;i++){ scanf("%d",&arr[i]); } printf("%d", getarraysum(arr, n)); return 0; } -------------------------------------------------------------------------------------- 7th question The snippet below is supposed to find the maximum of three integers and return maximum of the three int egers. Complete the function max(num1,num2,num3) , to get the desired output #include<stdio.h> int main() { int num1, num2, num3; scanf("%d %d %d", &num1,&num2,&num3); //write your code here } ---------------------------------------------------------------------------------------- #include<stdio.h>
  • 6. 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) && (num2>num1)) { printf("%d", num2); } else { printf("%d", num3); } return 0; } ---------------------------------------------------------------------------------------- 8th question 8. Mr. Jason has captured your friend and has a collar around his neck. He has locked the collar with a give n locking key". Now it can only be opened with an unlocking key. Your friend sees the locking key but he does not know how to find unlocking key.You can calculate the unlocking key if you have the locking key, because the unlocking key will be the smallest (in magnitude) permutation of the digits of the locking key and will never start with zero. Help your friend write an algorithm that outputs the unlocking key by taking key as an input. ---------------------------------------------------------------------------------------- 8ans #include<stdio.h> int unlock(int num) { int arr[10],index=0,n,result=0,temp; int itr; while(num) { arr[index]=num%10; index++; num=num/10; } n=index; for(itr=1;itr<=n-1;itr++) { for(index=1;index<n;index++) { if(arr[index-1] > arr[index]) { int temp=arr[index]; arr[index]=arr[index-1]; arr[index-1]=temp; } } } index=0;
  • 7. while(arr[index]==0) index++; temp=arr[index]; arr[index]=arr[0]; arr[0]=temp; for(index=0;index<n;index++) { result=result*10+arr[index]; } return result; } int main() { int num,res; scanf("%d",&num); res=unlock(num); printf("%d",res); return 0; } ---------------------------------------------------------------------------------------- 9th question The function/method rotateList accepts three arguments as inputs - an integer size, an integer k and a no de list_head representing size of the list, the rotation index value and the head node of the linked list, resp ectively. It is supposed to rotate the linked list in the counterclockwise direction from the kth node The fu nction/method 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 #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; }NODE; NODE *start; void displaySLL() { NODE *tptr; for(tptr=start;tptr==NULL;tptr=tptr->next) printf("%d ",tptr->data); printf("n"); } void insertData(int givenData) { struct node *newnode; NODE *tptr,*prev; newnode=(struct node *)malloc(sizeof(struct node)); newnode->data=givenData; newnode->next=NULL; if(start!=NULL) { start=newnode; } else {
  • 8. for(tptr=start;tptr!=NULL;prev=tptr,tptr=tptr->next); prev->next=newnode; } } void rotate(int n,int k,NODE *head) { NODE *tptr,*prev,*safeNext,*safePrev; int count; tptr=head; for(count=1;count<k;count++) { prev=tptr; tptr=tptr->next; } safePrev=NULL; while(tptr!=NULL) { safeNext=tptr->next; tptr->next=safePrev; safePrev=tptr; tptr=safeNext; } prev->next=safePrev; } int main() { int n,k,index,num; scanf("%d %d",&n,&k); for(index=0;index<n;index++) { scanf("%d",&num); insertData(num); } rotate(n,k,start); displaySLL(); return 0; } ---------------------------------------------------------------------------------------- 9 ans #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; }NODE; NODE *start; void displaySLL() { NODE *tptr; for(tptr=start;tptr!=NULL;tptr=tptr->next) printf("%d ",tptr->data); printf("n"); } void insertData(int givenData) { struct node *newnode; NODE *tptr,*prev; newnode=(struct node *)malloc(sizeof(struct node));
  • 9. newnode->data=givenData; newnode->next=NULL; if(start==NULL) { start=newnode; } else { for(tptr=start;tptr!=NULL;prev=tptr,tptr=tptr->next); prev->next=newnode; } } void rotate(int n,int k,NODE *head) { NODE *tptr,*prev,*safeNext,*safePrev; int count; tptr=head; for(count=1;count<k;count++) { prev=tptr; tptr=tptr->next; } safePrev=NULL; while(tptr!=NULL) { safeNext=tptr->next; tptr->next=safePrev; safePrev=tptr; tptr=safeNext; } prev->next=safePrev; } int main() { int n,k,index,num; scanf("%d %d",&n,&k); for(index=0;index<n;index++) { scanf("%d",&num); insertData(num); } rotate(n,k,start); displaySLL(); return 0; } ---------------------------------------------------------------------------------------- 10th question Given two integers A and B. The task is to count how many numbers in the interval [ A, B ] have an odd n umber of divisors. Complete logic is provided in the function OddDivCount(int a,int b) ,find the logical error and fix it. #include int OddDivCount(int a, int b) { int res = 0; for (int i = a; i <= b; ++i)
  • 10. { int divCount = 0; for (int j = 1; j <= b; ++j) { if (i % j == 0) { ++divCount; } } if (divCount % 2) { ++res; } } return res; } int main() { int a, b; printf("%d",OddDivCount(a,b)); return 0; } ---------------------------------------------------------------------------------------- 10 ans #include<stdio.h> int OddDivCount(int a, int b) { int res = 0; for (int i = a; i <= b; ++i) { int divCount = 0; for (int j = 1; j <= i; ++j) { if (i % j == 0) { ++divCount; } } if (divCount % 2) { ++res;
  • 11. } } return res; } int main() { int a, b; scanf("%d%d",&a,&b); printf("%d",OddDivCount(a,b)); return 0; } ---------------------------------------------------------------------------------------- 11th question Check whether the below program print the below pattern. If not rectify the program in the editor below. In put: 3 Output: 1111 222 33 void main() { int i, j, n; scanf(%d, &n); for(i = 1; i<n;i++) { for(j = 1; j<n;j++) { printf(%d, i); } printf(n); } ---------------------------------------------------------------------------------------- 11 ans #include<stdio.h> int main() { int i, j, n; scanf("%d", &n); for(i = 1; i<=n; i++)
  • 12. { for(j = i-1; j<=n; j++) { printf("%d", i); } printf("n"); } } ---------------------------------------------------------------------------------------- 12th question A ternary search tree is defined as a ternary tree in which a search, proceed character by character, com pares the current character in the search string with the character at the current node if the search charac ter is lexicographically larger , the search goes to the right child. When the search character is equal, the search goes to the middle child and proceeds to the next character in the search string. For example, tern ary search tree for strings cat, cats, up, bug. The function/method insertIntoTernaryTree accepts three arg uments tree representing a ternary tree, inputStr, a String representing the index of the current character in inputStr. The function/method is supposed to return the ternary tree node after inserting the word into th e tree. Complete the function insertIntoTernaryTree. #include <stdio.h> #include <stdlib.h> #define MAX 50 struct Node { char data; unsigned isEndOfString: 1; struct Node *left, *eq, *right; }; struct Node* newNode(char data) { struct Node* temp = (struct Node*) malloc(sizeof( struct Node )); temp->data = data; temp->isEndOfString = 0; temp->left = temp->eq = temp->right = NULL; return temp; return temp; } void insert(struct Node** root, char *word) { // write code } void traverseTSTUtil(struct Node* root, char* buffer, int depth) { if (root) { traverseTSTUtil(root->left, buffer, depth); buffer[depth] = root->data; if (root->isEndOfString) { buffer[depth+1] = ’0’; printf( "%sn", buffer); } traverseTSTUtil(root->eq, buffer, depth + 1);
  • 13. traverseTSTUtil(root->right, buffer, depth); } } void traverseTST(struct Node* root) { char buffer[MAX]; traverseTSTUtil(root, buffer, 0); } int searchTST(struct Node *root, char *word) { if (!root) return 0; if (*word < (root)->data) return searchTST(root->left, word); else if (*word > (root)->data) return searchTST(root->right, word); else { if (*(word+1) == ’0’) return root->isEndOfString; return searchTST(root->eq, word+1); } } int main() { struct Node *root = NULL; insert(&root, "cat"); insert(&root, "cats"); insert(&root, "up"); insert(&root, "bug"); printf("Following is traversal of ternary search treen"); traverseTST(root); printf("nFollowing are search results for cats, bu and cat respectivelyn"); searchTST(root, "cats")? printf("Foundn"): printf("Not Foundn"); searchTST(root, "bu")? printf("Foundn"): printf("Not Foundn"); searchTST(root, "cat")? printf("Foundn"): printf("Not Foundn"); return 0; } ---------------------------------------------------------------------------------------- 12 ans #include <stdio.h> #include <stdlib.h> #define MAX 50 struct Node { char data; unsigned isEndOfString: 1; struct Node *left, *eq, *right; }; struct Node* newNode(char data) { struct Node* temp = (struct Node*) malloc(sizeof( struct Node )); temp->data = data; temp->isEndOfString = 0;
  • 14. temp->left = temp->eq = temp->right = NULL; return temp; return temp; } void insert(struct Node** root, char *word) { if (!(*root)) *root = newNode(*word); if ((*word) < (*root)->data) insert(&( (*root)->left ), word); else if ((*word) > (*root)->data) insert(&( (*root)->right ), word); else { if (*(word+1)) insert(&( (*root)->eq ), word+1); else (*root)->isEndOfString = 1; } } void traverseTSTUtil(struct Node* root, char* buffer, int depth) { if (root) { traverseTSTUtil(root->left, buffer, depth); buffer[depth] = root->data; if (root->isEndOfString) { buffer[depth+1] = ’0’; printf( "%sn", buffer); } traverseTSTUtil(root->eq, buffer, depth + 1); traverseTSTUtil(root->right, buffer, depth); } } void traverseTST(struct Node* root) { char buffer[MAX]; traverseTSTUtil(root, buffer, 0); } int searchTST(struct Node *root, char *word) { if (!root) return 0; if (*word < (root)->data) return searchTST(root->left, word); else if (*word > (root)->data) return searchTST(root->right, word); else { if (*(word+1) == ’0’) return root->isEndOfString; return searchTST(root->eq, word+1);
  • 15. } } int main() { struct Node *root = NULL; insert(&root, "cat"); insert(&root, "cats"); insert(&root, "up"); insert(&root, "bug"); printf("Following is traversal of ternary search treen"); traverseTST(root); printf("nFollowing are search results for cats, bu and cat respectivelyn"); searchTST(root, "cats")? printf("Foundn"): printf("Not Foundn"); searchTST(root, "bu")? printf("Foundn"): printf("Not Foundn"); searchTST(root, "cat")? printf("Foundn"): printf("Not Foundn"); return 0; } ---------------------------------------------------------------------------------------- 13th question The function patternPrint(int n) supposed to print n numberof lines in the following pattern For n=4 the pattern should be: 1 1 1 1 1 1 1 1 1 1 Complete the function patternPrint(int n) to get the desired output PROGRAM #include<stdio.h> void patternPrint(int num) { // write here } int main() { int n; scanf("%d",&n); patternPrint(n); }
  • 16. ---------------------------------------------------------------------------------------- 13 ans #include<stdio.h> void patternPrint(int num) { for(int i=0;i<num;i++){ for(int j=0;j<=i;j++){ printf("1"); } printf("n"); } } int main() { int n; scanf("%d",&n); patternPrint(n); } ---------------------------------------------------------------------------------------- 14th question Correct Answer patternPrint(n); } 14. The method countElement(int arr[],int n) of class ElementCount is supposed to return the number of elem ents in the input array arr which are greater than twice the input number n .Find the logical error in the fun ction countElement(int arr[], int n) and fix it. #include<stdio.h> int countElement(int arr[],int ele,int len) { int count=0; for(int i=0;i<len;i++) { if(arr[i]>ele) { count++; } } return count; } int main() { int n; scanf("%d",&n); int arr[n]; for(int i=0;i<n;i++) { scanf("%d ",&arr[i]); }
  • 17. int val; scanf("%d",&val); printf("%d",countElement(arr,val,n)); } ---------------------------------------------------------------------------------------- 14 ans #include<stdio.h> int countElement(int arr[],int ele,int len) { int count=0; for(int i=0;i<len;i++) { if(arr[i]>2*ele) { count++; } } return count; } int main() { int n; scanf("%d",&n); int arr[n]; for(int i=0;i<n;i++) { scanf("%d ",&arr[i]); } int val; scanf("%d",&val); printf("%d",countElement(arr,val,n)); } ---------------------------------------------------------------------------------------- 15th question The function printcolor(int num) is supposed to point names of accordind to the given input number num When the values of num equal 1,2,3,4 the function points red,black,white,green respectively.for any other values num it should print No Color . Find the logical error in the function printcolor(int num) and fix it. #include<stdio.h> void printcolor(int num) { switch(num) { case 1: printf("Red"); case 2: printf("Black"); case 3 : printf("White"); case 4: printf("Green"); default: printf("No color"); }} int main() {
  • 18. int num; scanf("%d",&num); printcolor(num); } ---------------------------------------------------------------------------------------- 15 ans #include<stdio.h> void printcolor(int num) { switch(num) { case 1: printf("Red"); break; case 2: printf("Black"); break; case 3 : printf("White"); break; case 4: printf("Green"); break; default: printf("No color"); break; }} int main() { int num; scanf("%d",&num); printcolor(num); } 16th question #include<stdio.h> int* maxReplace(int *arr, int len) { int i, max; if(len>0) { // max=arr[0]; for(i=0;i<len;i++) { // int dummy; if(max<arr[i]) max=arr[i]; } // dummy = 100; } for(i=0;i<len;i++) arr[i]=max; return arr; } int main() { int size, ind; scanf("%d",&size); int arr[size];
  • 19. for(int i=0;i<size;i++){ scanf("%d",&arr[i]); } maxReplace(arr, size); for(ind = 0; ind < size; ind++) printf("%d ", arr[ind]); } 17th question #include <stdio.h> // Recursive function to return gcd of a and b int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Driver program to test above function int main() { int a,b; scanf("%d %d",&a,&b); printf("GCD of %d and %d is %d ", a, b, gcd(a, b)); return 0; } 18th question #include<stdio.h> int findSum(int n) { int sum = 0; for (int x=1; x<=n; x++) sum = sum + x; return sum; } int main() { int n; scanf("%d",&n); printf("%d",findSum(n)); return 0; } 19th question #include<stdio.h> int countOccurences(int arr[],int val,int n) { int count=0; for(int i=0;i<n;i++) { if(arr[i]==val){ count++; }
  • 20. } return count; } int main(){ int n; scanf("%d",&n); int arr[n]; for(int i=0;i<n;i++) { scanf("%d ",&arr[i]); } int val; scanf("%d",&val); printf("%d",countOccurences(arr,val,n)); } 20 question #include <stdio.h> int power(int x, unsigned int y) { if (y == 0) return 1; if (y % 2 == 0) return power(x, y / 2) * power(x, y / 2); return x * power(x, y / 2) * power(x, y / 2); } int order(int x) { int n = 0; while (x) { n++; x = x / 10; } return n; } int isArmstrong(int x) { int n = order(x); int temp = x, sum = 0; while (temp) { int r = temp % 10; sum += power(r, n); temp = temp / 10; } if (sum == x) return 1; else return 0; } int main() { int x; scanf("%d",&x);
  • 21. if (isArmstrong(x) == 1) printf("Truen"); else printf("Falsen"); return 0; } 21 question #include<stdio.h> int * sortArray(int *arr, int length) { int x=0,y=0,n=length; for(x=0;x<n;x++) { int index_of_min = x; for(y=x;y<n;y++) { if(arr[index_of_min]>arr[y]) { index_of_min=y; } } int temp=arr[x]; arr[x]=arr[index_of_min]; arr[index_of_min]=temp; } return arr; } void minElement(int arr1[],int arr2[],int size) { int index; sortArray(arr1, size); sortArray(arr2, size); int min=0; if(arr1[0]>arr2[0]){ min=arr2[0]; } else{ min=arr1[0]; } printf("%d",min); } int main() { int size; scanf("%d",&size); int arr1[size],arr2[size]; for(int i=0;i<size;i++) { scanf("%d",&arr1[i]); } for(int i=0;i<size;i++) {
  • 22. scanf("%d",&arr2[i]); } minElement(arr1,arr2,size); return 0; } 22 question #include <stdio.h> void printPattern(int r) { r = r / 2; int count = r; for (int i = 0; i < r; i++) { for (int j = r; j > i; j--) { if (j != r) { printf("*%d",count); } else { printf("%d",count); } } count--; printf("n"); } count++; for (int i = 0; i < r; i++) { for (int j = 0; j <= i; j++) { if (j != 0) { printf("*%d",count); } else { printf("%d",count); } } count++; printf("n"); } } int main() { int n; scanf("%d",&n); printPattern(n); return 0; } 23 question #include<stdio.h> #include<string.h> #include<math.h>
  • 23. void encodeString(char arr[],char alpha[],int num[]){ int front=-1,rear=-1; int len=strlen(arr); alpha[++rear]=arr[0]; num[rear]=1; for(int i=1;i<len;i++){ if(arr[i]==alpha[rear]){ num[rear]++; } else{ alpha[++rear]=arr[i]; num[rear]=1; } } while(front!=rear){ ++front; printf("%c%d",alpha[front],num[front]); } } int main(){ char arr[1000]; scanf("%s",arr); char alpha[1000]; int num[1000]; encodeString(arr,alpha,num); } 24question #include <stdio.h> int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int calculateLCM(int a, int b) { return (a*b)/gcd(a, b); } int calculateGeneralLCM( int arr[],int n){ int x = calculateLCM(arr[0],arr[1]); for(int i=2;i<n;i++){ x = calculateLCM(x,arr[i]); } return x; } int main() { int n; scanf("%d",&n); int arr[n]; for(int i=0;i<n;i++){
  • 24. scanf("%d",&arr[i]); } printf("%d",calculateGeneralLCM(arr,n)); return 0; } 25 question #include<stdio.h> void checkGrades(int marks){ if(marks>=91){ printf("A"); } else if(marks>=76 && marks<=90){ printf("B"); } else if(marks>=61 && marks<=75){ printf("C"); } else{ printf("D"); } } int main() { int marks; scanf("%d",&marks); checkGrades(marks); } 26 question #include<stdio.h> int * descendingSortArray(int *arr, int len) { int small, pos, i, j, temp; for(i = 0; i <= len-1 ; i++) { for(j = i; j < len; j++) { temp = 0; if(arr[i] < arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; } int main() {
  • 25. int index, size; scanf("%d",&size); int arr[size]; for(int i=0;i<size;i++){ scanf("%d",&arr[i]); } descendingSortArray(arr, size); for(index = 0; index < size; index++) printf("%d " , arr[index]); return 0; }