Questens
Questens
Output should give number of Candies sold and updated number of Candies in JAR.
Given,
K =< 5, where k is number of minimum candies that must be inside JAR ever.
Input Value
o 3
Output Value
Input Value
o 0
Output Value
o INVALID INPUT
C++
#include <iostream.h>
using namespace std;
int main()
{
int n=10, k=5;
int num;
cin>>num;
if(num>=1 && num<=5)
{
cout<< "NUMBER OF CANDIES SOLD : "<<num<<"\n";
cout<<"NUMBER OF CANDIES LEFT : "<<n-num;
}
else
{
cout<<"INVALID INPUT\n";
cout<<"NUMBER OF CANDIES LEFT : "<<n;
}
return 0;
}
Python
total_candies = 10
no_of_candies = int(input())
if no_of_candies in range(1, 6):
print('No. of Candies Sold:',no_of_candies)
print('No. of Candies Left:',total_candies-no_of_candies)
else:
print("Invalid Input")
print('No. of Candies Left:',total_candies)
que2.
Selection of MPCS exams include a fitness test which is conducted on ground. There will be a batch of 3 trainees,
appearing for running test in track for 3 rounds. You need to record their oxygen level after every round. After
trainee are finished with all rounds, calculate for each trainee his average oxygen level over the 3 rounds and
select one with highest oxygen level as the most fit trainee. If more than one trainee attains the same highest
average level, they all need to be selected.
Display the most fit trainee (or trainees) and the highest average oxygen level.
Note:
The oxygen value entered should not be accepted if it is not in the range between 1 and 100.
If the calculated maximum average oxygen value of trainees is below 70 then declare the trainees as unfit
with meaningful message as “All trainees are unfit.
Example 1:
INPUT VALUES
95
92
95
92
90
92
90
92
90
OUTPUT VALUES
o Trainee Number : 1
o Trainee Number : 3
Note:
Round 1
Round 2
Round 3
Output must be in given format as in above example. For any wrong input final output should display
“INVALID INPUT”
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int[][] trainee = new int[3][3];
int[] average = new int[3];
int max = 0;
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
trainee[i][j] = sc.nextInt();
if(trainee[i][j] < 1 || trainee[i][j] > 100) {
trainee[i][j] = 0;
}
}
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
average[i] = average[i] + trainee[j][i];
}
average[i] = average[i] / 3;
}
for(int i = 0; i < 3; i++) {
if(average[i] > max) {
max = average[i];
}
}
for(int i = 0; i < 3; i++) {
if(average[i] == max) {
System.out.println("Trainee Number : " + (i + 1));
}
if(average[i] <70) {
System.out.print("Trainee is Unfit");
}
}
}
C++
#include<iostream.h>
using namespace std;
int main()
{
int trainee[3][3];
int average[3] = {0};
int i, j, max=0;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++) { cin>>trainee[i][j];
if(trainee[i][j]<1 || trainee[i][j]>100)
{
trainee[i][j] = 0;
}
}
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
average[i] = average[i] + trainee[j][i];
}
average[i] = average[i] / 3;
}
for(i=0; i<3; i++) { if(average[i]>max)
{
max = average[i];
}
}
for(i=0; i<3; i++)
{
if(average[i]==max)
{
cout<<"Trainee Number : "<<i+1<<"\n";
}
if(average[i]<70)
{
cout<<"Trainee is Unfit";
}
}
return 0;
}
Python
trainee = [[],[],[],[]]
for i in range(3):
for j in range(3):
trainee[i].append(int(input()))
if (trainee[i][-1]) not in range(1,101):
print("invalid input")
for i in range(3):
trainee[3].append((trainee[2][i]+trainee[1][i]+trainee[0][i])//3)
maximum = max(trainee[3])
for i in range(3):
if trainee[3][i] < 70 :
print("Trainee {0} is unfit".format(i+1))
elif trainee[3][i] == maximum:
print("Trainee Number: ",i+1)
que3
Explanation:
Step 1:
Step 2:
Step 3:
/*Java program to check whether a year entered by user is a leap year or not and a leap
year is a year
which is completely divisible by 4,but the year should not be a century year except it is
divisible by 400*/
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.");
}
}
C++
//C++ Program
//Leap year or not
#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;
}
Python
#python program to check if a year number taken from the user is a leap year or not, using
nested if-else.
num = int(input("Enter the year you want to check if is leap year or not: "))
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):
#the input year is divisible by 4, 100 and 400, hence leap year.
else:
else:
else:
#if the input num is not divisible by 4 then it can not be a leap year altogether.
Que4
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.
/*Java program to check whether a number entered by user is prime or not for only positive
numbers,
if the number is negative then ask the user to re-enter the number*/
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");
}
}
Que 5
0,0,7,6,14,12,21,18, 28
Explanation : In this series the odd term is increment of 7 {0, 7, 14, 21, 28, 35 – – – – – – }
Que 6
Consider the following series: 1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64, 729, 128, 2187 …
This series is a mixture of 2 series – all the odd terms in this series form a geometric series and all the even terms
form yet another geometric series. Write a program to find the Nth term in the series.
The value N in a positive integer that should be read from STDIN. The Nth term that is calculated by the program
should be written to STDOUT. Other than value of n th term,no other character / string or message should be
written to STDOUT. For example , if N=16, the 16th term in the series is 2187, so only value 2187 should be
printed to STDOUT.
Que7
This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and
every even terms is derived from the previous term using the formula (x/2)
The value n in a positive integer that should be read from STDIN the nth term that is calculated by the program
should be written to STDOUT. Other than the value of the nth term no other characters /strings or message
should be written to STDOUT.
For example if n=10,the 10 th term in the series is to be derived from the 9th term in the series. The 9th term is 8
so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.
Que.
1. These three words will be read one at a time, in three separate line
2. The first word should be changed like all vowels should be replaced by %
3. The second word should be changed like all consonants should be replaced by #
4. The third word should be changed like all char should be converted to upper case
Other than these concatenated word, no other characters/string should or message should be written to STDOUT
For example if you print how are you then output should be h%wa#eYOU.
You can assume that input of each word will not exceed more than 5 chars
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter three words : ");
String s1 = sc.next();
String s2 = sc.next();
String s3 = sc.next();
int l1 = s1.length();
int l2 = s2.length();
que.
1. Using a method, pass two variables and find the sum of two numbers.
Test case:
Number 1 – 20
Number 2 – 20.38
Sum = 40.38
There were a total of 4 test cases. Once you compile 3 of them will be shown to you and 1 will be a hidden one.
You have to display error message if numbers are not numeric.
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Number 1 : ");
int num1 = sc.nextInt();
System.out.print("Number 2 : ");
float num2 = sc.nextFloat();
float sum = num1 + num2;
System.out.println("Sum = "+sum);
}
}
que.
This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and
every even terms is derived from the previous term using the formula (x/2)
The value n in a positive integer that should be read from STDIN the nth term that is calculated by the program
should be written to STDOUT. Other than the value of the nth term no other characters /strings or message
should be written to STDOUT.
For example if n=10,the 10 th term in the series is to be derived from the 9th term in the series. The 9th term is 8
so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.
#include<stdio.h>
int main() {
//code
int n;
scanf(“%d”, &n);
if(n % 2 == 1)
{
int a = 1;
int r = 2;
int term_in_series = (n+1)/2;
int res = 2 * (term_in_series – 1);
printf(“%d “, res);
}
else
{
int a = 1;
int r = 3;
int term_in_series = n/2;
return 0;
}
Que.