Java – Loops

Java – For Loop

for loop: Execute a block of instructions repeatedly as long as the condition is valid. We use for loop only when we know the number of iterations to do. Syntax: for(init ; condition ; modify){            statements;} for(start ; stop ; step){            statements;} FlowChart: Print 1 to 10 Numbers: class Code{            public static void main(String[] args) {                        …

Java – For Loop Read More »

Java – For Loop Programs

Find Sum of First N numbers import java.util.Scanner;class Code{            public static void main(String[] args)            {                        Scanner scan = new Scanner(System.in);                        System.out.println(“Enter n value : “);                        int n = scan.nextInt();                        int sum=0;                        for (int i=1 ; i<=n ; ++i)                        {                                    sum = sum+i;                        }                        System.out.println(“Sum of first ” + n + ” numbers is : ” …

Java – For Loop Programs Read More »

Java – Factors Programs

Program to print factors of given number import java.util.Scanner;class Code{            public static void main(String[] args)            {                        Scanner sc = new Scanner(System.in);                        System.out.print(“Enter n value : “);                        int n = sc.nextInt();                        for (int i=1 ; i<=n ; i++)                        {                                    if(n%i==0)                                    {                                                System.out.println(i + ” is a factor”);                                    }                        }            }} Program to count factors of given …

Java – Factors Programs Read More »

Java – Prime and Perfect Numbers

Program to check the input number is Prime or Not import java.util.Scanner;class Code{            public static void main(String[] args)            {                        Scanner sc = new Scanner(System.in);                        System.out.print(“Enter n value : “);                        int n = sc.nextInt();                        int count=0;                        for (int i=1 ; i<=n ; i++)                        {                                    if(n%i==0)                                                count++;                        }                        if(count==2)                                    System.out.println(“Prime Number”);                        else                                    System.out.println(“Not a Prime Number”);            }} …

Java – Prime and Perfect Numbers Read More »

Java – While Loop

While loop: Execute a block of instructions repeatedly until the condition is false. We use while loop only when don ’t know the number of iterations to do. Syntax: while(condition){            statements;} FlowChart: Program to Display Last Digit of given number import java.util.Scanner;class Code{            public static void main(String[] args)            {                        Scanner scan = new Scanner(System.in);                        System.out.print(“Enter …

Java – While Loop Read More »

Java – Check Even or Odd until user exits

Check the given number is Even or Odd until user exits: import java.util.Scanner;public class Check{            public static void main(String[] args)            {                        Scanner scan = new Scanner(System.in);                        while(true)                        {                                    System.out.print(“Enter a number : “);                                    int num = scan.nextInt();                                     if(num%2==0)                                    {                                                System.out.println(num + ” is even.”);                                    }                                    else                                    {                                                System.out.println(num + ” is odd.”);                                    }                                     System.out.print(“Do you …

Java – Check Even or Odd until user exits Read More »

Java – Print Multiplication table until user exits

Program to print Multiplication table for given number until user exits: import java.util.Scanner;public class Check{            public static void main(String[] args)            {                        Scanner scan = new Scanner(System.in);                        while(true)                        {                                    System.out.print(“Enter table number : “);                                    int n = scan.nextInt();                                     for(int i=1 ; i<=10 ; i++)                                    {                                                System.out.println(n + ” * ” + i + ” = ” …

Java – Print Multiplication table until user exits Read More »

Java – Print Prime or Not until User Exits

Program to Print given Number is Prime or Not until User Exits: import java.util.Scanner;public class Check{            public static void main(String[] args)            {                        Scanner scan = new Scanner(System.in);                        while(true)                        {                                    System.out.print(“Enter table number : “);                                    int n = scan.nextInt();                                     int count=0;                                    for(int i=1 ; i<=n ; i++)                                    {                                                if(n%i==0)                                                {                                                            count++;                                                }                                    }                                    if(count==2)                                    {                                                System.out.println(n …

Java – Print Prime or Not until User Exits Read More »

Java – Arithmetic Operations Menu Driven Program using If

Perform Arithmetic Operations in Menu Driven Program using If Block: import java.util.Scanner;class Code{            public static void main(String[] args)            {                        Scanner sc = new Scanner(System.in);                        while(true)                        {                                    System.out.println(“1. Add”);                                    System.out.println(“2. Subtract”);                                    System.out.println(“3. Multiply”);                                    System.out.println(“4. Divide”);                                    System.out.println(“5. Quit”);                                    System.out.print(“Enter your choice : “);                                    int ch = sc.nextInt();                                    if(ch==1)                                    {                                                System.out.println(“Enter 2 numbers : “);                                                int a=sc.nextInt();                                                int …

Java – Arithmetic Operations Menu Driven Program using If Read More »

Scroll to Top