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

2ex.java

The document contains several simple Java programs demonstrating basic programming concepts such as printing output, decision making with if-else statements, branching using switch-case, and looping with for, while, and do-while loops. Each section includes example code and expected outputs for user inputs. The programs cover basic arithmetic, input handling, and control flow in Java.

Uploaded by

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

2ex.java

The document contains several simple Java programs demonstrating basic programming concepts such as printing output, decision making with if-else statements, branching using switch-case, and looping with for, while, and do-while loops. Each section includes example code and expected outputs for user inputs. The programs cover basic arithmetic, input handling, and control flow in Java.

Uploaded by

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

1.

Simple java program

import java.util.*;

public class SimpleProgram

public static void main(String[] args)

System.out.println("Hello, World!");

int num1 = 10;

int num2 = 20;

int sum = num1 + num2;

System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);

output:

Hello, World!

The sum of 10 and 20 is: 30

2.

1.1 decision making java program.

import java.util.Scanner;

public class DecisionMaking


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

// Input a number from the user


System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Decision-making using if-else
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}

scanner.close();
}
}

Example Output 1:

Enter a number: 25
The number is positive.

Example Output 2:

Enter a number: -10


The number is negative.

Example Output 3:

Enter a number: 0
The number is zero.

1.2.branching statement using java.

import java.util.Scanner;

public class BranchingStatement


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

// Input: User enters a number (1-7)


System.out.print("Enter a number (1-7) to find the day of the week: ");
int day = scanner.nextInt();

// Branching using switch-case


switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid input! Please enter a number between 1 and 7.");
}

scanner.close();
}
}

Example Output 1:

Enter a number (1-7) to find the day of the week: 3


Wednesday

Example Output 2:

Enter a number (1-7) to find the day of the week: 6


Saturday

Example Output 3 (Invalid Input):

Enter a number (1-7) to find the day of the week: 10


Invalid input! Please enter a number between 1 and 7.

1.3. looping statement using java

a. using a forloop:

import java.util.Scanner;

public class LoopingStatement


{

public static void main(String[] args)

System.out.println("The first 10 natural numbers are:");

// Looping using a for loop for (int i = 1; i <= 10; i++)

System.out.println(i);

Output:

The first 10 natural numbers are:

10

b. using a while loop:

import java.util.Scanner;

public class LoopingStatementWhile

{
public static void main(String[] args)

System.out.println("The first 10 natural numbers are:");

int i = 1; // Initialization while (i <= 10)

// Condition System.out.println(i);

i++; // Increment

Output:

The first 10 natural numbers are:

1 2 3 4 5 6 7 8 9 10

c. using while loop.

public class DoWhileExample


{
public static void main(String[] args)
{
System.out.println("The first 10 natural numbers are:");

int i = 1; // Initialization

// Looping using a do-while loop


do {
System.out.println(i);
i++; // Increment
} while (i <= 10); // Condition
}
}

Output:

The first 10 natural numbers are:


1
2
3
4
5
6
7
8
9
10

You might also like