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

java-1 2

The document contains multiple Java programs demonstrating various programming concepts including printing messages, temperature conversion, typecasting, voting eligibility, finding the greatest number, prime number checking, restaurant management, calculator operations, bitwise operations, loops, and pattern printing. Each program includes user input handling and output display. The code snippets illustrate fundamental programming techniques and control structures in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java-1 2

The document contains multiple Java programs demonstrating various programming concepts including printing messages, temperature conversion, typecasting, voting eligibility, finding the greatest number, prime number checking, restaurant management, calculator operations, bitwise operations, loops, and pattern printing. Each program includes user input handling and output display. The code snippets illustrate fundamental programming techniques and control structures in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Write a program to Print Hello World using escape sequence \n, \r

public class abc {

public static void main(String[] args) {

System.out.println("Hello\r world\n");

}
Write a program to convert temperature from Celsius to Fahrenheit by

taking input from the user.

import java.util.Scanner;

class DegFaren {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the Celsius value: ");

float d = sc.nextFloat();

float fahrenheit = (d * 9 / 5) + 32;

System.out.println("The Fahrenheit value is: " + fahrenheit);

}
Write a program to accept values in different data types, try to assign int to

double, int to byte, and vice versa. Demonstrate the use of Implicit

typecasting and Explicit Type Casing

import java.util.Scanner;

public class TypeCasting {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter an integer: ");

int intValue = sc.nextInt();

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

double doubleValue = sc.nextDouble();

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

byte byteValue = sc.nextByte();

double implicitDouble = intValue;

System.out.println("\nImplicit Typecasting: int to double: " + implicitDouble);

int explicitInt = (int) doubleValue;

System.out.println("\nExplicit Typecasting: double to int: " + explicitInt);

byte explicitByte = (byte) intValue;

System.out.println("\nExplicit Typecasting: int to byte: " + explicitByte);

int byteToInt = byteValue;

System.out.println("\nImplicit Typecasting: byte to int: " + byteToInt);

System.out.print("\nEnter a large integer to demonstrate int to byte overflow: ");

int largeInt = sc.nextInt();

byte overflowByte = (byte) largeInt;

System.out.println("Explicit Typecasting (int to byte with overflow): " + overflowByte);

sc.close();
}

}
Write a program to check whether the person’s age allows him to cast the

vote or not

import java.util.Scanner;

public class Vote {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your age: ");

int age = scanner.nextInt();

if (age >= 18) {

System.out.println("You are eligible to vote.");

} else {

System.out.println("You are not eligible to vote.");

scanner.close();

}
Write a program to check whether the person’s age and citizenship allows

him to cast a vote or not

import java.util.Scanner;

public class eligibility {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your age: ");

int age = scanner.nextInt();

System.out.print("Are you a citizen of this country (yes/no)? ");

String citizenship = scanner.next();

if (age >= 18 && citizenship.equalsIgnoreCase("yes")) {

System.out.println("You are eligible to vote.");

} else if (age < 18) {

System.out.println("You are not eligible to vote because you are under 18.");

} else {

System.out.println("You are not eligible to vote because you are not a citizen.");

scanner.close();

}
Write a program to find the greatest number among the 3 numbers given by

the user.
import java.util.Scanner;

public class GreatestNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");

double num1 = scanner.nextDouble();

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

double num2 = scanner.nextDouble();

System.out.print("Enter the third number: ");

double num3 = scanner.nextDouble();

double greatest;

if (num1 >= num2 && num1 >= num3) {

greatest = num1;

} else if (num2 >= num1 && num2 >= num3) {

greatest = num2;

} else {

greatest = num3;

System.out.println("The greatest number is: " + greatest);

scanner.close();

}
Write a program to check if a given number is a prime number or not.
import java.util.Scanner;

public class PrimeNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

int num = scanner.nextInt();

if (num <= 1) {

System.out.println(num + " is not a prime number.");

} else {

boolean isPrime = true;

for (int i = 2; i <= Math.sqrt(num); i++) {

if (num % i == 0) {

isPrime = false;

break;

}}

if (isPrime) {

System.out.println(num + " is a prime number.");

} else {

System.out.println(num + " is not a prime number.");

}}

scanner.close();

}
Write a program to design a Food/ Restaurant Management System using

Nested if or Nested Switch

import java.util.Scanner;

public class Restaurant {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Welcome to XYZ Restaurant");

System.out.println("Please choose a category:");

System.out.println("1. Starters");

System.out.println("2. Main Course");

System.out.println("3. Desserts");

System.out.println("4. Drinks");

int categoryChoice = scanner.nextInt();

double totalBill = 0;

if (categoryChoice == 1) {

System.out.println("Choose a starter:");

System.out.println("1. Soup - $5");

System.out.println("2. Spring Rolls - $7");

System.out.println("3. Garlic Bread - $4");

int starterChoice = scanner.nextInt();

if (starterChoice == 1) {

totalBill += 5;

} else if (starterChoice == 2) {

totalBill += 7;

} else if (starterChoice == 3) {
totalBill += 4;

} else {

System.out.println("Invalid choice, no starter added.");

} else if (categoryChoice == 2) {

System.out.println("Choose a main course:");

System.out.println("1. Pizza - $10");

System.out.println("2. Burger - $8");

System.out.println("3. Pasta - $9");

int mainCourseChoice = scanner.nextInt();

if (mainCourseChoice == 1) {

totalBill += 10;

} else if (mainCourseChoice == 2) {

totalBill += 8;

} else if (mainCourseChoice == 3) {

totalBill += 9;

} else {

System.out.println("Invalid choice, no main course added.");}

} else if (categoryChoice == 3) {

System.out.println("Choose a dessert:");

System.out.println("1. Ice Cream - $3");

System.out.println("2. Cake - $4");

System.out.println("3. Pie - $5");

int dessertChoice = scanner.nextInt();

if (dessertChoice == 1) {

totalBill += 3;

} else if (dessertChoice == 2) {

totalBill += 4;
} else if (dessertChoice == 3) {

totalBill += 5;

} else {

System.out.println("Invalid choice, no dessert added.");

} else if (categoryChoice == 4) {

System.out.println("Choose a drink:");

System.out.println("1. Water - $1");

System.out.println("2. Soda - $2");

System.out.println("3. Juice - $3");

int drinkChoice = scanner.nextInt();

if (drinkChoice == 1) {

totalBill += 1;

} else if (drinkChoice == 2) {

totalBill += 2;

} else if (drinkChoice == 3) {

totalBill += 3;

} else {

System.out.println("Invalid choice, no drink added.");

} else {

System.out.println("Invalid category choice.");

System.out.println("Total Bill: $" + totalBill);

scanner.close();

}
Write a program to make a calculator (+,-, *,/,%) depicting all arithmetic

operators

import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");

double num1 = scanner.nextDouble();

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

double num2 = scanner.nextDouble();

System.out.println("Choose an operator (+, -, *, /, %): ");

char operator = scanner.next().charAt(0);

double result = 0;

if (operator == '+') {

result = num1 + num2;

} else if (operator == '-') {

result = num1 - num2;

} else if (operator == '*') {

result = num1 * num2;

} else if (operator == '/') {

if (num2 != 0) {

result = num1 / num2;

} else {

System.out.println("Error! Division by zero.");

return;

}
} else if (operator == '%') {

result = num1 % num2;

} else {

System.out.println("Invalid operator!");

return;

System.out.println("Result: " + num1 + " " + operator + " " + num2 + " = " + result);

scanner.close();

}
Write a program using bitwise operator (&amp;,|,^,~, &gt;&gt;, &lt;&lt;, &gt;&gt;&gt;)

import java.util.Scanner;

public class Operations {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

int num1 = scanner.nextInt();

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

int num2 = scanner.nextInt();

int andResult = num1 & num2;

System.out.println("Bitwise AND (num1 & num2): " + andResult);

int orResult = num1 | num2;

System.out.println("Bitwise OR (num1 | num2): " + orResult);

int xorResult = num1 ^ num2;

System.out.println("Bitwise XOR (num1 ^ num2): " + xorResult);

int notResult1 = ~num1;

int notResult2 = ~num2;

System.out.println("Bitwise NOT (~num1): " + notResult1);

System.out.println("Bitwise NOT (~num2): " + notResult2);

int leftShiftResult = num1 << 2; // Shifting num1 left by 2 positions

System.out.println("Bitwise Left Shift (num1 << 2): " + leftShiftResult);

int rightShiftResult = num1 >> 2; // Shifting num1 right by 2 positions

System.out.println("Bitwise Right Shift (num1 >> 2): " + rightShiftResult);

int unsignedRightShiftResult = num1 >>> 2; // Unsigned shifting num1 right by 2 positions

System.out.println("Bitwise Unsigned Right Shift (num1 >>> 2): " + unsignedRightShiftResult);

scanner.close();

}
}
Write a program to execute the loop from 1 to 10 and display output: 1,3,5,9

public class Loop {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

if (i == 1 || i == 3 || i == 5 || i == 9) {

System.out.print(i + " ");

}
Write a program to execute the loop 10 times from 10 to 1 and display

output: 10,9,8,7,6,5,4,3,2,1

public class Countdown {

public static void main(String[] args) {

for (int i = 10; i >= 1; i--) {

System.out.print(i + ",");

}
Write a program to display the following pattern upto N rows, taking the

value of N from the user:

12

123

1234

import java.util.Scanner;

public class NumberPattern {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of rows (N): ");

int N = scanner.nextInt();

for (int i = 1; i <= N; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(j);

System.out.println();

scanner.close();

}
Write a program to input marks of 50 students using an array and display

the average marks of the class.

import java.util.Scanner;

public class marks {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int[] marks = new int[10];

int totalMarks = 0;

System.out.println("Enter the marks of 10 students:");

for (int i = 0; i < 10; i++) {

System.out.print("Enter marks of student " + (i + 1) + ": ");

marks[i] = scanner.nextInt();

totalMarks += marks[i];

double averageMarks = totalMarks / 10.0;

System.out.println("The average marks of the class are: " + averageMarks);

scanner.close();

}
Write a program to display the following pattern upto

**

***

****

public class StarPattern {

public static void main(String[] args) {

for (int i = 1; i <= 4; i++) {

for (int j = 1; j <= i; j++) {

System.out.print("*");

System.out.println();

}
Write a program to display the following pattern

******

***

public class StarPattern {

public static void main(String[] args) {

for (int i = 6; i >= 1; i -= 2) {

for (int j = 1; j <= i; j++) {

System.out.print("*");

System.out.println();

}
Write a program where a loop will execute from 1 to 1000 times, but break

after it receives 3

public class BreakLoop{

public static void main(String[] args) {

for (int i = 1; i <= 1000; i++) {

System.out.println(i);

if (i == 3) {

System.out.println("Breaking the loop as the number 3 is reached.");

break;

You might also like