0% found this document useful (0 votes)
24 views2 pages

Java Programs for Iterations and Numbers

The document discusses Java programs involving iterations, series, number checking programs like checking if a number is prime, Armstrong, palindrome etc. It provides 14 questions/problems with sample inputs and outputs to write Java programs to solve.

Uploaded by

sarthak14jan09
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)
24 views2 pages

Java Programs for Iterations and Numbers

The document discusses Java programs involving iterations, series, number checking programs like checking if a number is prime, Armstrong, palindrome etc. It provides 14 questions/problems with sample inputs and outputs to write Java programs to solve.

Uploaded by

sarthak14jan09
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

Dr.

Virendra Swarup Public School, Cantt


Computer Applications
Iterations in Java
Write the Output:
Q1 while(++i<6)
x = 5; y = 50; k*=i;
while(x<=y) [Link](k);
{ Q4
y = y/x; int a,b;
[Link](y); for(a=6, b=4; a<=4; a=a+6 )
} {
Q2 if(a%b==0)
int m = 2; break;
int a = 15; [Link](a);
for(int i=1; i<5; i++) }
m++; Q5
n--; int i;
[Link](“m=”+m); for(i=5; i>10; i++)
[Link](“n=”+n); [Link](i);
Q3 [Link](i*4);
Int k=1, i=2;
Programs:
1. Write the programs in programs in Java to display the first ten terms of the following series:
(a) 0, 1, 2, 3, 6,
(b) 1, -3, 5, -7, 9,
(c) 0, 3, 8, 15,
(d) 1, 11, 111, 1111,
(e) 1, 12, 123, 1234,
2. Write the programs in Java to find the sum of the following series:
(a) S = 1 + 1 + 2 + 3 + 5 +………… to n terms
(b) S = 2 – 4 + 6 – 8 +………… to n
(c) S = 1 + (1 + 2) + (1 + 2 + 3) + ………… + (1+2+3+………+n)
(d) S = 1 + (1 * 2) + (1 * 2 * 3) + ………… + (1*2*3*………*n)
3. Write the programs to find the sum of the following series:
(a) S = a + a2 + a3 + …………… + an
(b) S = (a+1) + (a+2) + (a+3) + …………… + (a+n)
(c) S = (a/2) + (a/5) + (a/8) + (a/11) + …………… + (a/20)
(d) S = a - a3 + a5 - a7 + …………… to n
4. Write a programs to input a number and find the number is Armstrong or not.
Sample Input: 153
Sample Output: 153 = 13 + 53 + 33
It is an Armstrong Number
5. Write a program to accept a number from the user and check whether it is a Palindrome number or not.
A number is a Palindrome which when reads in reverse order is same as in the right order.
Sample Input: 242
Sample Output: A Palindrome number
Sample Input: 467
Sample Output: A not a Palindrome number
6. Write a program in Java to accept a number. Check and print whether it is a prime number or not. A
prime number is a number which is divisible by 1 and itself only. For example: 2, 3, 5, 7, 11,13 are all prime
numbers.
7. 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.
8. Write a program to enter two numbers and check whether they are co-prime or not.
[Two numbers are said to be co-prime, if their HCF is 1 (one).]
Sample Input: 14, 15
Sample Output: They are co-prime.
9. Write a program to input a number. Display the product of the successors of even digits of the number
entered by user.
Sample Input: 2745
Sample Output: 15
[Hint: The even digits are: 2 and 4
The product of successors of even digits is: 3*5= 15]
10. Write a program to input a number and check and print whether it is Pronic number or not
[Pronic number is the number which is the product of two consecutive integers.]
Examples:
12 = 3*4
20 = 4*5
42 = 6*7
11. Write a program to input a number and check whether it is a prime number or not. If it is not a prime
number then display the next number that is prime.
Sample Input: 14
Sample Output: 17
12. 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.
13. 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 two-digit number” otherwise, display
the message “Not a special two-digit number”.
14. Write a program to input a number. 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: Sample Input: 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 * 1 * 2 * 4 = 8
16. 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 number 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 by the user.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should be displayed.s

Common questions

Powered by AI

A Java program identifies an automorphic number by squaring the number and checking if the number itself is present as the last digit(s) of its square. This involves basic arithmetic operations and string manipulations. For instance, a number like 25 is automorphic as 625 ends with 25 .

Design a Java program that iterates through each digit of the number, identifies even digits, and calculates the sum or product of their successors by adding one to each. This involves using loops and conditionals to extract and process individual digits and is illustrated by example 2745, where 2 and 4 yield successors 3 and 5 with a product of 15 .

To implement a Java program displaying the Fibonacci sequence up to the first 10 terms in a menu-driven format, use loops to sum the preceding two terms starting at 0 and 1. Utilize a switch statement for menu management, ensuring proper input prompts and sequence generation like 0, 1, 1, 2, 3, 5,... .

To implement a Java program that detects if a number is an Armstrong number, the logic involves calculating the sum of its digits each raised to the power of the number of digits, and comparing this sum with the original number. An Armstrong number is one in which this sum equals the number itself. For example, for 153, calculate 1^3 + 5^3 + 3^3 = 153, confirming its Armstrong nature .

A Java program checks if two numbers are co-prime by calculating their greatest common divisor (GCD) using the Euclidean algorithm. Two numbers are co-prime if their GCD is 1. Implement loops or recursion to compute GCD; if it's 1, then the numbers are co-prime, e.g., 14 and 15 .

In a Java program to determine if a number is a palindrome, you reverse the number and compare it to the original number. This involves extracting digits using modulo and division operations, constructing the reverse number, and checking equality with the original. A palindrome number remains the same when read backward, such as 242 .

Implementing a Java program to find the next prime number involves first checking for the primality of the given number. If not prime, increment the number until a prime is found, utilizing efficient primality tests that consider divisibility by 2 and iterate up to the square root for higher efficiency. This continues until a prime is located, such as moving from 14 to 17 .

A Pronic number is a number that is the product of two consecutive integers, expressed as n(n+1). To check for Pronic numbers in a Java program, iterate integers, calculate their product using this formula, and compare it with the given number. If equality is found, it confirms the number is Pronic, such as 12 = 3*4 .

To determine if a number is a Spy number in Java, the program should calculate both the sum and product of its digits. A Spy number has equal sum and product of digits. Extract these digits using loops or recursion, compute their sum and product separately, then check for equality, e.g., 1124 where both the sum (8) and product (8) match .

To calculate the sum of the series S = 2 - 4 + 6 - 8 +... to n terms in Java, iterate through terms, applying alternating sign logic using a (-1)^n factor for subtraction and addition. Simplify with formulas where possible, combining loops and arithmetic operations to handle terms and accumulate sum, demonstrating with sequence evaluations .

You might also like