- Write a program to sum three numbers in Java.
- Write a program to calculate CGPA using marks of three subjects (out of 100)
- Write a Java program that asks the user to enter his/her name and greets them with “Hello <name>, have a good day” text.
- Write a Java program to convert Kilometers to miles.
- Write a Java program to detect whether a number entered by the user is an integer or not.
Code as Described in the Video
package com.company;
import java.util.Scanner;
public class CWH_Ch1_PS {
public static void main(String[] args) {
// Question1
// int a = 4;
// int b = 17;
// int c =6;
// int sum = a + b+c;
// System.out.println(sum);
// Question2
// float subject1 = 45;
// float subject2 = 95;
// float subject3 = 48;
// float cgpa = (subject1 + subject2 +subject3)/30;
// System.out.println(cgpa);
// Question 3
// System.out.println("What is your name");
// Scanner sc = new Scanner(System.in);
// String name = sc.next();
// System.out.println("Hello " + name + " have a good day!");
// Question 5
System.out.println("Enter your number");
Scanner sc = new Scanner(System.in);
System.out.println(sc.hasNextInt());
}
}
Solutions: 1. Write a program to sum three numbers in Java.
2. Write a program to calculate CGPA using marks of three subjects (out of 100)
3. Write a Java program that asks the user to enter his/her name and greets them with “Hello {name}, have a good day” text.
4. Write a Java program to convert Kilometers to miles.
5. Write a Java program to detect whether a number entered by the user is an integer or not.
package com.company;
public class ch1practiceset {
public static void main(String[] args) {
int a=10;
int b=15;
int c=5;
int sum=a+b+c;
System.out.println(sum);
}
}
Output :
30
2. Write a program to calculate CGPA using marks of three subjects (out of 100)
package com.company;
public class ch1practiceset {
public static void main(String[] args) {
float subject1=45;
float subject2=95;
float subject3=48;
float cgpa=(subject1+subject2+subject3)/30;
System.out.println(cgpa);
}
}
Output :
6.266667
3. Write a Java program that asks the user to enter his/her name and greets them with “Hello {name}, have a good day” text.
package com.company;
import java.util.Scanner;
public class ch1practiceset {
public static void main(String[] args) {
System.out.println("What is Your name ?");
Scanner sc=new Scanner(System.in);
String name=sc.next();
System.out.println("Hello "+name+",have a good day");
}
}
/*
Output :
What is Your name ?
Aryadeep
Hello Aryadeep,have a good day */
4. Write a Java program to convert Kilometers to miles.
package com.company;
import java.util.Scanner;
public class ch1practiceset {
public static void main(String[] args) {
System.out.println("Kilometer to Miles Conversion");
Scanner sc=new Scanner(System.in);
System.out.println("Enter in Kilometers :");
double a=sc.nextDouble();
double convert=a*0.621371;
System.out.println("Answer in Miles is : "+convert);
}
}
/*
Output :
Kilometer to Miles Conversion
Enter in Kilometers :
5
Answer in Miles is : 3.106855
*/
5. Write a Java program to detect whether a number entered by the user is an integer or not.
package com.company;
import java.util.Scanner;
public class ch1practiceset {
public static void main(String[] args) {
System.out.println("Enter the Number :");
Scanner sc=new Scanner(System.in);
System.out.println(sc.hasNextInt());
}
}
/*
Output :
Enter the Number :
45
true
Enter the Number :
ffggmm
false
*/