Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Java - Questions and Answers

1. The document describes a Java program that uses a while loop to repeatedly print "Hello World" 5 times. 2. It initializes an integer i to 1, then loops while i is less than 6. Inside the loop it prints "Hello World" and increments i by 1. 3. This causes "Hello World" to print 5 times before the condition i < 6 becomes false and exits the loop.

Uploaded by

Nazia Bee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java - Questions and Answers

1. The document describes a Java program that uses a while loop to repeatedly print "Hello World" 5 times. 2. It initializes an integer i to 1, then loops while i is less than 6. Inside the loop it prints "Hello World" and increments i by 1. 3. This causes "Hello World" to print 5 times before the condition i < 6 becomes false and exits the loop.

Uploaded by

Nazia Bee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java – While Loop

class whileLoopDemo { Program starts.


    public static void main(String args[]) 2. i is initialized with value 1.
    { 3. Condition is checked. 1 < 6 yields true.
        // initialization expression 3.a) "Hello World" gets printed 1st time.
        int i = 1; 3.b) Updation is done. Now i = 2.
        // test expression 4. Condition is checked. 2 < 6 yields true.
        while (i < 6) { 4.a) "Hello World" gets printed 2nd time.
            System.out.println("Hello World"); 4.b) Updation is done. Now i = 3.
            // update expression 5. Condition is checked. 3 < 6 yields true.
            i++; 5.a) "Hello World" gets printed 3rd time
Hello World
        } 5.b) Updation is done. Now i = 4.
Hello World
    } 6. Condition is checked. 4 < 6 yields true.
Hello World
} 6.a) "Hello World" gets printed 4th time
Hello World
6.b) Updation is done. Now i = 5.
Hello World
7. Condition is checked. 5 < 6 yields true.
7.a) "Hello World" gets printed 5th time
7.b) Updation is done. Now i = 6.
8. Condition is checked. 6 < 6 yields false.
9. Flow goes outside the loop. Program terminates.

class whileLoopDemo { Total Sum Try: class printNumbers{


    public static void main(String args[]) public static void
    { main (Strimg arg[]) {
        int x = 1, sum = 0; int x=1;
 // Exit when x becomes greater than 4 while(x < 5) { 1
        while (x <= 10) { System.out.println (x);
2
            // summing up x x++;
            sum = sum + x; }} 3
// Increment the value of x for 55
            // next iteration 4
            x++; 5
        }
        System.out.println("Summation: " + sum);
    } }
Multiplication number num = console.nextInt();

import java.util.Scanner; System.out.println("Multiplication Table of " + num);


public class Table for(int i=1; i<=10; i++)
{ {
public static void main(String[] args) System.out.println(num +" x " + i + " = " + (num*i) );
{ }
Scanner console = new Scanner(System.in); }
int num; }
System.out.print("Enter any positive integer: ");

1
Factorial Number package hello_world; Power base
package hello_world; import java.util.Scanner;
import java.util.Scanner; public class power {
public class factorial public static void main(String[] args)
{ {
public static void main(String[] args) Scanner console = new Scanner(System.in);
{ int base;
int power;
Scanner console = new Scanner(System.in); int result = 1;
int num; // To hold number
int fact = 1; // To hold factorial System.out.print("Enter the base number ");
System.out.print("Enter any positive integer: "); base = console.nextInt();
num = console.nextInt(); System.out.print("Enter the power ");
for(int i=1; i<=num; i++) power = console.nextInt();
{ for(int i = 1; i <= power; i++)
fact *= i; 1*2*3*4*5 = 120 {
} result *= base;
System.out.println("Factorial: "+ fact); }
}} System.out.println("Result: "+ result);
}}
23 = 2*2*2 = 8
Reverse number Print of sum and odd numbers - don’t understand
package hello_world;
import java.util.Scanner; import java.util.Scanner;
public class reverse_Number{ public class Sum_Even_Odd_Numbers
public static void main(String[] args) {
public static void main(String[] args)
{
{
Scanner console = new Scanner(System.in); Scanner console = new Scanner(System.in);
int number; int number;
int reverse = 0; char choice;
System.out.print("Enter the number "); int evenSum = 0;
number = console.nextInt(); int oddSum = 0;
int remainder = 0; 123456 do
while(number>0) {
{ System.out.print("Enter the number ");
654321 number = console.nextInt();
remainder = number % 10;
reverse = reverse * 10 + remainder; if( number % 2 == 0)
{
number /= 10;
evenSum += number;
} }
System.out.println("Reverse of " + number + " is " + else
reverse); {
} oddSum += number;
} }
// why use temp variable System.out.print("Do you want to continue y/n? ");
choice = console.next().charAt(0);
}while(choice=='y' || choice == 'Y');
System.out.println("Sum of even numbers: " + evenSum);
System.out.println("Sum of odd numbers: " + oddSum);

2
}
}

HCF Sum two numbers using


package hello_world; package hello_world;
import java.util.Scanner; import java.util.Scanner;
public class HCF{ public class Sum_two_numbers{
public static void main(String[] args){
public static void main(String[] args)
Scanner console = new Scanner(System.in); {
int dividend, divisor; Scanner console = new Scanner(System.in);
int remainder, hcf = 0; int number1, number2;
char choice;
System.out.print("Enter the first number "); do
dividend = console.nextInt(); {
System.out.print("Enter the second number "); System.out.print("Enter the first number ");
divisor = console.nextInt(); number1 = console.nextInt();

do System.out.print("Enter the second number ");


{ number2 = console.nextInt();
remainder = dividend % divisor;
if(remainder == 0) int sum = number1 + number2;
{ System.out.println("Sum of numbers: " + sum);
hcf = divisor;
} System.out.print("Do you want to continue y/n? ");
else choice = console.next().charAt(0);
{ System.out.println();
dividend = divisor;
divisor = remainder; }while(choice=='y' || choice == 'Y');
} }
}while(remainder != 0); Using choice of YES or No
System.out.println("HCF: " + hcf); } continue writing
}}

You might also like