Looping Exercises in Java
Looping Exercises in Java
Write a program that reads a set of integers, and then prints the sum of the even and odd
integers.
import java.util.Scanner;
int number;
char choice;
int evenSum = 0;
int oddSum = 0;
do
{
System.out.print("Enter the number ");
number = console.nextInt();
if( number % 2 == 0)
{
evenSum += number;
}
else
{
oddSum += number;
}
System.out.print("Do you want to continue y/n? ");
choice = console.next().charAt(0);
OR
Write a program that prompts the user to input an integer and then outputs the
number with the digits reversed. For example, if the input is 12345, the output
should be 54321.
import java.util.Scanner;
int number;
int reverse = 0;
while(temp>0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp /= 10;
}
System.out.println("Reverse of " + number + " is " + reverse);
}
}
Write a program that prompts the user to input a positive integer. It should then
output a message indicating whether the number is a prime number.
OR
1. public class Prime {
2.
3. public static void main(String[] args) {
4.
5. int num = 29;
6. boolean flag = false;
7. for(int i = 2; i <= num/2; ++i)
8. {
9. // condition for nonprime number
10. if(num % i == 0)
11. {
12. flag = true;
13. break;
14. }
15. }
16.
17. if (!flag)
18. System.out.println(num + " is a prime number.");
19. else
20. System.out.println(num + " is not a prime number.");
21. }
22. }
Two numbers are entered through the keyboard. Write a program to find the value of one
number raised to the power of another. (Do not use Java built-in method)
import java.util.Scanner;
public class PowerDemo
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int base;
int power;
int result = 1;
System.out.print("Enter the base number ");
base = console.nextInt();
System.out.print("Enter the power ");
power = console.nextInt();
for(int i = 1; i <= power; i++)
{
result *= base;
}
import java.util.Scanner;
int number,
countPositive = 0,
countNegative = 0,
countZero = 0;
char choice;
do
number = console.nextInt();
if(number > 0)
countPositive++;
countNegative++;
}
else
countZero++;
choice = console.next().charAt(0);
Write a program to enter the numbers till the user wants and at the end the program
should display the largest and smallest numbers entered.
import java.util.Scanner;
int number;
char choice;
do
number = console.nextInt();
max = number;
min = number;
choice = console.next().charAt(0);
}
}
Write a program to enter the numbers till the user wants and at the end it should
display the count of positive, negative and zeros entered.
import java.util.Scanner;
{
Scanner console = new Scanner(System.in);
int number,
countPositive = 0,
countNegative = 0,
countZero = 0;
char choice;
do
number = console.nextInt();
if(number > 0)
countPositive++;
countNegative++;
}
else
countZero++;
choice = console.next().charAt(0);
Write a program to enter the numbers till the user wants and at the end the program
should display the largest and smallest numbers entered.
import java.util.Scanner;
int number;
char choice;
do
number = console.nextInt();
max = number;
min = number;
choice = console.next().charAt(0);
Write a program to print out all Armstrong numbers between 1 and 500. If sum of
cubes of each digit of the number is equal to the number itself, then the number is
called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
System.out.println(number);
import java.util.Scanner;
int firstTerm = 0,
secondTerm = 1,
thirdTerm;
number = console.nextInt();
firstTerm = secondTerm;
secondTerm = thirdTerm;
Write a program to calculate the sum of following series where n is input by user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n
import java.util.Scanner;
double sum = 0;
number = console.nextInt();
sum += 1.0/i;
Write a program that generates a random number and asks the user to guess what
the number is. If the user's guess is higher than the random number, the program
should display "Too high, try again." If the user's guess is lower than the random
number, the program should display "Too low, try again." The program should use a
loop that repeats until the user correctly guesses the random number.
import java.util.Scanner;
number = (int) (Math.random() * 100) + 1; // get random number between 1 and 100
System.out.println();
do
guess = console.nextInt();
tries++;
}
else