Iterative Construct

Section B

2005

Question 8
Write a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd numbers from a list of numbers (N) entered by the User. The list terminates when the User enters a zero [15]

import java.util.*;
public class Sum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int num,s1=0,s2=0,s3=0;
System.out.println("Enter number of numbers");
int N=sc.nextInt();
for(int i=1;i<=N;i++)
{
System.out.println("Enter number (to stop, input 0 (zero)): ");
num=sc.nextInt();
if(num==0)
break;
else if(num<0)
s1+=num;
else if(num>0&&num%2==0)
s2+=num;
else if(num>0&&num%2==1)
s3+=num;
}
System.out.println("Sum of negative numbers = "+s1);
System.out.println("Sum of positive even numbers= "+s2);
System.out.println("Sum of positive odd numbers = "+s3);
}
}

Variable Description

VariableTypeDescription
numintFor number
nintFor number of numbers
s1, s2, s3intFor sum of negative, positive even, and positive odd
iintLoop variable

2006

Question 8
Write a program to calculate and print the sum of odd numbers and the sum of even numbers for the first n natural numbers. The integer n is to be entered by the user. [15]

import java.util.*;
public class Sum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Input the number of numbers: ");
int n = sc.nextInt();
int s1=0,s2=0;
for(int i=1;i<=n;i++)
{
if(i%2!=0)
s1+=i; //To find the sum of odd numbers
else
s2+=i; //To find the sum of even numbers
}
System.out.println("Sum of odd numbers : "+s1);
System.out.println("Sum of even numbers: "+s2);
}
}

2008

Question 7
Write a menu driven class to accept a number from the user and check whether it is Palindrome or Perfect.
(a) Palindrome number - (a number is a Palindrome when read in reverse order is same as read in the right order)
Example: 11,101,151.
(b) Perfect number - (a number is called Perfect if it is equal to the sum of its factors other than the number itself.)
Example: 6=1+2+3 [15]

import java.util.*;
public class Number
{
public static void main()
{
static Scanner sc=new Scanner(System.in);
System.out.println("\t\t\t\tMenu");
System.out.println("1 Palindrome number");
System.out.println("2 Perfect number");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter a number");
int n=sc.nextInt();
int i=n,d,rev=0;
while(i>0)
{
d = i%10;
rev=rev*10+d;
i=i/10;
}
if(rev==n)
System.out.println("Palindrome number");
else
System.out.println("Not a Palindrome number");
break;
case 2: System.out.println("Enter a number");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n/2;i++)
if(n%i==0)
s=s+i;
if(s==n)
System.out.println("Perfect number");
else
System.out.println("Not a Perfect number");
break;
default:System.out.println("Wrong choice. Try again.");
}
}
}

Question 9
Write a program to calculate and print the sum of each of the following series: [15]
(a) Sum (S) = 2 – 4 + 6 – 8 + … - 20
(b) Sum (S) =x/2 + x/5 + x/8+ x/11 + ….+ x/20
(Value of x to be input by the user.)

import java.util.*;
public class Sum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,sign=1,s=0;
for(i=2;i<=20;i+=2)
{
s+=i*sign;
sign=-sign;
}
System.out.println("Sum of first series: "+s);
System.out.println("Enter value for x");
double x=sc.nextDouble();
double s=0.0;
for(i=2;i<=20;i+=3)
s=s+x/i;
System.out.println("Sum of second series: "+s);
}
}

2009

Question 5
Write a program to generate a triangle or an inverted triangle till n terms based upon the user’s choice:[15]
Example 1
Input:
Type 1 for a triangle and
type 2 for an inverted triangle
1
Enter the number of terms : 5
Output :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Example 2
Input:
Type 1 for a triangle and
type 2 for an inverted triangle
2
Enter the number of terms: 6
Output :
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

import java.util.*;
public class Triangle
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Type 1 for a triangle and");
System.out.println("type 2 for an inverted triangle");
int ch=sc.nextInt();
switch(ch)
{
case 1 : System.out.print( “Enter the number of terms”);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
System.out.print(i+" ");
System.out.println();
}
break;
case 2 : System.out.print( “Enter the number of terms”);
int n=sc.nextInt();
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
System.out.print(i+" ");
System.out.println();
}
break;
default : System.out.println("Wrong choice");
}
}
}

Question 8
Write a menu driven program to accept a number from the user and check whether it is a ‘BUZZ’ number or to accept any two numbers and print the ‘GCD’ of them.
(a) A BUZZ number is number which either ends with 7 or divisible by 7.
(b) GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the larger number by the smaller; the remainder then divides the previous divisor. This process is repeated till the remainder is zero. The divisor then results the GCD. [15]

import java.util.*;
public class Numbers
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("1 BUZZ Number check");
System.out.println("2 GCD find");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter the number");
int n=sc.nextInt();
if(n%10==7||n%7==0)
System.out.println("BUZZ Number");
else
System.out.println("Not a BUZZ Number");
break;
case 2: System.out.println("Enter two numbers");
int a=sc.nextInt();
int b=sc.nextInt();
if(a>b)
{
dividend=a;
divisor=b;
}
else
{
dividend=b;
divisor=a;
}
remainder=dividend%divisor;
while(remainder>0)
{
dividend=divisor;
divisor=remainder;
remainder=dividend%divisor;
}
System.out.println("GCD="+divisor);
break;
default:System.out.println("Wrong choice");
}
}
}

2010

Question 6
Shasha Travels Pvt. Ltd. gives the following discount to its customers:
Ticket amount Discount
Above Rs. 70000 18%
Rs. 55001 to Rs. 70000 16%
Rs. 35001 to Rs. 55000 12
Less than Rs. 25001 2
Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer.
(Imagine there are 15 customers, first customer is given the serial number 1, next customer 2. so on) [15]
Sl.No.NameTicket chargesDiscountNet amount
1--------

import java.util.*;
class Travels
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of customers: ");
int n=sc.nextInt();
double dis,net;
for(int i=1;i<=n;i++)
{
System.out.println("Enter name and ticket amount: ");
String name=sc.next();
double amt=sc.nextDouble();
if(amt>70000)
dis=amt*18/100.0;
else if(amt>55000)
dis=amt*16/100.0;
else if(amt>35000)
dis=amt*12/100.0;
else if(amt>25000)
dis=amt*10/100.0;
else
dis=amt*2/100.0;
net=amt-dis;
System.out.println("Sl.No.\tName\tTicket charges\tDiscount\tNet amount");
System.out.println(i+"\t"+name+"\t"+amt+"\t"+dis+"\t"+net);
}
}
}

Question 7
Write a menu driven program to accept a number and check and display whether it is prime number or not OR an automorphic number or not. (Use switch case statement.)
(a) Prime number: A number is said to be prime number if it is divisible only by 1 and itself and not by any other number. E.g.: 3,5,7,11,13, etc.
(b) Automorphic number: An automorphic number is the number which is contained in the last digit(s)of its square. E.g.: 25 is an automorphic number as its square is 625 and 25 is present as last two digits. [15]

import java.util.*;
public class MenuDriven
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("1 Prime");
System.out.println("2 Automorphic");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter number");
int n=sc.nextInt();
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
System.out.println("Prime");
else
System.out.println("Not Prime");
break;
case 2: System.out.println("Enter number");
int n=sc.nextInt();
int i=n, p=1;
while(i>0)
{
i/=10;
p*=10;
}
if(n== n*n%p)
System.out.println("Automorphic");
else
System.out.println("Not Automorphic");
break;
default: System.out.println("Wrong choice");
}
}
}

2011

Question 6
Write a program to input a number and print whether it is a special number or not. (A number is said to be a special number if the sum of the factorial of the digits of the number same as the number). Example: 145.
1! + 4! + 5! = 1 + 24 +120 = 145. (Where ! stands for factorial and factorial of a number is product of integers from 1 to that number. Example 5! = 1*2*3*4*5=120) [[15]]

import java.util.*;
public class Numbers
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i=n,j,d,f,s=0;
while(i>0)
{
d=i%10;
f=1;
for(j=1;j<=d;j++)
f=f*j;
s=s+f;
i=i/10;
}
if(s==n)
System.out.println( “Special number”);
else
System.out.println( “Not a Special number”);
}
}

Question 9
Write a menu driven program to perform the following: Use switch case.
(a) To print series 0, 3, 8, 15, 24 … n terms (value of n is to be input by user).
(b) To find the sum of the series: S=1/2 + 3/4 + 5/6 + … + 19/20 [15]

import java.util.*;
public class Series
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“\t\t\t\tMenu”);
System.out.println(“1 Print series”);
System.out.println(“2 Find sum”);
System.out.println( “Enter your choice”);
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println( “Enter number of terms”);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
System.out.print((i*i-1)+“, ”);;
break;
case 2: double s=0,a=2;
for(int i=1;i<=19;i+=2)
{
s=s+i/a;
a+=2;
}
System.out.println(“Sum=”+s)
break;
default: System.out.println( “Wrong choice.”);
}
}
}

2012

Question 8
Using the switch statement write a menu driven program to:
(i) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5 …
The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
(ii) Find the sum of the digits of an integer that is input.
Sample input: 15390 Sample output: Sum of digits=18
For an incorrect choice an appropriate error message should be displayed. [15]

import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("1 fibonacci series");
System.out.println("2 sum of digits");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: int a=0,b=1,c,i;
for(i=1;i<=10;i++)
{
System.out.print(a “, “);
c=a+b;
a=b;
b=c;
}
System.out.println();
break;
case 2: System.out.println("Enter number");
int n=sc.nextInt();
int i=n,s=0,d;
while(i>0)
{
d=i%10;
s+=d;
i/=10;
}
System.out.println("Sum of digits="+s);
break;
default:System.out.println("Wrong choice. Try again");
}
}
}

2013

Question 5
The International Standard Book Number (ISBN) is a unique numeric book identifier which is printed on every book. The ISBN is based upon 10 digit code.
The ISBN is legal if: 1*digit1 + 2*digit2 + 3*digit3 + 4*digit4 + 5*digit5 + 6*digit6 + 7*digit7 + 8*digit8 + 9*digit9 + 10*digit10 is divisible by 11.
Example: for an ISBN 1401601499
Sum = 1*1 + 2*4 + 3*0 + 4*1 + 5*6 + 6*0 + 7*1 + 8*4 + 9*9 + 10*9 = 253 which is divisible by 11.
Write a program to: (i) Input the ISBN code as a 10 digit integer.
(ii) If the ISBN is not a 10 digit number, output the message, “Illegal ISBN” and terminate the program.
(iii) If the number is 10 digit, extract the digits of the number and compute the sum as explained above
If the sum is divisible by 11, output a message, “Legal ISBN”. If the sum is not divisible by 11, output the, “Illegal ISBN”. [15]

import java.util.*;
public class Numbers
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number ");
long n= sc.nextLong();
if(!(n>=1000000000&&n<=9999999999))
System.out.println(“Illegal ISBN”);
else
{
long a=n,d,s=0;
for(int i=10;i>=1;i--)
{
d=a%10;
s+=i*d;
a/=10;
}
if(s%11==0)
System.out.println("Legal ISBN");
else
System.out.println("Illegal ISBN ");
}
}
}

Question 9
Using the switch statement, write a menu driven program:
(i) To check and display whether a number input by the user is a composite number or not. (A number is said to be a composite, if it has one or more than one factor excluding 1 and the number itself.
Example : 4,6,8,9…).
(ii) To find the smallest digit of an integer that is input.
Sample input: 6524
Sample output: smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed. [15]

import java.util.*;
public class Number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“1 Check composite”);
System.out.println(“2 Find smallest digit”);
System.out.println( “Enter your choice”);
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println(“Enter number”);
int n=sc.nextInt();
int c=0;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
c++;
}
if(c>=1)
System.out.println("Composite number");
else
System.out.println("Not a composite number ");
break;
case 2: System.out.println(“Enter number”);
int n=sc.nextInt();
int i=n,d,small=9;
while(i>0)
{
d=i%10;
if(d < small)
small=d;
i/=10;
}
System.out.println(“Smallest digit = ”+small);
break;
default: System.out.println( “Wrong choice. Try again”);
}
}
}

2015

Question 5
Write two separate programs to generate the following patterns using iteration (loop) statements:
a)
*
* #
* # *
* # * #
* # * # *
b)
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5 [[15]]

a)
public class Pattern 1
{
public static void main()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
if(j%2==1)
System.out.print("*");
else
System.out.print("#");
}
System.out.println();
}
}
}
b)
public class Pattern2
{
public static void main()
{
for(int i=1;i<=5;i++)
{
for(int j=5;j>=i;j--)
System.out.print(j+" ");
System.out.println();
}
}
}

Question 9
Using a switch statement write a menu driven program to:
(i) To find and display all the factors of a number input by the user (including 1 and excluding the number itself. Sample input: n=15
Sample output: 1, 3, 5
(ii) To find and display the factorial of a number input by the user (the factorial of a non-negative integer n, denoted by n!, is the product of all integer less than or equal to n.
Sample input: n=5
Sample output: 5! = 1x2x3x4x5 = 120
For an incorrect choice an appropriate error message should be displayed. [15]

import java.util.*;
public class MenuDriven
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Menu");
System.out.println("1 Factors");
System.out.println("2 Factorial");
System.out.println("Enter your choice");
int ch= sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter number");
int n= sc.nextInt();
System.out.println("Factors:");
for(int i=1;i<=n/2;i++)
{
if(n%i==0)
System.out.print(i+", ");
}
break;
case 2: System.out.println("Enter number");
int n= sc.nextInt();
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
System.out.println("Factorial: "+f);
break;
default: System.out.println("Wrong entry. Try again");
}
}
}

2016

Question 5
Using the switch statement, write a menu driven program for the following:
(i) To print the Floyd’s triangle given below
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(ii) To display the following pattern:
I
I C
I C S
I C S E
For an incorrect option, an appropriate error message should be displayed. [15]

import java.util.*;
public class Triangles
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Menu”);
System.out.println(“1 FloydTriangle “);
System.out.println(“2 CharacterTriangle “);
System.out.println(“Enter your choice”);
int ch= sc.nextInt();
switch(ch)
{
case 1: int k=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
System.out.print(k++ +" ");
System.out.println();
}
break;
case 2: String s=“ICSE”;
for(int i=0;i < s.length();i++)
{
for(int j=0;j <= i;j++)
System.out.print(s.charAt(j)+” “);
System.out.println();
}
break;
default: System.out.println(“Wrong choice.”);
}
}
}

2017

Question 5
Write a program to accept a number and check and display whether it is a spy number or not.
(A number is spy if the sum of its digits equals the product of its digits.)
Example: Consider the number 1124. Sum of digits:= 1+1+2+4=8. Product of digits = 1*1*2*4 = 8 [[15]]

import java.util.*;
public class Numbers
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number ");
int n=sc.nextInt();
int i=n,d,s=0,p=1;
while(i>0)
{
d=i%10;
s+=d;
p*=d;
i/=10;
}
if(s==p)
System.out.println("Spy number");
else
System.out.println("Not a spy number ");
}
}

Question 6
Using switch statement, write a menu driven program for the following:
(i) To find and display the sum of the series given below:
S=x1 - x2 + x3 - x4 + x5 ……. - x20 (where x=2)
(ii) To display the following series:
1 11 111 1111 11111
For an incorrect option an appropriate error message should be displayed [15]

import java.util.*;
public class MenuDriven
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("\t\t\t\tMenu");
System.out.println("1 Sum of series");
System.out.println("2 Print series");
System.out.println("enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: int x=2,s=0,sign=1;
for(int i=1;i<=20;i++)
{
s+=Math.pow(x,i);*sign;
sign=-sign;
}
System.out.println("Sum: "+s);
break;
case 2: for(int i=1;i<=5;i=i*10+1)
System.out.println(i+" ");
break;
default: System.out.println("Wrong choice.");
}
}
}

2018

Question 5
Write a program to input a number and check and print whether it is a Pronic number or not. (Pronic number is the number which is the product of two consecutive integers)
Examples: 12=3x4, 20=4x5, 41=6x7 [[15]]

import java.util.*;
public class Pronic
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
boolean flag=true;
for(int i=1;i<=n/2;i++)
{
if(i*(i+1)==n)
{
flag=true;
break;
}
}
if(flag)
System.out.println(n+" is a Pronic number");
else
System.out.println(n+" is a not a Pronic number");
}
}

2019

Question 5
Using switch statement write a menu driven program to do the following:
(a) To generate and print Letters from A to Z and their Unicode
LettersUnicode
A 65
B 66
.. ..
Z 90
(b) Display the following pattern using iteration statement:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 [15]

import java.util.*;
public class MenuDriven
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("\t\t\t\tMenu");
System.out.println("1 Unicode");
System.out.println("2 Pattern");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Letters\tUnicode");
for(char i='A';i<='Z';i++)
System.out.println(i+"\t"+(int)i);
break;
case 2: for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j+"\t");
System.out.println();
}
break;
default: System.out.println("Wrong choice.");
}
}
}

Question 9
A tech number has even number of digits. If the number is split in two equal halves, then square of sum of these halves is equal to the number itself. Write a program to generate and print all four digits tech numbers.
Example: Consider the number 3025
Square of sum of the halves of 3025 = (30+25)2 = (55)2 = 3025 is a tech number. [[15]]

public class Numbers
{
public static void main()
{
for(int i=1000;i<=9999;i++)
{
int h1=i/100;
int h2=i%100;
int s=h1+h2;
if(s*s==n)
System.out.println(i);
}
}
}

2020

Question 8
Write a menu driven program to perform the following operations as per user’s choice: [15]
(i) To print the value of c=a2+2ab, where a varies from 1.0 to 20.0 with increment of 2.0 and b=3.0 is a constant.
(ii) To display the following pattern using for loop:
A
AB
ABC
ABCD
ABCDE
Display proper message for an invalid choice.

import java.util.*;
public class MenuDriven
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("\t\t\t\tMenu");
System.out.println("1 Value of c=a2+2ab");
System.out.println("2 Alphabet Pattern");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter values for a and b");
double a=sc.nextDouble();
double b=sc.nextDouble();
double c=a*a+2*a*b;
System.out.println("Value of c = "+c);
break;
case 2: for(int i=1;i<=5;i++)
{
char ch='A';
for(int j=1;j<=i;j++)
System.out.print(ch++);
System.out.println();
}
break;
default: System.out.println("Wrong choice.");
}
}
}