Cognizant Coding Questions and Answers

40

This article covers all the important Cognizant Coding Questions which they
mainly ask in their examinations.

Note: Cognizant changed their exam pattern, now it conducts two rounds which are the
following:

  1. Round 1: Aptitude + Automata fix
  1. Round 2: Coding

For more details about Cognizant Examinations please check out following articles

Here in this article we mainly focus on the coding section if you want to check other Cognizant
details like syllabus, recruitment process or interview questions then must see our Menu panel.

Cognizant coding details

  Section    Details  
  Total Questions    4  
  Time Limit    40 minutes + 80 minutes  
  Difficulty Level    Medium  
  Package Offer    Nearly 6 LPA  

Here is the list of question which covered in this article

Cognizant Coding Question – 1

Program to find the LCM of given two numbers

Here the user should give you two numbers for that you have to find the LCM of them,

Where LCM is Lowest Common factor of two numbers is the
smallest number that divides both of them.

Sample Input 1:

10 15

Sample Output 1:

30

Sample Input 2:

60 20

Sample Output 2:

60

Solution in C:

#include<iostream>

using namespace std;

int HCF(int, int);

int LCM(int, int);

int main() {

               int
num1,
num2, result;

              
cin>>num1;

              
cin>>num2;

               result =
LCM
( num1, num2 );

              
cout<<
result;

               return
0;

}

// This function find the Highest Common Factor of the two numbers

int HCF (int num1, int num2) {

               if (num2
==
0)

                             
return num1;

               return
HCF(num2, num1 % num2);

}

// This function find the Lowest Common Factor of the two numbers

int LCM (int num1, int num2) {

               return
(num1
* num2) / HCF(num1, num2);

}

Cognizant Coding Question – 2

Ananya Pandey’s professor has suggested her to prepare well for the lesson on seasons.
While her professor says a month, she needs to tell the season corresponding to that month.
Write a program to solve the above task.

  • Spring – March to May,
  • Summer – June to August,
  • Autumn – September to November and,
  • Winter – December to February.

The month should be in the range of 1 to 12. If not the output should be “Invalid
month”.

Sample Input 1:

Sample Output 1:

Sample Input 2:

Sample Output 2:

Solution in C:

#include<stdio.h>

#include <stdlib.h>

int main()

{

printf(“Enter the month:”);

int entry ;

scanf(“%d”,&entry);

switch (entry)

{

               
case
12:

               
case
1:

               
case
2:

                     
printf(“Season: Winter”);

               
break;

               
case
3:

               
case
4:

               
case
5:

               
printf(“Season: Spring”);

               
break;

               
case
6:

               
case
7:

               
case
8:

               
printf(“Season: Summer”);

               
break;

               
case
9:

               
case
10:

               
case
11:

               
printf(“Season: Autumn”);

               
break;

               
default:

               
printf(“Invalid month”);

           }

    return 0;

}

Output:

Enter the month: 10

Season: Autumn

Cognizant Coding Question – 3

There is a scheme declared in theatre, in which one ticket gets a 10% discount on the total cost
of tickets if there is a bulk booking which is more than 20 tickets, and if there is a special
coupon card then there is 2% discount on total the cost. Also, customer can buy Refreshments
which additional cost of Rs. 50 per person.

The cost of the K class Rs. 75 and the Q class is Rs. 150.

Write a program to find the total cost as per the scheme.

Note: In-class K and class Q, you have to buy at a time the minimum of 5 tickets
and a maximum of 40. If this condition fails then display the message “Minimum of 5 and
Maximum of 40 Tickets”.  

If the user is given a value other than class ‘K’ or ‘Q’ the output
should be “Invalid Input”.

The cost should be printed correctly to the two decimal places.

Sample Input 1:

  • Enter the no of ticket:35
  • Do you want refreshment:y
  • Do you have coupon code:y
  • Enter the circle:k

Sample Output 1:

Sample Input 2:

Sample Output 2:

  • Minimum of 5 and Maximum of 40 Tickets

Solution in C:

#include<stdio.h>

#include <stdlib.h>

int main()

{

int noTicket;

double total = 0,cost;

char ref[2], co[2] , circle[2];

printf(“Enter the no of ticket:”);

scanf(“%d”,&noTicket);

if (noTicket < 5 || noTicket > 40) {

printf(“Minimum of 5 and Maximum of 40 tickets”);

exit(0);

}

printf(“Do you want refreshment:”);

scanf(“%s”,&ref);

printf(“Do you have coupon code:”);

scanf(“%s”,&co);

printf(“Enter the circle:”);

scanf(“%s”,&circle);

if(circle[0] ==  ‘K’)

cost=75*noTicket;

else if(circle[0]== ‘Q’)

cost=150*noTicket;

else

{

printf(“Invalid Input”);

exit(0);

}

total=cost;

if(noTicket>20)

cost= cost – ((0.1)*cost);

total=cost;

if(co[0]== ‘y’)

total= cost – ((0.02)*cost);

if(ref[0]== ‘y’)

total += (noTicket*50);

printf(“Ticket cost:%.2f”,total);

return 0;

}

Print Prime number series for the given integer

Print the prime numbers from 1 to N, where N is an input.

Sample Input 1:

15

Sample Output 1:

2       3      
5       7      
11      13

Sample Input 2:

5

Sample Output 2:

2       3       5

Solution in C:

#include<iostream>

using namespace std;

bool isPrime(int);

int main(){

               int n;

              
cin>>n;

               for(int
i =
1; i <= n; i++) {

                             
int isPrimeNumber = isPrime(i);

                             
if(isPrimeNumber == true) {

                                            
cout<<i<<“t”;

                             
}

               }

               return
0;

}

// This function is identifying the number is prime or not 

bool isPrime(int n){

               bool
flag =
false;

               for(int
i=2;
i<=n/2; i++){

                             
if(n%i == 0){

                                            
flag = true;

                                            
break;

                             
}

               }

               if(flag
==
false && n > 1){


                             
// Number is prime

                             
return true;

               }

// Number is not a prime

               return
false;

}

Solution in C and for better understanding the concept of prime numbers, check it out this: Prime Number Program in C

Cognizant Coding Question – 5

Find Factors of the given number

Bela teaches his son to find the factors of the given numbers. When he provides a number to his
son, he should tell the factors of that number. Help him to do this, by writing a program. 

Note:

  • If the input is zero (0) then the output will be “No
    Factors
    “.
  • If the input is any negative number, then first convert it positive solve.

Sample Input 1:

54

Sample Output 1:

Factors for 56 are:

1

2

4

7

8

14

28

56

Sample Input 2:

-256

Sample Output 2:

Factors of 256 are:

1

2

4

8

16

32

64

128

256

Solution in C:

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

    int n, i;

    cout << “Enter the positive integer: “;

    cin >> n;

    if(n == 0) {

            cout << “No
Factors”;

            return false;

            }

            if(n < 0) {

                       
n = abs( n );

            }

    cout << “Factors of ” << n << ” are:

<< endl; 

    for(i = 1; i <= n; ++i) {

        if(n % i == 0) {

            cout << i <<
endl;    

                       
}

    }

    return 0;

}

Output:

Enter the positive integer: 56

Factors for 56 are:

1

2

4

7

8

14

28

56

LEAVE A REPLY

Please enter your comment!
Please enter your name here