Cognizant Coding Interview Questions

Cognizant Coding Interview Questions 2022

On this page, find the programming questions with solutions asked in the Cognizant Interview.
Page Highlights:

cognizant genc interview coding questions

Question 1: Write a Program to Reverse any Number.

#include
using namespace std;

//main program
int main ()
{
//variables initialization
int num, reverse = 0, rem;

printf("Enter a number: ");
scanf("%d",&num);

//loop to find reverse number
while(num != 0)
{
rem = num % 10;
reverse = reverse * 10 + rem;
num /= 10;
};

//output
printf("Reverse: %d",reverse);

return 0;
}
// Time complexity O(N)
// Space complexity : O(1)

For more codes check : Reverse of a Number in C

#include<iostream> 
using namespace std;

//main program
int main ()
{
//variables initialization
int num, reverse = 0, rem;

cout <<"Enter a number: "; 
cin >> num;

//loop to find reverse number
while(num != 0)
{
rem = num % 10;
reverse = reverse * 10 + rem;
num /= 10;
};

//output
cout <<"Reversed Number: "<<reverse;

return 0;
}
// Time complexity : O(N)
// Space complexity : O(1)
// where N is number of digits in num

Find More Solutions at C++ Program to Reverse any Number

>public class Main
{
public static void main (String[]args)
{

//variables initialization
int num = 1234, reverse = 0, rem;


//loop to find reverse number
while (num != 0)
{
rem = num % 10;
reverse = reverse * 10 + rem;
num /= 10;
};

//output
System.out.println ("Reversed Number: " + reverse);
}
}
Find More Solutions at JAVA Program to Reverse any Number
num = 1234
temp = num
reverse = 0
while num > 0:
remainder = num % 10
reverse = (reverse * 10) + remainder
num = num // 10

print(reverse)

Find More Solutions at Python Program to Reverse any Number

Question 2: Write a Program to Find out the Sum of Digits of a Number.

#include

int main ()
{
int num, sum = 0;

printf("Enter any num: ");
scanf("%d",&num);

//loop to find sum of digits
while(num!=0){
sum += num % 10;
num = num / 10;
}

//output
printf("Sum: %d",sum);

return 0;

}
// Time complexity : O(N)
// Space complexity : O(1)

Find More Solutions at C Program to Find the Sum of Digits of a Number

//C++ Program
//Sum of digits in a number
#include
using namespace std;

int main ()
{
int num, sum = 0;

cout <<"Enter any num:"; cin >> num;

//loop to find sum of digits
while(num!=0){
sum += num % 10;
num = num / 10;
}

//output
cout <<"\nSum of digits : " << sum;

return 0;
}
// Time complexity : O(N)
// Space complexity : O(1)
// where N is number of digits in num

Find More Solutions at C++ Program to Find the Sum of Digits of a Number

public class Main
{
public static void main (String[]args)
{

int num = 12345, sum = 0;




//loop to find sum of digits
while(num!=0){
sum += num % 10;
num = num / 10;
}

//output
System.out.println ("Sum of digits : " + sum);
}

}

Find More Solutions at JAVA Program to Find the Sum of Digits of a Number

num = input("Enter Number: ")
sum = 0

for i in num:
sum = sum + int(i)

print(sum)

Find More Solutions at Python Program to Find the Sum of Digits of a Number

Question 3: Write a Program to Find out the Power of a Number

// pow function is contained in math.h library
#include<stdio.h>
#include <math.h>

int main() 
{
double base = 2.3;
double exp = 2.1;
double result;

// calculates the power
result = pow(base, exp);

// %lf used for double
printf("%lf ^ %lf = %lf\n", base, exp, result);

// following can be used for precision setting
printf("%.1lf ^ %.1lf = %.2lf", base, exp, result);

return 0;
}

Find More Solutions at C Program to find out the Power of a Number

// This method handles all cases, 
// when exponent/bases are integers/decimals or positive/negative
// pow function is contained in math.h library
#include
#include
using namespace std;

int main() 
{
double base = 1.5;
double expo1 = 2.5;
double expo2 = -2.5;
double res1, res2;

// calculates the power
res1 = pow(base, expo1);
res2 = pow(base, expo2);

cout << base << " ^ " << expo1 << " = " << res1 << endl;
cout << base << " ^ " << expo2 << " = " << res2 << endl;

return 0;
}

Find More Solutions at C++ Program to Find out the Power of a Number

public class Main
{
public static void main(String[] args) {

double base = 1.5;
double expo1 = 2.5;
double expo2 = -2.5;
double res1, res2;

// calculates the power
res1 = Math.pow(base, expo1);
res2 = Math.pow(base, expo2);
System.out.println(base + " ^ " + expo1 + " = " + res1 );
System.out.println(base + " ^ " + expo2 + " = " + res2 );
}
}

Find More Solutions at JAVA Program to Find out the Power of a Number

num, power = 3, 2
print(pow(num,power))

Find More Solutions at Python Program to Find out the Power of a Number

Question 4: Write a Program to Add two Fractions

#include<stdio.h>
int main()
{
//for initialize variables
int numerator1, denominator1, numerator2, denominator2, x, y, c, gcd_no;

//To take user input of numerators and denominators
printf("Enter the numerator for 1st number : ");
scanf("%d",&numerator1);
printf("Enter the denominator for 1st number : ");
scanf("%d",&denominator1);
printf("Enter the numerator for 2nd number : ");
scanf("%d",&numerator2);
printf("Enter the denominator for 2nd number : ");
scanf("%d",&denominator2);

//numerator
x=(numerator1*denominator2)+(denominator1*numerator2); 

//denominator
y=denominator1*denominator2; 

// Trick part. Reduce it to the simplest form by using gcd.
for(c=1; c <= x && c <= y; ++c)
{
if(x%c==0 && y%c==0)
gcd_no = c;
}

//To display fraction of givien numerators and denominators
printf("(%d / %d) + (%d / %d) = (%d / %d)", numerator1, denominator1, numerator2, denominator2, x/gcd_no, y/gcd_no);

return 0;
}

Find More Solutions at C Program to add two fractions

#include<iostream>
using namespace std;

// GCD function
int findGCD(int n1, int n2)
{
int gcd;
for(int i=1; i <= n1 && i <= n2; i++)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
return gcd;
}

// Main Program
int main()
{
int num1,den1;

//user input first fraction
cout << "Enter numerator and denominator of first number : "; cin >> num1 >> den1;

int num2,den2;

//user input second fraction
cout << "Enter numerator and denominator of second number: "; cin >> num2 >> den2;

//finding lcm of the denominators
int lcm = (den1*den2)/findGCD(den1,den2);

//finding the sum of the numbers
int sum=(num1*lcm/den1) + (num2*lcm/den2);

//normalizing numerator and denominator of result
int num3=sum/findGCD(sum,lcm);

lcm=lcm/findGCD(sum,lcm);

//printing output
cout<<num1<<"/"<<den1<<" + "<<num2<<"/"<<den2<<" = "<<num3<<"/"<<lcm;

return 0;
}

Find More Solutions at C++ Program for Addition of two Fractions

//Java program to add two fractions
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from the user 
System.out.print("Enter numerator for first fraction : "); 
int num1 = sc.nextInt();
System.out.print("Enter denominator for first fraction : "); 
int den1 = sc.nextInt();
System.out.print("Enter numerator for second fraction : "); 
int num2 = sc.nextInt();
System.out.print("Enter denominator for second fraction : "); 
int den2 = sc.nextInt();
int num, den, x;
System.out.print("("+num1+" / "+den1+") + ("+num2+" / "+den2+") = ");
//logic for calculating sum of two fractions
if(den1 == den2)
{
num = num1 + num2 ;
den = den1 ;
}
else{
num = (num1*den2) + (num2*den1);
den = den1 * den2;
}
if(num > den)
x = num;
else
x = den;
for(int i = 1 ; i <= x ; i++)
{
if(num%i == 0 && den%i == 0)
{
num = num/i;
den = den/i;
}
}
//logic for getting simplified fraction
int n = 1;
int p = num;
int q = den;
if( num != den)
{
while(n != 0)
{
//storing remainder
n = num % den; 
if(n != 0)
{
num = den;
den = n;
}
} 
}
System.out.println("("+p/den+" / "+q/den+")");
//closing scanner class(not compulsory, but good practice)
sc.close(); 
}
}

Find More Solutions at JAVA Program to Add two Fractions

def findGCD(n1, n2):
gcd = 0
for i in range(1, int(min(n1, n2)) + 1):
if n1 % i == 0 and n2 % i == 0:
gcd = i
return gcd


# input first fraction
num1, den1 = map(int, list(input("Enter numerator and denominator of first number : ").split(" ")))

# input first fraction
num2, den2 = map(int, list(input("Enter numerator and denominator of second number: ").split(" ")))

lcm = (den1 * den2) // findGCD(den1, den2)

sum = (num1 * lcm // den1) + (num2 * lcm // den2)

num3 = sum // findGCD(sum, lcm)

lcm = lcm // findGCD(sum, lcm)

print(num1, "/", den1, " + ", num2, "/", den2, " = ", num3, "/", lcm)

Find More Solutions at Python Program to add two fractions

Question 5: Write a Program to Find the Largest Element in an Array.

// C Program to find largest element in an array
#include<stdio.h>

int getLargest(int arr[], int len)
{
// assign first array element as largest
int max = arr[0];

// linearly search for the largest element
for(int i=1; i max)
max = arr[i];
}

return max;

}
int main()
{
int arr[] = {20, 5, 35, 40, 10, 50, 15};

// get the length of the array
int len = sizeof(arr)/sizeof(arr[0]); 

printf("The Largest element is: %d", getLargest(arr, len));
}
// Time complexity: O(N)
// Space complexity: O(N)

Find More Solutions at C Program to Find the Largest Element in an Array

#include<bits/stdc++.h>
using namespace std;

int main(){

int arr[]={10, 89, 67, 56, 45, 78};
int n = sizeof(arr)/sizeof(arr[0]);
int max_element = INT_MIN;

for(int i=0; i<n; i++){ if(arr[i]>max_element)
max_element = arr[i];
}

cout<<max_element;
}

Find More Solutions at C++ Program to Find the Largest Element in an Array

import java.util.Scanner;

public class Main
{
public static void main(String args[])
{

int arr[] = {12, 13, 1, 10, 34, 10};

int max = arr[0];

for(int i=0; i<arr.length; i++)
{
if(max < arr[i])
{
max = arr[i];
}

}

System.out.print(max); 
}
}

Find More Solutions at JAVA Program to Find the Largest Element in an Array

a = [10, 89, 9, 56, 4, 80, 8]
max_element = a[0]

for i in range(len(a)):
if a[i] > max_element:
max_element = a[i]

print (max_element)

Find More Solutions at Python Program to Find the Largest Element in an Array

Question 6: Write a Program to Find the Roots of a Quadratic Equation

#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>

void findRoots(int a, int b, int c)
{
if (a == 0) {
printf("Invalid");
return;
}

int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));

if (d > 0) {
printf("Roots are real and different \n");
printf("%f\n%f", (double)(-b + sqrt_val) / (2 * a),(double)(-b - sqrt_val) / (2 * a));
}
else if (d == 0) {
printf("Roots are real and same \n");
printf("%f", -(double)b / (2 * a));
}
else // d < 0
{
printf("Roots are complex \n");
printf("%f + i%f\n%f - i%f", -(double)b / (2 * a), sqrt_val/(2 * a), -(double)b / (2 * a), sqrt_val/(2 * a));
}
}

int main()


{
int a = 1, b = 4, c = 4;

findRoots(a, b, c);
return 0;
}

Find More Solutions at C Program to Find the Roots of a Quadratic Equation

/* Write a program to find roots of a quadratic equation in C++*/
#include 
using namespace std;

void findRoots(int a, int b, int c)
{
if (a == 0) {
cout << "Invalid";
return;
}

int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));

if (d > 0) {
cout << "Roots are real and different \n";
cout << (double)(-b + sqrt_val) / (2 * a) << "\n"<< (double)(-b - sqrt_val) / (2 * a);
}
else if (d == 0) {
cout << "Roots are real and same \n";
cout << -(double)b / (2 * a);
}
else // d < 0
{
cout << "Roots are complex \n";
cout << -(double)b / (2 * a) << " + i" << sqrt_val<< "\n" << -(double)b / (2 * a) << " - i" << sqrt_val;
}
}

// Driver code
int main()
{
int a = 1, b = 4, c = 4;

findRoots(a, b, c);
return 0;
}

Find More Solutions at C++ Program to Find the Roots of a Quadratic Equation

import java.io.*;
import static java.lang.Math.*;
class Main{

static void findRoots(int a, int b, int c)
{
if (a == 0) {
System.out.println("Invalid");
return;
}

int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));

if (d > 0) {
System.out.println("Roots are real and different");
System.out.println((double)(-b + sqrt_val) / (2 * a) + "\n"+ (double)(-b - sqrt_val) / (2 * a));
}
else if (d == 0) {
System.out.println("Roots are real and same ");
System.out.println(-(double)b / (2 * a) + "\n" + -(double)b / (2 * a));
}
else // d < 0
{
System.out.println("Roots are complex");

System.out.println(-(double)b / (2 * a) + " + i" + sqrt_val + "\n" + -(double)b / (2 * a) + " - i" + sqrt_val);
}
}

// Driver code
public static void main(String args[])
{

int a = 1, b = 4, c = 4;

// Function call
findRoots(a, b, c);
}
}

Find More Solutions at JAVA Program to Find the Roots of a Quadratic Equation

# Write a program to find roots of a quadratic equation in Python
import math

def findRoots(a, b, c):

if a == 0:
print("Invalid")
return -1

d = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(d))

if d > 0:
print("Roots are real and different ")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif d == 0:
print("Roots are real and same")
print(-b / (2*a))
else: # d<0
print("Roots are complex")
print(- b / (2*a), " + i", sqrt_val)
print(- b / (2*a), " - i", sqrt_val)


# Driver Program
a = 1
b = 4
c = 4

# Function call
findRoots(a, b, c)

Find More Solutions at Python Program to Find the Roots of a Quadratic

Question 7: Write a Program to Find the Prime Factors of a Number.

#include<stdio.h>

void primefactor(int num) {

printf("Prime factors of the number : ");
for (int i = 2; num > 1; i++) {

while (num % i == 0) {
printf("%d ", i);
num = num / i;
}
}
}
int main() {

int num;
printf("Enter the positive integer: ");
scanf("%d", &num);

primefactor(num);
return 0;
}

Find More Solutions at C Program to Find the Prime Factors of a Number

// C++ program to print all prime factors 
#include 
using namespace std;

void primeFactors(int n) 
{ 
while (n % 2 == 0) 
{ 
cout << 2 << " "; 
n = n/2; 
}

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

while (n % i == 0) 
{ 
cout << i << " "; 
n = n/i; 
} 
}

if (n > 2) 
cout << n << " "; 
}

// Driver code 
int main() 
{ 
int n = 315; 
primeFactors(n); 
return 0; 
}

Find More Solutions at C++ Program to Find the Prime Factors of a Number

import java.io.*;
import java.lang.Math;

class Main {

public static int isprime(int n){

for(int i = 2; i<=Math.sqrt(n); i++){
if(n%i==0)
return 0;
}

return 1;
}

public static void primeFactors(int n)
{

for(int i = 2; i<= n; i++){
if(isprime(i)==1){
int x = n;
while(x%i==0){
System.out.print(i + " ");
x /= i;
}
}
}

}

public static void main(String[] args)
{
int n = 90;
primeFactors(n);
}
}

Find More Solutions at JAVA Program to Find the Prime Factor of a Number

def Prime_Factorial(n):
if n < 4:
return n
arr = []
while n > 1:
for i in range(2, int(2+n//2)):
if i == (1 + n // 2):
arr.append(n)
n = n // n
if n % i == 0:
arr.append(i)
n = n // i
break
return arr


n = 210
print(Prime_Factorial(n))

Find More Solutions at Python Program to Find the Prime Factor of a Number

Question 8: Write a Program to Convert Digits to Words.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void convert_to_words(char* num)
{
int len = strlen(num);

/* Base cases */
if (len == 0) {
fprintf(stderr, "empty string\n");
return;
}
if (len > 4) {
fprintf(stderr,
"Length more than 4 is not supported\n");
return;
}

char* single_digits[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

char* two_digits[]= { "", "ten", "eleven","twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

char* tens_multiple[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

char* tens_power[] = { "hundred", "thousand" };

printf("\n%s: ", num);

if (len == 1) {
printf("%s\n", single_digits[*num - '0']);
return;
}

while (*num != '\0') {

if (len >= 3) {
if (*num - '0' != 0) {
printf("%s ", single_digits[*num - '0']);
printf("%s ", tens_power[len - 3]); 
}
--len;
}

else {
if (*num == '1') {
int sum = *num - '0' + *(num + 1) - '0';
printf("%s\n", two_digits[sum]);
return;
}

else if (*num == '2' && *(num + 1) == '0') {
printf("twenty\n");
return;
}

else {
int i = *num - '0';
printf("%s ", i ? tens_multiple[i] : "");
++num;
if (*num != '0')
printf("%s ",
single_digits[*num - '0']);
}
}
++num;
}
}

int main(void)
{
convert_to_words("9459");
return 0;
}

Find More Solutions at C Program to Convert Digits to Words

#include<bits/stdc++.h>
using namespace std;

void numToWords(string num){

int length_of_string = num.size();

if (length_of_string == 0){
cout<<"String is Empty"; return; } if (length_of_string > 4){
cout<<"Please enter the string with supported length"; 
return; 
}

string ones_digits[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

string tens_digits[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen","nineteen"};

string multiple_of_ten[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

string power_of_ten[] = {"hundred", "thousand"};

cout<<num<<":\n";

if (length_of_string == 1){ 
cout<<ones_digits[num[0] - '0']; 
//return;
}

int x=0;

while (x < num.size()){ if(length_of_string >= 3){
if (num[x] - 48 != 0){
cout<<ones_digits[num[x] - 48]<<"\n";
cout<<power_of_ten[length_of_string - 3]<<"\n";
length_of_string--;
}
}
else{
if (num[x] - 48 == 1){
int sum = (num[x] - 48 + num[x] - 48); 
cout<<tens_digits[sum]; 
// return;
}

else if(num[x] - 48 == 2 and num[x + 1] - 48 == 0){
cout<<"twenty"; //return; } else{ int i = num[x] - 48; if(i > 0){
cout<<multiple_of_ten[i]<<" "; 
}
else{ 
cout<<" ";
}
x += 1; 
if(num[x] - 48 != 0){
cout<<ones_digits[num[x] - 48];
}

}

}

x++;
}

}

int main(){

numToWords("1121");
return 0;

}

Find More Solutions at C++ Program to Convert Digits to Words

class Main {

static void convert_to_words(char[] num)
{

int len = num.length;

// Base cases
if (len == 0) {
System.out.println("empty string");
return;
}
if (len > 4) {
System.out.println(
"Length more than 4 is not supported");
return;
}

String[] single_digits = new String[] {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};

String[] two_digits = new String[] {
"", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"
};

String[] tens_multiple = new String[] {
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"
};

String[] tens_power = new String[] { "hundred", "thousand" };

System.out.print(String.valueOf(num) + ": ");

if (len == 1) {
System.out.println(single_digits[num[0] - '0']);
return;
}

int x = 0;
while (x < num.length) {

if (len >= 3) {
if (num[x] - '0' != 0) {
System.out.print(single_digits[num[x] - '0'] + " ");
System.out.print(tens_power[len - 3] + " ");

}
--len;
}

else {

if (num[x] - '0' == 1) {
int sum
= num[x] - '0' + num[x + 1] - '0';
System.out.println(two_digits[sum]);
return;
}

else if (num[x] - '0' == 2
&& num[x + 1] - '0' == 0) {
System.out.println("twenty");
return;
}

else {
int i = (num[x] - '0');
if (i > 0)
System.out.print(tens_multiple[i] + " ");
else
System.out.print("");
++x;
if (num[x] - '0' != 0)
System.out.println(single_digits[num[x] - '0']);
}
}
++x;
}
}

// Driver Code
public static void main(String[] args)
{
convert_to_words("1121".toCharArray());
}
}

Find More Solutions at JAVA Program to Convert Digits to Words

from num2words import num2words

# Most common usage.
print(num2words(11098))

# can also specify this for -th format
print(num2words(11098, to='ordinal'))

Find More Solutions at Python Program to Convert Digits to Words

Question 9: Write a Program to Find the Factorial of a Number using Recursion.

#include<stdio.h>
int getFactorial(int num)
{
if(num == 0)
return 1;

return num * getFactorial(num-1);
}
int main ()
{
int num = 7;

int fact = getFactorial(num);

printf("Fact %d: %d",num, fact);
}

Find More Solutions at C Program to Find the Factorial of a Number using Recursion

#include<iostream>
using namespace std;
int main ()
{
int num = 6, fact = 1;

// Factorial of negative number doesn't exist
// Read more here - https://www.quora.com/Is-the-factorial-of-a-negative-number-possible
if(num < 0)
cout << "Not Possible";
else
{
for(int i = 1; i <= num; i++)
fact = fact * i;
}

cout << "Fact " << num << ": " << fact;
}
// Time complexity: O(N)
// Space complexity: O(1)

Find More Solutions at C++ Program to Find the Factorial of a Number using Recursion

class Main {
// method to find factorial of given number
static int factorial(int n)
{
if (n == 0)
return 1;

return n * factorial(n - 1);
}

// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(5));
}
}

Find More Solutions at JAVA Program to Find the Factorial of a Number using Recursion

def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)


num = 5
print("Factorial of", num, "is", factorial(num))

Find More Solutions at Python Program to Find the Factorial of a Number usinf Recursion

Question 10: Write a Program to Reverse an Array

#include <stdio.h>

void printReverse(int arr[], int len){

for(int i = len - 1; i >= 0; i--)
printf("%d ", arr[i]);
}

int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};

int len = sizeof(arr)/sizeof(arr[0]);

printf("Array in Reverse:\n");
printReverse(arr, len);

return 0;
}
Find More Solutions at C Program to Reverse an Array
>#include<bits/stdc++.h>
using namespace std;

int main(){

int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr)/sizeof(arr[0]);

for(int i=n-1; i>=0; i--)
cout<<arr[i]<<" ";
}

Find More Solutions at C ++ Program to Reverse an Array

import java.util.Scanner;

public class Main
{
public static void main(String args[])
{

int arr[] = {10, 20, 30, 40, 50};

int n=arr.length;
for(int i=n-1; i>=0; i--)
System.out.print(arr[i]+" "); 
}
}

Find More Solutions at JAVA Program to Reverse an Array

def reverseList(A, start, end):
while start < end:
A[start], A[end] = A[end], A[start]
start += 1
end -= 1
# Driver function to test above function
A = [10, 20, 30, 40, 50]
reverseList(A, 0, 4)
print(A)

Find More Solutions at Python Program to Reverse an Array

More Cognizant Coding Questions:-

  • Write a program to add two numbers without using the addition operator.
  • Write a program to subtract two numbers without using the subtraction operator.
  • Write a program to find the largest among three numbers using the binary minus operator.
  • Write a program to find out the largest among three numbers using a conditional operator.
  • Write a program to find out the generic root of any number.
  • Write a program to find out the NCR factor of a given number.
  • Write a program to convert string to int without using library functions.
  • Write a program to print 1 to 100 without using loop.
  • Write a program to swap two numbers.
  • Write a program to find the largest of n numbers.
  • Write a program to split numbers into digits.
  • Write a program to count the number of digits in a string.

6 comments on “Cognizant Coding Interview Questions”


  • Shubham

    4. Write a c program to add two numbers without using addition operator.
    #include
    int main()
    {
    int a,b,c;
    printf(“enter first number : “);
    scanf(“%d”,&a);

    printf(“enter second number : “);
    scanf(“%d”,&b);
    for(c=0;c<b;c++)
    {
    a++;
    }
    printf("the total is : %d",a);
    return 0;
    }


  • Shubham

    4. Write a c program to add two numbers without using addition operator.
    #include

    int main()
    {

    int a,b,c;

    printf(“enter first number : “);
    scanf(“%d”,&a);

    printf(“enter second number : “);
    scanf(“%d”,&b);

    for(c=0;c<b;c++)
    {
    a++;
    }

    printf("the total is : %d",a);

    return 0;
    }


  • Yatheesha

    Dear sir/madam
    If you don’t mine can you share the answers of the above questions.
    Thank you
    Regards
    Yatheesha


    • N

      Please share the answers too mam.some of them are easy to solve but other questions we didn’t get idea.


    • ashish

      Q 2 summ of a digit of a number:

      def digitsum(n):
      if n==0:
      return 0
      else:
      return n%10 + digitsum(n//10)

      print(digitsum(10006))

      Q power of a number

      def num_power(n,p):
      answer=1
      while p>0:
      answer = answer*n
      p-=1
      return answer
      print(num_power(8,0))

      Q add two numbers without + operator

      def Add(x, y):
      while (y != 0):
      carry = x & y
      x = x ^ y
      y = carry << 1
      return x

      print(Add(2, 3))

      Q sub two numbers without – operator

      def subtract(x, y):
      while (y != 0):
      borrow = (~x) & y
      x = x ^ y
      y = borrow << 1
      return x
      print(subtract(10,9))

      Q find prime number in a given range
      def prime_num(n):
      if n0:
      counter+=1
      n=n//10
      print(n)
      return counter

      print(digit_counter(1234))