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

11.count of 3 Multiples

The document describes a program to perform arithmetic operations on two input numbers based on a choice variable. It includes a function that takes the two numbers and choice as arguments and returns the result. If the numbers are negative or greater than 32767, or if the choice is invalid, it returns -1 and prints an error message. Sample inputs and outputs are provided to demonstrate calculating the sum, difference, product and quotient based on the choice.

Uploaded by

priya piyu
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)
174 views16 pages

11.count of 3 Multiples

The document describes a program to perform arithmetic operations on two input numbers based on a choice variable. It includes a function that takes the two numbers and choice as arguments and returns the result. If the numbers are negative or greater than 32767, or if the choice is invalid, it returns -1 and prints an error message. Sample inputs and outputs are provided to demonstrate calculating the sum, difference, product and quotient based on the choice.

Uploaded by

priya piyu
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

11.

Count of 3 Multiples
 
Write a program to find the count of 3 multiples in a given input integer array.
 
Include a function named divisibleBy3 that accepts 2 arguments and returns an int. 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 an int that corresponds to the count of 3 multiples.
 
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 an integer that corresponds to the count of 3 multiples
 
Assume that the maximum number of elements in the array is 20.
 
Sample Input 1:
8
1
6
3
5
61
80
102
9
 
Sample Output 1:
4
 
Sample Input 2:
-5
 
Sample Output 2:
Invalid Input
 
Sample Input 3:
5
23
2
-200
 
Sample Output 3:
Invalid Input
 import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                        int n, i,count=0,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]%3==0 && a[i]!=0)
                                                            count++;
                                                }
                                                System.out.print(count);
                                    }
                        }
            }
}

12.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,sumo=0,sume=0;
                        double sum,avg;
                        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)
                                                {
                                                           
                                                            System.out.print("Invalid input");
                                                            System.exit(0);
                                                }
                                    }
                                    for(i=0;i<n;i++)
{
                                               if(a[i]%2==0)
                                                 sume=sume+a[i];
                                               else
                                                   sumo=sumo+a[i];
                                      }
                                      sum=sume+sumo;
                                      avg = sum/2;
                                     System.out.printf("%.2f",avg);
                                   
                        }
            }
}
13.Decimal Conversion
 
Write a program to convert a given input binary number to decimal.
 
Include a function named convertToDecimal that accepts an integer argument and returns an integer that
corresponds to the decimal representation of the input number. If the input value is not a binary value or if
the input is negative or if the input is greater than 11111, the function returns -1.
 
If the function returns -1, print Invalid Input.
 
Input and Output Format:
Input consists of a single integer that corresponds to the binary representation of a number.
Output consists of a single integer that corresponds to the decimal equivalent of the given number.
Refer sample output for formatting specifications.
 
 
Sample Input 1:
1100
 
Sample Output 1:
12
 
Sample Input 2:
101010
 
Sample Output 2:
Invalid Input
 
Sample Input 3:
1201
 
Sample Output 3:
Invalid Input
 
import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                        int num=0,binary,rem,power;
                        double a,decimal=0;
                        Scanner sc=new Scanner(System.in);
                        num=sc.nextInt();
                        if(num<0 || num>11111)
                        System.out.print("Invalid Input") ;
                        else
                        {
                                    binary = num;
                                    power = 0;
                                    while (binary != 0)
                                    {         
                                                rem = binary % 10;
                                                a= Math.pow(2, power);
                                                decimal = decimal+ rem * a;
                                                power++;
                                                binary = binary / 10;
                                    }
                                    System.out.printf("%.0f",decimal);
                        }
            }
}

 
14.Arithmetic Operation
 
Write a program to perform a specific arithmetic operation
 
Include a function named performArithmeticOperation that accepts 3 integer arguments and returns an
integer that corresponds to the result. The first and second arguments correspond to the input numbers and
the third argument corresponds to the choice of arithmetic operation.
 
If argument 3 =1, calculate the sum of input1 and input2
If argument 3 =2, calculate the difference of input1 and input2
If argument 3 =3, calculate the product of input1 and input2
If argument 3 =4, calculate the quotient of input1 divided by input 2
 
If the first two argument's values is negative or greater than 32767 , the function returns -1.
If the third argument's value is not in the range 1 to 4, the function returns -1.
 
If the function returns -1, print Invalid Input.
 
Input and Output Format:
Input consists of 3 integers.
Output consists of an integer.
Refer sample output for formatting specifications.
 
 
Sample Input 1:
4
12
3
 
Sample Output 1:
48
 
Sample Input 2:
-67
2
1
 
Sample Output 2:
Invalid Input
 
import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                     int a,b,choice,result=0;
                        Scanner in=new Scanner(System.in);
                        a = in.nextInt();
b = in.nextInt();
choice = in.nextInt();
                      if(a<0 || b<0 || a>32767 || b>32767)
                        System.out.print("Invalid input");
                        else if((choice<1)||(choice>4))
                        System.out.print("Invalid input");
                        else
                        {
                                    switch(choice)
{
                                                case 1:
                                                            result = a+b;
                                                            break;
                                                case 2:
                                                            result = a-b;
                                                            break;
                                                case 3:
                                                            result = a*b;
                                                            break;
                                                case 4:
                                                            result = a/b;
                                                            break;
                                    }
                                    System.out.print(result);
                        }
            }         
}

15.digitFactorial

Read the question carefully and follow the input and output format.

In a given input number , find out the factorial of each individual digit and assign it to output array.

Input and Output Format:


Input consists of a single integer. Output consists of an Integer array, the individual factorials.

Print "Number too large" when the given input numbers is greater than 32767 .
Print "Number too small" when the given input is a negative number.

Include a function named digitFactorial(int number) whose return type is void.


The output array is stored in a global variable named factorial.

Sample Input 1:
123

Sample Output 1:
1
2
6
Sample Input 2:
-2526

Sample Output 2:
Number too small

import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                        int n,i,fact,k=0,rem;
                        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);

                        }
                        int factorial[]=new int[100];
while(n!=0)
{
                                  rem=n%10;
                                 fact=1;
                                 for(i=1;i<=rem;i++)
                                  {
                                          fact = fact * i;
                                  }
                                  factorial[k]=fact;
                                   k++;
                                   n=n/10;
                        }
                        for(i=k-1;i>=0;i--)
                        System.out.println(factorial[i]);
           }
}

16.searchKeys

Read the question carefully and follow the input and output format.

Given an integer array, first index represents the key & second index represents the value. Find keys for
the given value.
Input and Output Format:
First line of input consists of n, the number of elements. Next n lines correspond to the array elements. The
next line consistts of an integer that represents the value to be searched. 
Output consist of an integer array.

1) Print "Invalid array size" when size of the array is negative and terminate the program.
2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the
program.
3) Print "Key not found" when there is no keys found.

Include a function named searchKeys(int array[], int size) whose return type is void.
The output array is stored in a global variable named found.

Sample Input 1:
8
1
4
2
4
3
4
5
6
4

Sample Output 1:
1
2
3

Sample Input 2:
5
5
6
7
8
9
-5

Sample Output 2:
Key not found

import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                        int n, i,k=0,se,flag=0,f=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)
                                                {
                                                            System.out.print("Invalid input");
                                                            System.exit(0);
                                                }
                                    }
                                    se=in.nextInt();
                                    if(se<0)
{
                                    System.out.print("Key not found");
System.exit(0);
                                   }
                                    int found[]=new int[100];
for(i=0;i<n;i++)
                                    {
                                      if(a [i]==se)
{
                                                    f=1;
                                                            found[k]=a [i-1];
                                                            k++;
                                                  }
                                     }
                                      if(f==0)
                                      System.out.print("Key not found");
                                       else
                                      {
                                                  for(i=0;i<k;i++)
                                                  System.out.println(found[i]);
                                       }
                        }
           }
}

17.passCount

Read the question carefully and follow the input and output format.

Given a input array, First index Represents RollNo second index represents Mark and so on.  Write a
program to find the number of students who had cleared the exam.

Note : If marks >=70 then He /she Cleared the exam. Array size is always even.

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 an integer,

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 number available in the input array and terminate the
program.

Include a function named passCount(int array[], int size) whose return type is an integer , the count.

Sample Input 1:
8
1
70
2
55
3
75
4
80

Sample Output 1:
3

Sample Input 2:
5
6
2
8
-2
Sample Output 2:
Invalid input

import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                        int n, i,count=0;
                        Scanner in=new Scanner(System.in);
                        n = in.nextInt();
                        if(n < 0 || n%2!=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)
                                                {
                                                          
                                                            System.out.print("Invalid input");
                                                            System.exit(0);
                                                }
                                    }
                                for(i=1;i<n;i=i+2)
{
                                                if(a[i]>=70)
                                                count++;
                                   }
                                     System.out.print(count);
}
          }
}

 18.5 Multiples --- Average

 
Write a program to find the average of multiples of 5 upto 'n'. n is given as input.
 
Include a function named findAverageBy5s that accepts an integer argument and returns a float that
corresponds to the average of multiples of 5.
 
If the input value is negative or greater than 32767, print Invalid Input and terminate the program.
 
Input and Output Format:
Input consists of a single integer.
Output consists of a floating point number. Output is displayed correct to 2 decimal places.
Refer sample output for formatting specifications.
 
 
Sample Input 1:
10
 
Sample Output 1:
7.50
 
Sample Input 2:
-67
 
Sample Output 2:
Invalid Input
 
import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                        int n,i,count=0;
                        double sum=0,avg;
                        Scanner in=new Scanner(System.in);
                        n = in.nextInt();
                        if(n<0 || n>32767)
                        {
                                    System.out.println ("Number too small");
                                    System.exit(0);
                        }
                       for(i=5;i<=n;i++)
{
                                    if(i%5==0)
{
                                                sum=sum+i;
                                                count++;
                                    }
                        }
                        avg = sum/count;
                        System.out.printf("%.2f",avg);
            }
}
 
19.Sum of Odd Digits
 
Write a program to find the sum of the odd digits in a number.
 
Include a function named sumOddDigits that accepts an integer argument and returns an integer that
corresponds to the sum of the odd digits. The function returns -1 if the input is less than zero or if it is
greater than 32767.
 
If the function returns -1, print “Invalid Input”.
 
Input and Output Format:
 
The input consists of an integer.
The output consists of an integer that corresponds to the sum of the odd digits in the number.
 
Sample Input 1:
3487
 
Sample Ouput 1:
10
 
Sample Input 2:
-8
 
Sample Output 2:
Invalid Input
 
import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                        int n,i,rem,sum=0;
                        Scanner in=new Scanner(System.in);
                        n = in.nextInt();
                        if(n<0 || n>32767)
                        {
                                    System.out.println ("Number too small");
                                    System.exit(0);
                        }
                      else
{
                                    while(n!=0)
{
                                                rem=n%10;
                                                if(rem%2!=0)
                                                sum=sum+rem;
                                                n=n/10;
                                    }
                                    System.out.println (sum);
                        }
            }
}

20.Largest Array

 
Write a program which takes two arrays of the same size as a input and compares the first element of first
array with the first element of second array and stores the largest of these into the first element of the
output array. Repeat the process till the last element of the first array is checked with the last element of
the second array.
 
Include a function named largestArray that accepts 3 arguments and its return type is void. The first
argument is input array 1, the second argument is  input array 2 and the third argument is an int that
corresponds to the size of the array. The output array is stored in a global variable named output1.
 
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 the largest array.
Assume that the maximum number of elements in the array is 20.
 
Sample Input 1:
4
2
1
3
4
1
9
2
8
 
Sample Output 1:
2
9
3
8
 
 
Sample Input 2:
-5
 
Sample Output 2:
Invalid Input
 
Sample Input 3:
5
23
2
-200
 
Sample Output 3:
Invalid Input
 import java.util.Scanner;
public class Main
{
            public static void main(String[] args)
            {
                        int n,i,j,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)
                                                {
                                                            System.out.print("Invalid input");
                                                            System.exit(0);
                                                }
                                    }
                                    int b[]=new int[n];
                                   for(i = 0; i< n; i++)
                                    {
                                                b[i] = in.nextInt();
                                                if(b[i] < 0)
                                                {
                                                            System.out.print("Invalid input");
                                                            System.exit(0);
                                                }
                                    }
                                     int output1[]=new int[100];   
                                     for(i=0;i<n;i++)
                                     {
                                              if(a[i]>b[i])
{
                                                            output1[i]=a [i];
                                                    }
                                                    else
{
                                                               output1[i]=b[i];
                                                     }         
                                      }
                                      for(i=0;i<n;i++)
{
                                                   System.out.println(output1[i]);
                                      }
                        }
            }
}

You might also like