91.
Odd Even Average
The Owner of a block visited the Layout and found that he has some plot numbers of his own
and some are odd numbers and some are even numbers. He is maintaining the details in a file
in the system. For the password protection our owner has followed one formula. He
calculated the sum of his even numbers plot and sum of odd numbers plot and found the
average of those two and he used that average as his password for the details file. Find the
password that our owner has arrived.
Include a function named avgOddEvenSum that accepts 2 arguments and returns a float. The
first argument is the input array and the second argument is an int that corresponds to the size
of the array. The function returns a float that corresponds to the average of the array.
If the size of the array is negative or if any element in the array is negative , print “Invalid
Input” and terminate the program.
Input and Output Format:
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in
the array. The next 'n' integers correspond to the elements in the array.
Output consists of a floating point number that corresponds to the average. It is displayed
correct to 2 decimal places.
Assume that the maximum size of the array is 20.
Sample Input 1:
5
1
2
3
4
5
Sample Output 1:
7.50
Sample Input 2:
-5
Sample Output 2:
Invalid Input
Sample Input 3:
5
23
2
-5
Sample Output 3:
Invalid Input
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, i, sumodd=0,sumeven=0;
// int flag=0;
Scanner in=new Scanner(System.in);
n = in.nextInt();
if(n < 0)
{
System.out.print("Invalid array size");
System.exit(0);
}
else
{
int a[]=new int[n];
for(i = 0; i< n; i++)
{
a[i] = in.nextInt();
if(a[i] < 0)
{
//flag=1;
System.out.print("Invalid input");
System.exit(0);
}
}
// if(flag!=1)
//{
double avg,av;
for(i=0;i<n;i++)
{
if(a [i]%2==0)
sumeven=sumeven+a[i];
else
sumodd=sumodd+a[i];
}
avg = (sumeven+sumodd);
av=avg/2;
System.out.printf("%.2f",av);
//}
}
}
}
92.convertToBinary
Read the question carefully and follow the input and output format.
Kate is a military officer. He needs to send top-secret code to the other soldiers in a different
place. He need to encrypt it. We need to write a function to convert the given decimal number
to the corresponding binary number.
Input consist of single integer. Output consists of binary number.
1) Print "Number too small" when input is negative.
2) Print "Number too large" when input value is greater than 100.
Include a function named convertToBinary(int num) whose return type is void.
Sample Input 1:
12
Sample Output 1:
1100
Sample Input 2:
101
Sample Output 2:
Number too large
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n,i=0;
Scanner in=new Scanner(System.in);
n = in.nextInt();
if(n<0)
{
System.out.print("Number too small");
System.exit(0);
}
else if(n>100)
{
System.out.print("Number too large");
System.exit(0);
}
else
{
int a[]=new int[20];
while(n>0)
{
a[i]=n%2;
i++;
n=n/2;
}
for(int j=i-1;j>=0;j--)
System.out.print (a[j]);
}
}
}
93.reverseNumber
Read the question carefully and follow the input and output format.
Write a program to find the reverse of a given input integer
Input and Output Format :
Input consists of an integer, n. Output consist of the reverse of the number n.
Print "Number too large" when the given input number is greater than 32767
Print "Number too small" when the given input numbers is a negative number.
Sample Input 1:
1234
Sample Output 1:
4321
Sample Input 2:
326357
Sample Output 2:
Number too large
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, rev=0 ,rem;
//int flag=0;
Scanner in=new Scanner(System.in);
n = in.nextInt();
if(n<0)
{
System.out.println ("Number too small");
System.exit(0);
//flag=1;
}
if(n>32767)
{
System.out.println ("Number too large");
System.exit(0);
//flag=1;
}
//if(flag==0)
//{
while (n != 0)
{
rem =n%10;
rev = (rev *10)+rem;
n=n/10;
}
System.out.println (rev);
//}
}
}
94.Sum of Prime Cubes
Given an input integer n, write a program to find the sum of the cubes of the prime numbers
upto n. (including n)
Please note that 1 is neither prime nor composite.
The function returns -1 if the input is a negative number or if it is greater than 3000.
If the function returns -1, print Invalid Input.
Input and Output Format:
Input consists of a single integer.
Output consists of a single integer.
Refer sample output for formatting specifications.
Sample Input 1:
5
Sample Output 1:
160
Sample Input 2:
-241
Sample Output 2:
Invalid Input
Sample Input 3:
50000
Sample Output 3:
Invalid Input
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int i,n,j, k,sum=0,flag=0;
Scanner in=new Scanner(System.in);
n = in.nextInt();
if(n<0 || n>3000)
System.out.println ("Invalid input");
else
{
for(i=2;i<=n;i++)
{
int flag=0;
for(j=2;j<i;j++)
{
if(i%j==0)
flag=1;
}
if(flag==0)
{
sum=sum+(i*i*i);
}
}
System.out.println(sum);
}
}
}
95.Negative Elements
Write a program to remove all the negative elements of the input array , sort the positive
elements and stores them in an output array .
Include a function named eliminateNegatives that accepts 2 arguments and its return type is
void. The first argument is the input array and the second argument is an int that corresponds
to the size of the array. The output array is stored in a global variable named output1 and the
size of the output array is stored in a global variable named output2.
If the size of the array is negative , print “Invalid Input” and terminate the program.
Input and Output Format:
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in
the array. The next 'n' integers correspond to the elements in the array.
Output consists of an integer array.
Refer sample output for formatting specifications.
Assume that the maximum number of elements in the array is 20.
Sample Input 1:
8
1
6
3
5
-6
8
-15
9
Sample Output 1:
1
3
5
6
8
9
Sample Input 2:
-5
Sample Output 2:
Invalid Input
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, i,j,k=0, temp;
Scanner in=new Scanner(System.in);
n = in.nextInt();
if(n < 0)
{
System.out.print("Invalid array size");
System.exit(0);
}
else
{
int a[]=new int[n];
for(i = 0; i< n; i++)
a[i] = in.nextInt();
int output1[]=new int[n];
for(i=0;i<n;i++)
{
if(a [i]>0)
{
output1[k]=a [i];
k++;
}
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(output1[i]>output1[j])
{
temp = output1[i];
output1[i]=output1[j];
output1[j]=temp;
}
}
}
for(i=0;i<k;i++)
System.out.println(output1[i]);
}
}
}
96.Savings Calculation
Jim got salary for this month and he spends 50% of his salary for food and 20% of his salary for travel. If
the number of days he worked is 31 he gets a bonus of Rs.500. Write a program to find how much he can
save in his pocket after spending all these?
Include a function named calculateSavings that accepts 2 integer arguments and returns a float. The first
integer corresponds to Jim's basic salary and the second integer corresponds to the number of days Jim
has worked. The function returns a float that corresponds to the amount that Jim could save.
Print Invalid Input and terminate the program in the following cases:
1. Basic salary is greater than 9000
2. Number of working days is greater than 31
3. Basic salary is negative
4. Number of working days is negative
Input and Output Format:
Input consists of 2 integers. The first integer corresponds to Jim's basic salary and the second integer
corresponds to the number of days he has worked.
Output consists of a single float that corresponds to Jim's savings. Jim's savings is displayed correct to 2
decimal places.
Sample Input 1:
7000
30
Sample Output 1:
2100 .00
Sample Input 2:
50000
Sample Output 2:
Invalid Input
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int basic=0,days=0;
float spent,savings;
Scanner in=new Scanner(System.in);
basic=in.nextInt();
days=in.nextInt();
if(basic>9000 || days>31 || basic<0 || days<0)
{
System.out.println("Invalid input");
System.exit(0);
}
spent=(basic*70)/100;
if(days==31)
savings = basic+500-spent;
else
savings = basic-spent;
System.out.printf(“%.2f”,savings);
}
}
97.studentMarks
Read the question carefully and follow the input and output format.
The Given input is of the format XXXYY , where XXX is Id , YY is marks. Write a code to
display the id and marks separately as given in the output formats. [Refer sample input and
output]
Input and Output Format :
Input consists of a number. Refer sample output for output format.
Print "Number too large" when any of given input numbers is greater than 32767 .
Print "Number too small" when given input is a negative number.
Include a function named studentMarks(int number) whose return type is void.
Sample Input 1:
12345
Sample Output 1:
123
45
Sample Input 2:
-13
Sample Output 2:
Invalid input
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n=0,result;
Scanner in=new Scanner(System.in);
n = in.nextInt();
if(n<0)
System.out.println ("number too small");
else if(n>32767)
System.out.println ("number too large");
else
{
int id=0,mark=0,rem=0,i=1,flag=1,j=1;
while(n!=0)
{
if(flag<=2)
{
rem=n%10;
mark=mark+(rem*i);
i=i*10;
n=n/10;
flag++;
}
else
{
rem=n%10;
id=id+(rem*j);
j=j*10;
n=n/10;
flag++;
}
}
System.out.println(id);
System.out.println(mark);
}
}
}
98.maximumSum
Read the question carefully and follow the input and output format.
Given an Integer array, find out sum of Even and odd Numbers individually and find the
maximum.
Input and Output Format :
First line of input consists of n, the number of elements. Next n lines correspond to the array
elements. Output consist of maximum of odd and even sum.
1) Print "Invalid array size" when size of the array is a negative number and terminate the
program.
2) Print "Invalid input" when there is any negative numbers available in the input array and
terminate the program.
Sample Input 1:
5
12
13
14
15
16
Sample Output 1:
42
Sample Input 2:
-13
Sample Output 2:
Invalid array size
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, i, oddsum=0,evensum=0,max=0;
//int flag=0;
Scanner in=new Scanner(System.in);
n = in.nextInt();
if(n < 0)
{
System.out.print("Invalid array size");
System.exit(0);
}
else
{
int a[]=new int[n];
for(i = 0; i< n; i++)
{
a[i] = in.nextInt();
if(a[i] < 0)
{
//flag=1;
System.out.print("Invalid input");
System.exit(0);
}
}
//if(flag!=1)
//{
for(i=0;i<n;i++)
{
if(a[i]%2==0)
evensum = evensum + a[i];
else
oddsum = oddsum + a[i];
if(evensum>oddsum)
max = evensum;
else
max=oddsum;
}
System.out.print(max);
//}
}
}
}
99.newNumber
Read the question carefully and follow the input and output format.
Write a program to find the difference between consecutive digits in the given input integer and display it.
Input and Output Format:
Input consists of an integer and output the difference between the consecutive digits.
Print "Number too small" if the number is less than 0
Print "Number too large" if the number is greater than 32767
Sample Input 1:
1325
Sample Output 1:
213
Sample Input 2:
-13
Sample Output 2:
Number too small
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, rem,i=1,rem1=0,diff=0,digit=0;
Scanner in=new Scanner(System.in);
n = in.nextInt();
if(n<0)
{
System.out.println ("Number too small");
System.exit(0);
}
if(n>32767)
{
System.out.println ("Number too large");
System.exit(0);
}
else
{
while(n>10)
{
rem=n%10;
n=n/10;
rem1=n%10;
if(rem>rem1)
diff=rem-rem1;
else
diff=rem1-rem;
digit=digit+(diff*i);
i=i*10;
}
System.out.println(digit);
}
}
}
100.nonWorkingDoctors
Read the question carefully and follow the input and output format.
A doctor survey results information is stored in 2 arrays. First array represents all doctors ids
(working and non -working both). Second array represents only working doctor's id . Please
find the doctor ids who are not working .
Input and Output Format :
First line of input corresponds to n1, the size of first array and next n1 lines correspond to the
elements of the first array. The next line corresponds to n2, the size of second array and next
n2 lines correspond to the elements of the second array
Output is the id's of doctor who are not working
Print "Invalid array size" when size of the array is a negative number and terminate the
program
Print "Invalid id" when there is any negative numbers available in the input array and
terminate the program.
Include a function named nonWorkingDoctors(int total[],int working[],int n,int m) whose
return type is void.
Sample Input 1:
7
7
2
3
4
5
6
1
3
3
4
5
Sample Output 1:
7
2
6
1
Sample Input 2:
7
7
2
3
4
5
6
-1
Sample Output 2:
Invalid id
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int m,n, i,j;
//int flag=0;
Scanner in=new Scanner(System.in);
m=in.nextInt();
if(m < 0)
{
System.out.print("Invalid array size");
System.exit(0);
}
else
{
int a[]=new int[m];
for(i = 0; i< m; i++)
{
a[i] = in.nextInt();
if(a[i] < 0)
{
//flag=1;
System.out.print("Invalid input");
System.exit(0);
}
}
n=in.nextInt();
if(n<0)
{
System.out.print("Invalid array size");
System.exit(0);
}
int b[]=new int[n];
for(i = 0; i< n; i++)
{
b[i] = in.nextInt();
if(b[i] < 0)
{
//flag=1;
System.out.print("Invalid input");
System.exit(0);
}
}
//if(flag!=1)
//{
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(a[i]==b[j])
flag=1;
}
if(flag==0)
System.out.println(a[i]);
}
//}
}
}
}