looping
LOOPING CONCEPT
1.Print numbers from 1 to 10.
Program:
public class loop1 {
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
}
}
Output:
2.Print even numbers between 1 and 20.
Program:
public class even2{
public static void main(String[] args) {
for (int i = 2; i <= 20; i += 2) {
System.out.print(i + " ");
}
System.out.println();
}
}
Output:
3.Print the multiplication table of a given number.
Program:
public class multi3{
public static void main(String[] args){
for (int i = 1; i <= 10; i++) {
System.out.println(i + " x " + i + " = " + (i * i));
}
}
}
Output:
4.Print the sum of first 10 natural numbers.
Program:
public class natural4{
public static void main(String[] args){
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum = " + sum);
}
}
Output:
5.Print the sum of first N even numbers.
Program:
import java.util.Scanner;
public class small15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: Enter a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Handle negative numbers
number = Math.abs(number);
int smallestDigit = 9; // Initialize with the largest possible digit
if (number == 0) {
smallestDigit = 0;
}
while (number > 0) {
int digit = number % 10;
if (digit < smallestDigit) {
smallestDigit = digit;
}
// Exit early if 0 is found (smallest possible digit)
if (smallestDigit == 0) {
break;
}
number /= 10;
}
System.out.println("Smallest digit is: " + smallestDigit);
}
}
Output:
6.Print the factorial of a number.
Program:
import java.util.Scanner;
public class factorial6{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("Factorial of " + number + " is: " + factorial);
scanner.close();
}
}
Output:
Program:
import java.util.Scanner;
public class reverse7{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
System.out.println("Reversed number is: " + reversed);
scanner.close();
}
}
Output:
8.Print the sum of digits of a number.
Program:
import java.util.Scanner;
public class digit8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int sum = 0;
int temp = Math.abs(number);
while (temp != 0) {
sum += temp % 10;
temp /= 10;
}
System.out.println("Sum of digits of " + number + " is: " + sum);
scanner.close();
}
}
Output:
9.Print the product of digits of a number.
Program:
import java.util.Scanner;
public class pro9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int product = 1;
int temp = Math.abs(number);
if (temp == 0)
{
product = 0;
}
else
{
while (temp != 0) {
int digit = temp % 10;
product *= digit;
temp /= 10;
}
}
System.out.println("Product of digits of " + number + " is: " + product);
scanner.close();
}
}
Output:
10.Check if a number is a palindrome.
Program:
import java.util.Scanner;
public class palin10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int original = number;
int reversed = 0;
int temp = Math.abs(number);
while (temp != 0) {
int digit = temp % 10;
reversed = reversed * 10 + digit;
temp /= 10;
}
if (Math.abs(original) == reversed) {
System.out.println(number + " is a palindrome.");
} else {
System.out.println(number + " is not a palindrome.");
}
scanner.close();
}
}
Output:
11.Check if a number is an Armstrong number.
Program:
import java.util.Scanner;
public class arm11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int original = number;
int sum = 0;
int digits = String.valueOf(Math.abs(number)).length();
int temp = Math.abs(number);
while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}
if (sum == Math.abs(original)) {
System.out.println(original + " is an Armstrong number.");
} else {
System.out.println(original + " is not an Armstrong number.");
}
scanner.close();
}
}
Output:
12.Print the Fibonacci series up to N terms.
Program:
import java.util.Scanner;
public class fibonacci12{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms (N): ");
int N = scanner.nextInt();
int first = 0, second = 1;
System.out.println("Fibonacci series up to " + N + " terms:");
for
(int i = 1; i <= N; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
scanner.close();
}
}
Output:
13.Count the number of digits in a number.
Program:
import java.util.Scanner;
public class countdigits13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int count = 0;
int temp = Math.abs(number);
if (temp == 0) {
count = 1;
} else {
while (temp != 0) {
temp /= 10;
count++;
}
}
System.out.println("Number of digits in " + number + " is: " + count);
scanner.close();
}
}
Output:
14.Find the largest digit in a number.
Program:
import java.util.Scanner;
public class largest14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int temp = Math.abs(number);
int maxDigit = 0;
if (temp == 0) {
maxDigit = 0;
} else {
while (temp != 0) {
int digit = temp % 10;
if (digit > maxDigit) {
maxDigit = digit;
}
temp /= 10;
}
}
System.out.println("The largest digit in " + number + " is: " + maxDigit);
scanner.close();
}
}
Output:
15.Find the smallest digit in a number.
Program:
import java.util.Scanner;
public class small15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
number = Math.abs(number);
int smallestDigit = 9;
if (number == 0) {
smallestDigit = 0;
}
while (number > 0) {
int digit = number % 10;
if (digit < smallestDigit) {
smallestDigit = digit;
}
if (smallestDigit == 0) {
break;
}
number /= 10;
}
System.out.println("Smallest digit is: " + smallestDigit);
}
}
Output:
16.Print a square pattern of * (e.g., 5×5).
Program:
import java.util.Scanner;
public class square16{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the square: ");
int size = scanner.nextInt();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
17.Print a triangle pattern of *.
Program:
import java.util.Scanner;
public class triangle17 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:














Comments
Post a Comment