java project
java project
import java.util.Scanner;
public class CWH_chapter_4 {
public static void main(String[] args) {
2 Else if case
import java.util.Scanner;
public class CWH_chapter_4 {
public static void main(String[] args) {
System.out.println("Enter you age :");
Scanner s = new Scanner(System.in);
int a;
a = s.nextInt();
if (a > 20) {
System.out.println("yess ");
} else if (a > 35) {
System.out.println("ok");
} else if (a > 15) {
System.out.println("no ");
} else if (a > 50) {
System.out.println("happy happy ");
} else {
System.out.println("super happy");
}
}
}
Output
3 Switch case
import java.util.Scanner;
public class CWH_chapter_4 {
public static void main(String[] args) {
Output
4 Making calculator
import java.util.Scanner;
public class CWH_chapter_4 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Entr the first number: ");
int nu1 = s.nextInt();
switch (a) {
case "add":
System.out.println("the result is = " + (nu1 + nu2));
break;
case "multiply":
System.out.println(" the result is = " + (nu1 * nu2));
break;
case "subtract":
System.out.println("the result is = " + (nu1 - nu2));
break;
case "divide":
System.out.println(" the result is = " + (nu1 / nu2));
break;
default:
System.out.println("error");
}
}
}
Output
4 While loop
public class CWH_chapter_5 {
public static void main(String[] args) {
// while loop
int a =1;
while (a <= 10)
{
System.out.println(a);
a++;
}
Output
5 for loop
public class CWH_chapter_5 {
public static void main(String[] args) {
int a = 7;
int i;
for (i = 0; i < a; i++) {
System.out.println(2 * i);
}
}
}
Output
6 Break condition
public class CWH_chapter_5 {
public static void main(String[] args) {
int a = 10, i;
for (i = 0; i <= a; i++) {
System.out.println(i);
if (i == 4) {
System.out.println("break for the work");
break;
}
}
}
}
Output
7 Continue condition
public class CWH_chapter_5 {
public static void main(String[] args) {
int a=5,i;
for (i=0; i<=a; i++)
{
if (i==4)
{
System.out.println("java is continue");
continue;
}
System.out.println(i);
System.out.println("java is going on");
Output