JAVA Practice Set 1
Questions:
Q1: Write a program to sum three numbers in JAVA.
Q2: Write a program to calculate CGPA using marks of three subjects.
Q3: Write a JAVA program which asks the user to enter his/her name and greets them with "Hello <name> have a good day!" txt.
Q4: Write a JAVA program to calculate Kilometers to Miles.
Q5: Write a JAVA program to detect whether a number entered by the user is integer or not.
Solutions:
package com.company;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.Scanner;
public class PS1 {
public static void main(String[] args) {
// QUESTION 1
System.out.println("Question 1");
System.out.println("Sum of Three Numbers");
int a = 10;
int b = 20;
int c = 30;
int sum = a+b+c;
System.out.println(sum + "\n");
// QUESTION 2
System.out.println("Question 2");
System.out.println("CGPA calculation of three subjects");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of Subject 1");
float sub1 = sc.nextFloat();
System.out.println("Enter the number of Subject 2");
float sub2 = sc.nextFloat();
System.out.println("Enter the number of Subject 3");
float sub3 = sc.nextFloat();
System.out.println("Your CGPA is --- ");
float CGPA = (sub1+sub2+sub3)/30;
System.out.println(CGPA + "\n");
// QUESTION 3
System.out.println("Question 3");
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter your name");
String name = sc1.next();
System.out.println("Hello "+ name +" have a good day!\n");
// QUESTION 4
System.out.println("Question 4");
System.out.println("Kilometers to Miles Conversion\n");
System.out.println("Enter Kilometers value");
Scanner sc2 = new Scanner(System.in);
float km = sc2.nextFloat();
System.out.println("Miles");
System.out.println(km / 1.609);
// QUESTION 5
System.out.println("Question 5");
System.out.println("Enter the value");
Scanner sc3 = new Scanner(System.in);
System.out.println(sc3.hasNextInt());
}
}
Comments
Post a Comment