Java Programs
If-else Example
import java.util.Scanner;
public class Grading {
public static void main(String[] args) {
// Getting input
System.out.print("Enter your mark : ");
Scanner sc = new Scanner(System.in);
int mark = sc.nextInt();
char grade;
if(mark>=90){
grade = 'A';
}else if(mark>=80){
grade = 'B';
}else if(mark>=70){
grade = 'C';
}else if(mark>=60){
grade = 'D';
}else if(mark>=50){
grade = 'E';
}else{
grade = 'F';
}
System.out.println("Your grade is "+grade);
}
}
In Simple company, transport of company is based on weight and kilometres. Get the weight and kilometres and calculate cost as Rs.10 per kg for each kilometer. Give discount of 10% for cost greater than 1000, 5% for cost greater than 500
import java.util.Scanner;
class HelloWorld {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float weight; float kms;
        float cost; float discount = 0;
        System.out.print("Enter the total weight : ");
        weight = sc.nextFloat();
        System.out.print("Enter the total distance : ");
        kms = sc.nextFloat();
        cost = 10 * weight * kms;
        if(cost> 1000){
            discount = cost * 0.10f;
            cost -= discount;
        }else if(cost > 500){
              discount = cost * 0.05f;
            cost -= discount;
        }
        System.out.println("Original Price : "+(cost+discount));
        System.out.println("Discount : "+discount);
        System.out.println("Cost : "+cost);
    }
}
Simple User info Printing Screen
import java.util.Scanner;
public class userinfo {
   public static void main(String[] args) {
    String name;
    Long mobileNo;
    Character initial;
    Float cgpa;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter your name without initial : ");
    name = sc.nextLine();
    System.out.println("Enter your Initial(one word) : ");
    initial = sc.nextLine().charAt(0);
    System.out.println("Enter your mobile number : ");
    mobileNo = sc.nextLong();
    System.out.println("Enter yout cgpa : ");
    cgpa = sc.nextFloat();
    System.out.println("------------------------------------");
    System.out.println("\tYour Details");
    System.out.println("------------------------------------");
    System.out.println("Your name without initial : "+name);
    System.out.println("Your Initial(one word) : "+initial);
    System.out.println("Your mobile number : "+mobileNo);
    System.out.println("Yout cgpa : "+cgpa);
   }
}
Simple Switch Case Example
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.Scanner;
class HelloWorld {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float num1, num2, ans;
        System.out.println("Enter the number 1");
        num1 = sc.nextInt();
        System.out.println("Enter the number 2");
        num2 = sc.nextInt();
        System.out.println("1.Division \n2.Remainder");
        System.out.println("Enter your option : ");
        int option = sc.nextInt();
        switch(option){
            case 1:
                ans = num1/num2;
                System.out.println(num1+" divided by"+num2+" = "+ans);
                break;
            case 2:
                ans = num1%num2;
                System.out.println("Remainder of "+num1+" divided by "+num2+" = "+ans);
                break;
            default:
                System.out.println("Not a valid choice");
        }
    }
}
Loops in java
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class HelloWorld {
    public static void main(String[] args) {
     int i = 1;
    //  while loop
     while(i<10){
         System.out.println(i);
         i++;
     }
     i = 10;
    //  do while loop
    do {
        System.out.println(i);
         i++;
    }while(i<10);
    // for loop
    for (i = 1; i<10; i+=2){
        System.out.println(i);
    }
    }
}
Find sum of all the numbers divisible by 3 between 1 to 30
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class HelloWorld {
    public static void main(String[] args) {
    int sum = 0;
    for (int i = 3; i<=30; i+=3){
        sum+=i;
    }
    System.out.println("The Sum of all numbers divisible by 3 between 1-30 is "+sum);
    }
}
class HelloWorld {
    public static void main(String[] args) {
    int sum = 0;
    int i = 1;
    while (i<=30)
    {
        if (i%3==0){
            sum = sum+i;
        }
        i++;
    }
    System.out.println("The Sum of all numbers divisible by 3 between 1-30 is "+sum);
    }
}
Comments
Post a Comment