TCS NQT - Coding Ability

Test Number 6


1 .Explanation:

To check whether a year is leap or not

Step 1:

We first divide the year by 4.
If it is not divisible by 4 then it is not a leap year.
If it is divisible by 4 leaving remainder 0
Step 2:

We divide the year by 100
If it is not divisible by 100 then it is a leap year.
If it is divisible by 100 leaving remainder 0
Step 3:

We divide the year by 400
If it is not divisible by 400 then it is a leap year.
If it is divisible by 400 leaving remainder 0
Then it is not a leap year

         

#include< stdio.h >
int leapprog(int year)
{
//checking divisibility by 4
if(year%4 == 0)
{
//checking divisibility by 100
if( year%100 == 0)
{
//checking divisibility by 400
if ( year%400 == 0)
printf("%d, the year entered happens to be a leap year", year);
else
printf("%d is surely not a leap year", year);
}
else
printf("%d, the year entered happens to be a leap year", year );
}
else
printf("%d is surely not a leap year", year);
return 0;
}
int main()
{
int input_year, val;
printf("Enter the year that you want to check"); //enter the year to check
scanf("%d",&input_year);
val = leapprog(input_year);
return 0;
}

         

#include<stdio.h>
using namespace std;
//main program
int main()
{
//initialising variables
int year;
cout<<"Enter year to check: ";
//user input
cin>>year;
//checking for leap year
if( ((year % 4 == 0)&&(year % 100 != 0)) || (year % 400==0) )
{
//input is a leap year
cout<<year<<" is a leap year";
}
else
{
//input is not a leap year
cout<<year<< " is not a leap year";
}
return 0;
}

       

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc=new Scanner(System.in);
//input year from user
System.out.println("Enter a Year");
int year = sc.nextInt();
//condition for checking year entered by user is a leap year or not
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}

  

num = int(input("Enter the year you want to check if is leap year or not: "))

#take input year from the user to check if it is a leap year

if(num%4 == 0):

#check if the number is divisible by 4 and if true move to next loop

if(num%100 == 0):

#check if the input year is divisible by 100 and if true move to next loop

if(num%400 == 0):

print("The year {} is a leap year".format(num))

#the input year is divisible by 4, 100 and 400, hence leap year.

else:

print("The year {} is Not a leap year".format(num))

else:

print("The year {} is a leap year".format(num))

#if the number is divisible by both 4 and 100 it is a leap year

else:

print("The year {} is Not a leap year".format(num))

#if the input num is not divisible by 4 then it can not be a leap year altogether.





";

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")