0% found this document useful (0 votes)
29 views10 pages

All PGM

The document contains Java code examples for various looping programs, including printing numbers in a range, reversing numbers, multiplication tables, reversing tables, printing alphabets, even/odd number sums, digit reversing/summing, prime number checking, and strong number validation. It uses for and while loops along with if/else conditional statements to iterate through numbers and perform calculations on them for output.

Uploaded by

optimistabc28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views10 pages

All PGM

The document contains Java code examples for various looping programs, including printing numbers in a range, reversing numbers, multiplication tables, reversing tables, printing alphabets, even/odd number sums, digit reversing/summing, prime number checking, and strong number validation. It uses for and while loops along with if/else conditional statements to iterate through numbers and perform calculations on them for output.

Uploaded by

optimistabc28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

https://www.tutorjoes.

in/java_programming_tutorial/
looping_exercise_programs_in_java
-------------------------------------------------------------
Write a program to print all natural numbers from 1 to n

import java.util.Scanner;
class Natural_Number
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Starting Number : ");
int s =input.nextInt();
System.out.print("Enter The Ending Number : ");
int e =input.nextInt();
while(s<=e)
{
System.out.println(s);
s++;
}
}
}
-----------------------------------------------------------------------------------
-------------
2
import java.util.Scanner;
class Reverse_Numbers
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Starting Number : ");
int s =input.nextInt();
System.out.print("Enter The Ending Number : ");
int e =input.nextInt();
while(s>=e)
{
System.out.println(s);
s--;
}
}
}
-----------------------------------------------------------------------------------
----------------
3
import java.util.Scanner;
class Tables
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Starting Number : ");
int s =input.nextInt();
System.out.print("Enter The Ending Number : ");
int e =input.nextInt();
System.out.print("Enter The Tables Number : ");
int t =input.nextInt();
while(s<=e)
{
System.out.println(s+" * "+t+" = "+(s*t));
s++;
}
-----------------------------------------------------------------------------------
-------------------
4
import java.util.Scanner;
class Reverse_Tables
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Starting Number : ");
int s =input.nextInt();
System.out.print("Enter The Ending Number : ");
int e =input.nextInt();
System.out.print("Enter The Tables Number : ");
int t =input.nextInt();
while(s>=e)
{
System.out.println(s+" * "+t+" = "+(s*t));
s--;
}
-----------------------------------------------------------------------------------
--------------------

5
class Alphabet
{
public static void main(String[] args)
{
char s;
for(s='a';s<='z';s++)
{
System.out.println(s);
}
}
}
-----------------------------------------------------------------------------------
--------------
6
class Reverse_Alphabet
{
public static void main(String[] args)
{
char s;
for(s='Z';s>='A';s--)
{
System.out.println(s);
}
}
}
-----------------------------------------------------------------------------------
--------
7
import java.util.Scanner;
class Even_Numbers
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Number of Limit : ");
int l =input.nextInt();
for(int s=1;s<=l;s++)
{
if(s%2==0)
System.out.println(s);
}
}
}
-----------------------------------------------------------------------------------
-----------
8
import java.util.Scanner;
class Odd_Numbers
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Number of Limit : ");
int l =input.nextInt();
for(int s=1;s<=l;s++)
{
if(s%2==1)
System.out.println(s);
}
}
}
-----------------------------------------------------------------------------------
----------
9
import java.util.Scanner;
class Sum_Numbers
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Number of Limit : ");
int l =input.nextInt();
int sum = 0;
for(int s=1;s<=l;s++)
{
sum = sum + s;

}
System.out.println("Sum of Numbers :"+sum);
}
}
-----------------------------------------------------------------------------------
-----------
10
import java.util.Scanner;
class Sum_Odd_Number
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Number of Limit : ");
int l =input.nextInt();
int sum = 0;
for(int s=1;s<=l;s++)
{
if(s%2==1)
sum = sum + s;

}
System.out.println("Sum of Odd Numbers :"+sum);
}
}
-----------------------------------------------------------------------------------
-------------
11
import java.util.Scanner;
class Sum_Odd_Number
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Number of Limit : ");
int l =input.nextInt();
int sum = 0;
for(int s=1;s<=l;s++)
{
if(s%2==1)
sum = sum + s;

}
System.out.println("Sum of Odd Numbers :"+sum);
}
}
-----------------------------------------------------------------------------------
-------
12
class Ascii_Values
{
public static void main(String[] args)
{
for(int s=1;s<=255;s++)
{
System.out.println((char)s);
}

}
}
-----------------------------------------------------------------------------------
---------
13
import java.util.Scanner;
class Factorial
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Factorial Number : ");
int num = input.nextInt();
int fact = 1;
for(int i=1; i<=num; i++)
{
fact *= i;
}
System.out.println("Factorial: "+ fact);
}
}
-----------------------------------------------------------------------------------
---------
15
import java.util.Scanner;
class Digits_Reverse
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Digits :");
int num = input.nextInt();
int a = num;
int rev = 0;
int rem = 0;
while(num>0)
{
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
System.out.println("Given Digits :" + a);
System.out.println("Reverse Digits :" + rev);
}
}
-----------------------------------------------------------------------------------
--------
16
import java.util.Scanner;
class Sum_Digits
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Digits :");
int num = input.nextInt();
int a = num;
int sum = 0;
int rem = 0;
while(num>0)
{
rem = num % 10;
sum = sum + rem;
num /= 10;
}
System.out.println("Given Digits :" + a);
System.out.println("Sum of Digits :" + sum);
}
}
-----------------------------------------------------------------------------------
--
17
import java.util.Scanner;
class Prime_Numbe
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Number :");
int num = input.nextInt();
int i, count = 0;
for(i=2; i<num; i++)
{
if(num%i == 0)
{
count++;
break;
}
}
if(count==0)
System.out.println("This is a Prime Number.");
else
System.out.println("This is not a Prime Number.");
}
}
-----------------------------------------------------------------------------------
-
21
class Sum_Divisible_Nine
{
public static void main(String[] args)
{
int i, sum=0;
System.out.println("Numbers between 100 and 200, divisible by 9 : \n");
for(i=101;i<200;i++)
{
if(i%9==0)
{
System.out.println(i);
sum+=i;
}
}
}
}
-----------------------------------------------------------------------------------
-----------
23
import java.util.Scanner;
class StrongNumber
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Strong Number : ");
int n = input.nextInt();
int i, n1, s1=0,j;
int fact;
n1 = n;
for(j=n;j>0;j=j/10)
{
fact = 1;
for(i=1; i<=j % 10; i++)
{
fact = fact * i;
}
s1 = s1 + fact;
}
if(s1==n1)
{
System.out.print("Strong Number :"+ n1);
}
else
{
System.out.print("Not Strong Number :"+n1);
}
}
}

-----------------------------------------------------------------
38
import java.util.Scanner;
class Fibonacci
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Fibonacci Number : ");
int num = input.nextInt();
int a=0, b=1, c=0, i;
System.out.println("Fibonacci Numbers ... ");
for(i=1; i<=num; i++)
{
System.out.println(c);
a = b;
b = c;
c = a + b;
}
}
}
--------------------------------------------------------------------------
39
import java.util.Scanner;
class Strong_Number
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Number : ");
int n = input.nextInt();
int i,fact_n,rem,total = 0,temp = n;
while(n != 0)
{
i = 1;
fact_n = 1;
rem = n % 10;
while(i <= rem)
{
fact_n = fact_n * i;
i++;
}
total = total + fact_n;
n = n / 10;
}
if(total == temp)
System.out.println(temp + " is a Strong Number");
else
System.out.println(temp + " is not a Strong Number");
}
}
-----------------------------------------------------------------------------------
----
40
class AllStrong_Number
{
public static void main(String[] args)
{
for (int i = 1; i <= 100000; i++)
{
if (isItStrong(i))
{
System.out.println(i);
}
}
}
static boolean isItStrong(int num) {
int no = num;
int sum = 0;
while (no > 0) {
int digit = no % 10;
sum += fact(digit);

no = no / 10;
}
return sum == num;
}
static int fact(int digit) {
int f = 1;
for (int j = digit; j > 1; j--) {
f *= j;
}
return f;
}
}
-----------------------------------------------------------------------------------
-------
41
class AllPerfect_Number
{
static boolean Perfect_Number(int num)
{
int sum = 0;
for(int i=1; i<num; i++)
{
if(num%i==0)
{
sum = sum+i;
}
}
if(sum==num)
return true;
else
return false;
}
public static void main(String args[])
{
for(int i=1; i<=10000; i++)
{
if(Perfect_Number(i))
System.out.println(i);
}
}
}
-----------------------------------------------------------------------------------
------
42
import java.util.Scanner;
class Perfect_Number
{
static boolean Check_Perfect(int n)
{
if (n == 1)
return false;
int sum = 1;
for (int i = 2; i < n; i++) {

if (n % i == 0) {
sum += i;
}
}
if (sum == n)
return true;

return false;
}

public static void main(String[] args)


{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Number : ");
int n = input.nextInt();
if (Check_Perfect(n))
System.out.println(n + " is a Perfect Number");
else
System.out.println(n + " is not a Perfect Number");
}
}
-----------------------------------------------------------------------------------
--
43
class All_Armstrong
{
public static void main(String arg[])
{
int i=1,arm;
System.out.println("Armstrong Numbers Between 1 to 1000");
while(i<1000)
{
arm=armstrong(i);
if(arm==i)
System.out.println(i);
i++;
}
}
static int armstrong(int num)
{
int x,a=0;
while(num!=0)
{
x=num%10;
a=a+(x*x*x);
num/=10 ;
}
return a;
}
}
-----------------------------------------------------------------------------------
---------
42
import java.util.Scanner;
class Perfect_Number
{
static boolean Check_Perfect(int n)
{
if (n == 1)
return false;
int sum = 1;
for (int i = 2; i < n; i++) {

if (n % i == 0) {
sum += i;
}
}
if (sum == n)
return true;

return false;
}

public static void main(String[] args)


{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Number : ");
int n = input.nextInt();
if (Check_Perfect(n))
System.out.println(n + " is a Perfect Number");
else
System.out.println(n + " is not a Perfect Number");
}
}
-----------------------------------------------------------------------------------
------
43

You might also like