TCS NQT - Coding Ability

Test Number 12


1. Given a maximum of 100 digit numbers as input, find the difference between the sum of odd and even position digits.

Input 1:

4567

Expected output:

2

 

Explanation

The Sum of odd position digits 4 and 6 is 10. The Sum of even position digits 5 and 7 is 12. The difference is 12-10=2.

 

Input #2:

9834698765123

       

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

using namespace std;

int main()
{
int a = 0,b = 0,i = 0, n;
char num[100];

cin>> num;
n = strlen(num);
while(n>0)
{
if(i==0)
{
a+=num[n-1]-48;
n--;
i=1;
}
else
{
b+=num[n-1]-48;
n--;
i=0;
}
}
cout<< abs(a-b);

return 0;
}

        

import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String num = sc.nextLine();
int Osum=0,Esum=0;
for(int i=0;i<num.length();i++)
{
int n = (int)(num.charAt(i)-'0');
if(i%2==0)
Esum+=n;
else
Osum+=n;
}
System.out.println(Math.abs(Esum-Osum));
}
}

  

num=input()
Esum=int(0)
Osum=int(0)
for i in range(0,len(num)):
if(i%2==0):
Esum+=int(num[i])
else:
Osum+=int(num[i])
print(int(abs(Esum-Osum)))





";

2. Ques. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not?

Whether the number is positive or not, if it is negative then print the message “please enter the positive number”
It is positive then call the function prime and check whether the take positive number is prime or not.

#include<stdio.h>
void prime(int n)
{
int c=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
c = c+1;
}
if(c>=1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
}
void main()
{
int n;
printf("Enter no : "); //enter the number
scanf("%d",&n);
if(n<0)
{
printf("Please enter a positive integer");
}
else
prime(n);
}

#include
using namespace std;
//function declaration
void enter();
void check(int);
void prime(int);
//main program
int main()
{
enter();
return 0;

}
//function to enter value
void enter()
{
int num;
cout<<"Enter number:";
cin>>num;
check(num);
}
//function to check whether the input is positive or negative
void check(int num)
{
if(num<0)
{
cout<<"invalid input enter value again"<<endl;
enter();
}
else
{
prime(num);
}
}
//function to check for prime number
void prime(int num)
{
int i,div=0;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
div++;
}
}
//prime number only have two divisors
if(div==2)
{
cout<<num<<" is a prime number";
}
//not a prime number
else
{
cout<}

Output:

Enter number:29

29 is a prime number.

if the number is negative then ask the user to re-enter the number*/

//Prime number is a number which is divisible by 1 and another by itself only.

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//input a number from user
System.out.println("Enter the number to be checked : ");
int n = sc.nextInt();
//create object of class CheckPrime
Main ob=new Main();
//calling function with value n, as parameter
ob.check(n);
}
//function for checking number is positive or negative
void check(int n)
{
if(n<0)
System.out.println("Please enter a positive integer");
else
prime(n);
}
//function for checking number is prime or not
void prime(int n)
{
int c=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
++c;
}
if(c>=1)
System.out.println("Entered number is not a prime number");
else
System.out.println("Entered number is a prime number");
}
}

def prime(n):

if n > 1:

for i in range(2, n):

if (n % i) == 0:

print(n, "is not a prime number")

break

else:

print(n, "is a prime number")

num = int(input("enter a number: "))

if (num > 0):

prime(num)

else:

print("please enter a positive number")