Basic question 1
//1. Write a Java program to print 'Hello' on screen and your name on a separate line.
- public class practice1 {public static void main(String[] args) {System.out.println( "rakesh" );}}
- import java.util.Scanner;public class practice2 {//Write a Java program to print the sum of two numbers.// public static void main(String[] args) {// int i=5, y=8;// System.out.println(i+y);// }// input methodepublic static void main(String[] args) {Scanner sc = new Scanner(System.in);int a = sc.nextInt();System.out.print("=");int b =sc.nextInt();System.out.print(a+b);sc.close();}}
- public class practice3 {//Write a Java program to divide two numbers and print them on the screen.// public static void main(String[] args) {// int a=50, b=3;// System.out.println(a/b);// }// output 16 because here note is use float type and below try to use float typepublic static void main(String[] args) {float a=33,b=10;System.out.println(a/b);}}// out is 3.3 because i am also use float type date type
- // 4. Write a Java program to print the results of the following operations.// Test Data:// a. -5 + 8 * 6// b. (55+9) % 9// c. 20 + -3*5 / 8// d. 5 + 15 / 3 * 2 - 8 % 3import java.util.Scanner;public class practice4 {public static void main(String[] args) {// A. I am try to solve using input methode this questionScanner sc = new Scanner (System.in);System.out.println("Enter your input");int a = sc.nextInt();int b = sc.nextInt();int c = sc.nextInt();System.out.println(a+b*c);sc.close();// output is 43//b.try to solve using declare deta typeint d =(55+9), e=9;System.out.println(d%e);//out put 1//C.int f = 20, g=-3*5, h=8;System.out.println(f+g/h);//out put 19//d.int i =5,j=15/3*2, k=8%3;System.out.println(i+j-k);//out put is 13}}
- // //5. Write a Java program that takes two numbers as input and displays the product of two numbers.// Test Data:// Input first number: 25// Input second number: 5// Expected Output :// 25 x 5 = 125import java.util.Scanner;public class practice5 {public static void main(String[] args) {Scanner sc = new Scanner (System.in);int a = sc.nextInt();int b= sc.nextInt();System.out.println(a*b);sc.close();}}
- // 6. Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two numbers.// Test Data:// Input first number: 125// Input second number: 4// Expected Output :// 125 + 24 = 149// 125 - 24 = 101// 125 x 24 = 3000// 125 / 24 = 5// 125 mod 24 = 5import java.util.Scanner;public class practice6 {public static void main(String[] args) {Scanner sc = new Scanner (System.in);int a = sc.nextInt();int b = sc.nextInt();System.out.println(a + "+" +b + "=" + (a+b));System.out.println(a + "-" +b + "=" + (a-b));System.out.println(a + "*" +b + "=" + (a*b));System.out.println(a + "/" +b + "=" + (a/b));sc.close();// input is 125 4//out put is bellow// 125+4=129// 125-4=121// 125*4=500// 125/4=31}}
- // 7. Write a Java program that takes a number as input and prints its multiplication table up to 10.// Test Data:// Input a number: 8// Expected Output :// 8 x 1 = 8// 8 x 2 = 16// 8 x 3 = 24// ...// 8 x 10 = 80import java.util.Scanner;public class practice7 {public static void main(String[] args) {Scanner sc = new Scanner (System.in);int j = sc.nextInt();for(int i =1 ; i<=10; i++){System.out.println(j + "*" + i + "=" + (j*i));sc.close();}}}// input is 8// ouput is below// 8*1=8// 8*2=16// 8*3=24// 8*4=32// 8*5=40// 8*6=48// 8*7=56// 8*8=64// 8*9=72// 8*10=80
- // Write a Java program to display the following pattern.// Sample Pattern :// J a v v a// J a a v v a a// J J aaaaa V V aaaaa// JJ a a V a apublic class practice8 {public static void main(String[] args) {// Display the characters to form the text "Java" in a specific patternSystem.out.println(" J a v v a ");System.out.println(" J a a v v a a");System.out.println("J J aaaaa V V aaaaa");System.out.println(" JJ a a V a a");}}//out put bellow// J a v v a// J a a v v a a// J J aaaaa V V aaaaa// JJ a a V a a
- // Write a Java program to compute the specified expressions and print the output.// Test Data:// ((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5))public class practice9 {public static void main(String[] args) {System.out.println((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5));}}// ouput is 2.138888888888889
- // 10. Write a Java program to compute a specified formula.// Specified Formula :// 4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))// Expected Outputpublic class practice10 {public static void main(String[] args) {System.out.println(4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11)));}}// output 2.9760461760461765
- // Write a Java program to print the area and perimeter of a circle.// Test Data:// Radius = 7.5import java.util.Scanner;public class practice11 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);float a = sc.nextFloat();//area isSystem.out.println(Math.PI * a*a);//peremeter isSystem.out.println(2* Math.PI*a);sc.close();}}// output is// 176.71458676442586// 47.12388980384689
- // Write a Java program that takes three numbers as input to calculate and print the average of the numbers.import java.util.Scanner;public class practice12 {public static void main(String[] args) {Scanner sc = new Scanner (System.in);int a = sc.nextInt();int b = sc.nextInt();int c = sc.nextInt();System.out.println(a+b+c/3);sc.close();}}// input is// 85// 55// 45// ouput is 155
- // 13. Write a Java program to print the area and perimeter of a rectangle.// Test Data:// Height = 5.5 width = 8.5import java.util.Scanner;public class practice13 {public static void main(String[] args) {Scanner sc = new Scanner (System.in);System.out.print("Enter your height ");float Height = sc.nextFloat();System.out.print("Enter your width");float width = sc.nextFloat();System.out.print("area is ");System.out.println(Height*width);System.out.print("peremiter is ");System.out.print(2*(Height+width));sc.close();}}// input is// Enter your height 5.5// Enter your width8.5// out put is// area is 46.75// peremiter is 28.0
- public class practice14 {public static void main(String[] args) {// Display Java versionSystem.out.println("\nJava Version: " + System.getProperty("java.version"));// Display Java runtime versionSystem.out.println("Java Runtime Version: " + System.getProperty("java.runtime.version"));// Display Java home directorySystem.out.println("Java Home: " + System.getProperty("java.home"));// Display Java vendor nameSystem.out.println("Java Vendor: " + System.getProperty("java.vendor"));// Display Java vendor URLSystem.out.println("Java Vendor URL: " + System.getProperty("java.vendor.url"));// Display Java class pathSystem.out.println("Java Class Path: " + System.getProperty("java.class.path") + "\n");}}// Java Version: 20// Java Runtime Version: 20+36-2344// Java Home: C:\Program Files\Java\jdk-20// Java Vendor: Oracle Corporation// Java Vendor URL: https://java.oracle.com/// Java Class Path:
- // 15. Write a Java program to swap two variables.import java.util.Scanner;public class practice15 {public static void main(String[] args) {Scanner sc = new Scanner (System.in);int a = sc.nextInt();int b = sc.nextInt();a = a+b;b = a-b;a = a-b;System.out.println("a is " + a + "b is " +b);sc.close();}}// input is 10 20// out put is a is 20b is 10