0% found this document useful (0 votes)
8 views7 pages

Sample Java Programs On Control Structures

The document contains multiple Java programs demonstrating various functionalities such as a student grading system, a simple calculator, a password authentication system, and methods to find the largest of three numbers, sum of natural numbers, factorial of a number, and print elements from arrays. Each program includes user input and outputs results based on the logic implemented. The examples illustrate basic programming concepts including conditionals, loops, and array manipulation.

Uploaded by

Naveen Vnk
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)
8 views7 pages

Sample Java Programs On Control Structures

The document contains multiple Java programs demonstrating various functionalities such as a student grading system, a simple calculator, a password authentication system, and methods to find the largest of three numbers, sum of natural numbers, factorial of a number, and print elements from arrays. Each program includes user input and outputs results based on the logic implemented. The examples illustrate basic programming concepts including conditionals, loops, and array manipulation.

Uploaded by

Naveen Vnk
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/ 7

Student Grading System

import java.util.Scanner;

public class StudentGrading {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter marks: ");

int marks = sc.nextInt();

char grade;

if (marks >= 90)

grade = 'A';

else if (marks >= 80)

grade = 'B';

else if (marks >= 70)

grade = 'C';

else if (marks >= 60)

grade = 'D';

else

grade = 'F';

System.out.println("Grade = " + grade);

Output:

Enter marks: 82

Grade = B

Simple Calculator (Menu-driven)


import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division");

System.out.print("Choose operation: ");

int choice = sc.nextInt();

System.out.print("Enter two numbers: ");

double a = sc.nextDouble(), b = sc.nextDouble();

switch (choice) {

case 1: System.out.println("Sum = " + (a + b)); break;

case 2: System.out.println("Difference = " + (a - b)); break;

case 3: System.out.println("Product = " + (a * b)); break;

case 4:

if (b != 0)

System.out.println("Quotient = " + (a / b));

else

System.out.println("Cannot divide by zero.");

break;

default: System.out.println("Invalid choice");

Output:

1.Addition

2.Subtraction

3.Multiplication

4.Division

Choose operation: 3
Enter two numbers: 7 8

Product = 56.0

Password Authentication System


import java.util.Scanner;

public class AuthSystem {

public static void main(String[] args) {

String username = "admin";

String password = "1234";

Scanner sc = new Scanner(System.in);

System.out.print("Enter username: ");

String user = sc.nextLine();

System.out.print("Enter password: ");

String pass = sc.nextLine();

if (user.equals(username) && pass.equals(password))

System.out.println("Access Granted");

else

System.out.println("Access Denied");

Output:

Enter username: admin

Enter password: 1234

Access Granted

Find Largest of Three Numbers


import java.util.Scanner;

public class LargestThree {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter 3 numbers: ");

int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();

if (a >= b && a >= c)

System.out.println(a + " is largest");

else if (b >= a && b >= c)

System.out.println(b + " is largest");

else

System.out.println(c + " is largest");

Sum of Natural Numbers up to N


import java.util.Scanner;

public class SumNaturalNumbers {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter N: ");

int n = sc.nextInt(), sum = 0;

for (int i = 1; i <= n; i++)

sum += i;

System.out.println("Sum = " + sum);

Find Factorial of a Number


import java.util.Scanner;
public class FactorialWhile {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int fact = 1, i = 1;

while (i <= n) {

fact *= i;

i++;

System.out.println("Factorial: " + fact);

Continue Prompting Until “yes” is Entered


import java.util.Scanner;

public class PromptUntilYes {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String answer;

do {

System.out.print("Do you want to continue? (yes/no): ");

answer = sc.nextLine();

} while (!answer.equalsIgnoreCase("yes"));

System.out.println("Continuing...");

Output:
Do you want to continue? (yes/no): no

Do you want to continue? (yes/no): maybe

Do you want to continue? (yes/no): yes

Continuing...

Print All Elements of an Array


public class PrintArray {

public static void main(String[] args) {

int[] numbers = {10, 20, 30, 40, 50};

for (int num : numbers) {

System.out.println(num);

Output:

10

20

30

40

50

Display All Words in a String Array


public class WordsArray {

public static void main(String[] args) {

String[] words = {"Java", "is", "fun"};

for (String word : words) {

System.out.println(word);

}
}

Output:

Java

is

fun

Print Odd Numbers from Array


public class OddNumbers {

public static void main(String[] args) {

int[] values = {11, 22, 33, 44, 55};

for (int val : values) {

if (val % 2 != 0)

System.out.println(val);

Output:

11

33

55

You might also like