class 9 loop programs
class 9 loop programs
(b) do - while do {
//loop-body
} while (condition);
Java Programs
Question 1
Write the programs in Java to display the first ten terms of the following series:
(a) 1, 4, 9, 16,
public class Series
{
public static void main( ) {
for (int i = 1; i <= 10; i++) {
System.out.print(i * i + " ");
}
}
}
(b) 1, 2, 4, 7, 11,
public class Series
{
public static void main( ) {
for (int i = 0; i < 10; i++) { int
term = 1 + ((i * (i + 1)) / 2);
System.out.print(term + " ");
}
}
}
(c) 3, 6, 9, 12,
public class Series
{
public static void main( ) {
for (int i = 3; i <= 30; i = i + 3) {
System.out.print(i + " ");
}
}
}
(f) 0, 7, 26,
public class Series
{
public static void main( ) {
for (int i = 1; i <= 10; i++) { int term
= (int)(Math.pow(i, 3) - 1);
System.out.print(term + " ");
}
}
}
(g) 1, 9, 25, 49,
public class Series
{
public static void main( ) {
for (int i = 1; i <= 19; i = i + 2) {
System.out.print((i * i) + " ");
}
}
}
(i) 0, 3, 8, 15,
public class Series
{
public static void main( ) {
for (int i = 1; i <= 10; i++) {
System.out.print((i * i - 1) + " ");
}
}
}
Question 2
Write a program to input any 50 numbers (including positive and negative).
Perform the following tasks:
(a) Count the positive numbers
(b) Count the negative numbers
(c) Sum of positive numbers (d) Sum of negative numbers import
java.util.Scanner; public class MIntegers
{
public static void main( ) { Scanner in = new
Scanner(System.in); int pSum = 0, pCount = 0,
nSum = 0, nCount = 0; System.out.println("Enter
50 numbers");
for (int i = 1; i <= 50; i++)
{ int n = in.nextInt(); if (n
>= 0) {
pSum += n; pCount++;
}
else {
nSum += n; nCount++;
}
}
System.out.println("Positive Count = " + pCount);
System.out.println("Positive Sum = " + pSum);
System.out.println("Negative Count = " + nCount);
System.out.println("Negative Sum = " + nSum);
}
}
Question 3
Write a program to calculate the sum of all odd numbers and even numbers
between a range of numbers from m to n (both inclusive) where m < n. Input m and
n (where m<n). import java.util.Scanner;
public class OddEvenSum
{
public static void main( ) {
Scanner in = new Scanner(System.in);
System.out.print("Enter m: "); int m =
in.nextInt();
System.out.print("Enter n: "); int n
= in.nextInt();
long sumOdd = 0, sumEven = 0;
if (m > n) {
System.out.println("m should be less than n");
}
else {
for (int i = m; i <=n; i++) {
if (i % 2 == 0)
sumEven += i;
else sumOdd
+= i;
}
System.out.println("Even Sum: " + sumEven);
System.out.println("Odd Sum: " + sumOdd);
}
}
}
Question 4
Write a program to enter any 50 numbers and check whether they are divisible by 5
or not. If divisible then perform the following tasks:
(a) Display all the numbers ending with the digit 5.
(b) Count those numbers ending with 0 (zero).
import java.util.Scanner; public
class DivisibleBy5
{
public static void main( ) { final int COUNT =
50;
Scanner in = new Scanner(System.in);
int n, c = 0;
System.out.println("Enter " + COUNT + " numbers");
for (int i = 1; i <= COUNT; i++) {
n = in.nextInt();
if (i % 5 == 0) {
if (i % 10 == 5)
System.out.println("Number end with the digit 5");
if (i % 10 == 0) c++;
}
}
System.out.println("Count of numbers ending with 0: " + c);
}
}
Question 6
Write a program to display all the 'Buzz Numbers' between p and q (where p<q). A
'Buzz Number' is the number which ends with 7 or is divisible by 7.
import java.util.Scanner; public
class BuzzNumber
{
public static void main( ) {
Scanner in = new Scanner(System.in);
System.out.print("Enter p: "); int p =
in.nextInt();
System.out.print("Enter q: ");
int q = in.nextInt(); if (p < q) {
System.out.println("Buzz Numbers between "
+ p + " and " + q); for (int i
= p; i <= q; i++) { if (i % 10
== 7 || i % 7 == 0)
System.out.println(i);
}
}
else {
System.out.println("Invalid Inputs!!!");
System.out.println("p should be less than q");
}
}
}
Question 8
Write a program in Java to find the sum of the given series :
(a) 1 + 4 + 9 + ...... + 400
public class Series
{
public static void main( ) { int sum = 0;
for (int i = 1; i <= 20; i++) sum
+= (i*i);
System.out.println("Sum = " + sum);
}
}
else sum += i
* 2;
}
System.out.println("Sum = " + sum);
}
}
Question
Write a program to input a number and count the number of digits. The program
further checks whether the number contains odd number of digits or even number
of digits.
Sample Input: 749
Sample Output: Number of digits=3 The
number contains odd number of digits.
import java.util.Scanner; public
class DigitCount
{
public static void main( ) {
Scanner in = new Scanner(System.in);
Question
The Greatest Common Divisor (GCD) of two integers is calculated by the
continued division method. Divide the larger number by the smaller, the
remainder then divides the previous divisor. The process repeats unless the
remainder reaches to zero. The last divisor results in GCD.
Sample Input: 45, 20
Sample Output: GCD=5
import java.util.Scanner; public
class GCD
{
public static void main( ) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first
number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
while (b != 0) { int
t = b;
b = a % b;
a = t;
}
System.out.println("GCD=" + a);
}
}
Question 12
Write a program in Java to find the sum of the given series :
(a) S = a + a / 2 + a / 3 + ...... + a / 10
2 2 2 2
(b) S = a + a / 2 + a / 3 + ...... + a / 10
2 3 10
Question
In order to reach the top of a pole, a monkey in his first attempt reaches to a height
of 5 feet and in the subsequent jumps, he slips down by 2% of the height attained
in the previous jump. The process repeats and finally the monkey reaches the top of
the pole. Write a program to input height of the pole. Calculate and display the
number of attempts the monkey makes to reach the top of the pole.
{
public static void main( ) {
Scanner in = new Scanner(System.in);
System.out.print("Enter height of the pole: ");
double poleHt = in.nextDouble(); double jumpHt
= 5.0; int numAttempts = 1; while (jumpHt <
poleHt) { jumpHt += 5.0; jumpHt -= 2 * jumpHt
/ 100.0;
numAttempts++;
}
System.out.println("Number of Attempts = " + numAttempts);
}
}
Question
Write a menu driven program to input two positive numbers m and n (where m>n)
and perform the following tasks:
(a) Find the sum of two numbers without using '+' operator.
(b) Find the product of two numbers without using '*' operator.
(c) Find the quotient and remainder of two numbers without using '/' and '%'
operator.
[Hint: The last value obtained after each subtraction is the remainder and the
number of iterations results in quotient.]
Sample Input: m=5, n=2
5 - 2 =3
3 - 2 = 1, thus Quotient = 2 and Remainder = 1
Question
Write a menu driven class to accept a number from the user and check whether it is
a Palindrome or a Perfect number.
(a) Palindrome number: (A number is a Palindrome which when read in reverse
order is same as in the right order)
Example: 11, 101, 151 etc.
(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
if (revNum == num)
System.out.println(num + " is palindrome"); else
System.out.println(num + " is not palindrome"); break;
case 2: int
sum = 0;
if (num == sum)
System.out.println(num + " is a perfect number"); else
System.out.println(num + " is not a perfect number"); break;
default:
System.out.println("Incorrect Choice"); break;
}
}
}
Question
Write a menu driven program to accept a number from the user and check whether
it is a Prime number or an Automorphic number.
(a) Prime number: (A number is said to be prime, if it is only divisible by 1 and
itself)
Example: 3,5,7,11
(b) Automorphic number: (Automorphic number is the number which is contained
in the last digit(s) of its square.)
Example: 25 is an Automorphic number as its square is 625 and 25 is present as the
last two digits.
case 2:
boolean isAutomorphic = true;
int numCopy = num; int sq =
num * num;
if (isAutomorphic)
System.out.println(numCopy + " is automorphic"); else
System.out.println(numCopy + " is not automorphic"); break;
default:
System.out.println("Incorrect Choice"); break;
}
}
}
Question
Write a menu driven program to perform the following tasks by using Switch case
statement:
(a) To print the series:
0, 3, 8, 15, 24, ............ to n terms. (value of 'n' is to be an input by the user)
(b) To find the sum of the series:
S = (1/2) + (3/4) + (5/6) + (7/8) + ........... + (19/20)
case 2:
double sum = 0;
for (int i = 1; i <= 19; i = i + 2)
sum += i / (double)(i + 1);
System.out.println("Sum = " + sum);
break; default:
System.out.println("Incorrect Choice"); break;
}
}
}
Question
Using a switch statement, write a menu driven program to:
(a) 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.
(b) Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should be displayed.
case 2:
System.out.print("Enter number: ");
int num = in.nextInt(); int sum = 0;
while (num != 0) {
sum += num % 10; num
/= 10;
}
System.out.println("Sum of Digits " + " = " + sum); break;
default:
System.out.println("Incorrect choice"); break;
}
}
}
Question
A special two-digit number is such that when the sum of its digits is added to the
product of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the
product of its digits. If the value is equal to the number input, then display the
message "Special 2 - digit number" otherwise, display the message "Not a special
two-digit number".
case 2:
System.out.print("Enter number: "); num =
in.nextInt();
int f = 1; for (int i = 1; i <=
num; i++)
f *= i;
System.out.println("Factorial = " + f);
break; default:
System.out.println("Incorrect Choice"); break;
}
}
}
Question
Write a program to input a number. Check and display whether it is a Niven
number or not. (A number is said to be Niven which is divisible by the sum of its
digits).
Example: Sample Input 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.
import java.util.Scanner; public
class NivenNumber
{
public static void main( ) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: "); int
num = in.nextInt();
int orgNum = num;
int digitSum = 0;
while (num != 0) { int
digit = num % 10;
num /= 10; digitSum
+= digit;
}
/*
* digitSum != 0 check prevents
* division by zero error for the
* case when users gives the number
* 0 as input
*/
if (digitSum != 0 && orgNum % digitSum == 0)
System.out.println(orgNum + " is a Niven number"); else
System.out.println(orgNum + " is not a Niven number");
}
}
Question
Write a program to accept a number and check 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: Sample Input: 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1*1*2*4 = 8