0% found this document useful (0 votes)
66 views16 pages

Questens

The document describes a program to simulate a jar of candies for sale at a mall counter. The jar can hold a maximum of N candies and must always contain at least K candies. The program takes input for the number of candies a customer orders, updates the number of candies in the jar, and displays the number of candies sold and remaining. It returns an error message if the input amount is invalid. Sample input and output examples are provided.

Uploaded by

priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views16 pages

Questens

The document describes a program to simulate a jar of candies for sale at a mall counter. The jar can hold a maximum of N candies and must always contain at least K candies. The program takes input for the number of candies a customer orders, updates the number of candies in the jar, and displays the number of candies sold and remaining. It returns an error message if the input amount is invalid. Sample input and output examples are provided.

Uploaded by

priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

There is a JAR full of candies for sale at a mall counter.

JAR has the capacity N, that is JAR can contain


maximum N candies when JAR is full. At any point of time. JAR can have M number of Candies where M<=N.
Candies are served to the customers. JAR is never remain empty as when last k candies are left. JAR if refilled
with new candies in such a way that JAR get full.
Write a code to implement above scenario. Display JAR at counter with available number of candies. Input should
be the number of candies one customer can order at point of time. Update the JAR after each purchase and
display JAR at Counter.

Output should give number of Candies sold and updated number of Candies in JAR.

If Input is more than candies in JAR, return: “INVALID INPUT”

Given, 

N=10, where N is NUMBER OF CANDIES AVAILABLE

K =< 5, where k is number of minimum candies that must be inside JAR ever.

Example 1:(N = 10, k =< 5)

 Input Value

o 3

 Output Value

o NUMBER OF CANDIES SOLD : 3

o NUMBER OF CANDIES AVAILABLE : 7

Example : (N=10, k<=5)

 Input Value

o 0

 Output Value

o INVALID INPUT

o NUMBER OF CANDIES LEFT : 10


import java.util.Scanner;
class Main{
public static void main(String[] args) {
int n = 10, k = 5;
int num;
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
if(num >= 1 && num <= 5) {
System.out.println("NUMBER OF CANDIES SOLD : " + num);
System.out.print("NUMBER OF CANDIES LEFT : " + (n - num));
} else {
System.out.println("INVALID INPUT");
System.out.print("NUMBER OF CANDIES LEFT : " + n);
}
}
}

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.

 Average Oxygen Values should be rounded.

Example 1:

 INPUT VALUES

            95

            92

            95

            92

            90

            92

            90

            92
            90

 OUTPUT VALUES

o Trainee Number : 1

o Trainee Number : 3

Note:

Input should be 9 integer values representing oxygen levels entered in order as

Round 1

 Oxygen value of trainee 1

 Oxygen value of trainee 2

 Oxygen value of trainee 3

Round 2

 Oxygen value of trainee 1

 Oxygen value of trainee 2

 Oxygen value of trainee 3

Round 3

 Oxygen value of trainee 1

 Oxygen value of trainee 2

 Oxygen value of trainee 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

Checking if a given year is leap year or not

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 a leap year

/*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: "))

#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.

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*/

//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");
}
}

Que 5

Find the 15th term of the series?

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 – – – – – – }

                        And even term is a increment of 6 {0, 6, 12, 18, 24, 30 – – – – – – }

//Java program to find 15th element of the series


class Main
{
public static void main(String[] args)
{
int a = 7, b = 0,c;
System.out.println("Series :");
for(int i = 1 ; i < 8 ; i++)
{
c = a * b;
System.out.print(c+" "+(c-b)+" ");
b++;
}
c = a * b;
System.out.println(c);
System.out.print("15th element of the series is = "+c);
}
}

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.

You can assume that N will not exceed 30.

//Java program to find nth element of the series


import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//input value of n
System.out.print("Enter the value of n : ");
int n = sc.nextInt();
int a = 1, b = 1;
//statement for even value of n
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a * 2;
b = b * 3;
}
System.out.print(n+" element of the series is = "+b);
}
//statement for odd value of n
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a * 2;
b = b * 3;
}
a = a * 2;
System.out.print(n+" element of the series is = "+a);
}
}
}

Que7

Consider the below series :

0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8

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)

Write a program to find the nth term in this 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 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.

You can assume that the n will not exceed 20,000.

//Java program to find nth element of the series


import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 0, b = 0;
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
System.out.print(b);
}
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
a = a + 2;
System.out.print(a);
}
}
}

Que.

1. The program will recieve 3 English words inputs from STDIN

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

5. Then concatenate the three words and print them

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();

String str1 = "";


String str2 = "";
String str3 = "";
char c;
for(int i = 0 ; i < l1 ; i++)
{
c = s1.charAt(i);
if(c == 'A' || c == 'a' || c == 'E' ||
c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c ==
'U' || c == 'u')
str1 = str1 + "%";
else
str1 = str1 + c;
}
for(int i = 0 ; i < l2 ; i++)
{
c = s2.charAt(i);
if((c >= 'A' && c <= 'Z')||(c >= 'a' && c <= 'z'))
{
if(c == 'A' || c == 'a' || c == 'E' || c == 'e' ||
c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U' || c
== 'u')
str2 = str2 + c;
else
str2 = str2 + "#";
}
else
str2 = str2 + c;
}
str3 = s3.toUpperCase();
System.out.println(str1+str2+str3);
}
}

que.

Addition of two numbers a Twist

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.

Consider the below series :

0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8

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)

Write a program to find the nth term in this 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 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.

You can assume that the n will not exceed 20,000.

#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;

int res = term_in_series – 1;


printf(“%d “, res);
}

return 0;
}

Que.

You might also like