Public Class Public Static Void Int Float Float: Args S N A S N A A S N A

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 13

14 + 5 - 2 = 19 - 2 = 17

14 - 5 + 2 = 9 + 2 = 11

14*2/5=28/5=5

public class Main {


public static void main(String[] args) {
int s = 55, n=10;
float a=s/n;
System.out.println(a);
a=(float)s/n;
System.out.println(a);
}
}

BASIC CONSTRUCTS OF ANY PROGRAM


————————————————————————-
1. Sequence: The order of execution of the statements.
2. Selection/Branching: Choosing one path of execution from
the alternatives(one/two/more)
3.

SELECTION
—————————
Problem statement : Check whether a given integer (n) is
divisible by another given integer (d) or not.

Relational operators: >, <, >=,<=, ==, !=

a>b, a==b, a>=b etc.


import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
int n=sc.nextInt();
System.out.print("Enter the divisor: ");
int d=sc.nextInt();
int r=n%d;
if(r==0)
System.out.println(n+" is disvisible by "+d);
else
System.out.println(n+" is NOT disvisible by
"+d);
}
}

N.B: if(r=0)————— wrong

Problem Statement: Compare two given integers.

Complement
————————————————————————
a>b a <= b
a<b a >= b
a == b a != b

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
if(a>b)
System.out.println(a+" is greater than "+b);
else if(a<b)
System.out.println(a+" is less than "+b);
else
System.out.println(a+" is equal to "+b);
}
}

Problem Statement: Print the larger number from two given


integers.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
if(a==b)
System.out.println("Both are equal");
else if(a>b)
System.out.println("Larger number is "+a);
else
System.out.println("Larger number is "+b);
sc.close();
}
}

OR

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
if(a==b)
System.out.println("Both are equal");
else {
if(a>b)
System.out.println("Larger number is
"+a);
if(a<b)
System.out.println("Larger number is
"+b);
}
sc.close();
}
}

OR

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
int m;
if(a==b)
System.out.println("Both are equal");
else {
m=a;
if(b>m)
m=b;
System.out.println("Larger number is "+m);
}
sc.close();
}
}

OR

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
int m;
if(a==b) {
System.out.println("Both are equal");
System.exit(0);
}
m=a;
if(b>m)
m=b;
System.out.println("Larger number is "+m);

sc.close();
}
}

Branching: One-way or two-way or multi-way

ONE-WAY:
—————————
if(…..){
——————
      ——————
}

TWO-WAY
——————————
if(…..){
——————
      ——————
}
else {
——————
      ——————
}

MULTI-WAY
————————

if(…..){
——————
      ——————
}
else if(……){
——————
      ——————
}

else {
—————
—————
}

Which one should be used?

if(a==b)
System.out.println("Both are equal");
else if(a>b)
System.out.println("Larger number is "+a);
else
System.out.println("Larger number is "+b);

OR

if(a==b)
System.out.println("Both are equal");
if(a>b)
System.out.println("Larger number is "+a);
if(a<b)
System.out.println("Larger number is "+b);

Problem statement: Print the larger number among 3 given


numbers

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
System.out.print("Enter the third number: ");
int c=sc.nextInt();
int m;
if(a>b)
if(a>c)
m=a;
else
m=c;
else if(b>c)
m=b;
else
m=c;
System.out.println("Larger number is "+m);
sc.close();
}
}

OR
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
System.out.print("Enter the third number: ");
int c=sc.nextInt();
int m;
m=a;
if(b>m)
m=b;
if(c>m)
m=c;
System.out.println("Larger number is "+m);
sc.close();
}
}

Logical Operators: &&, ||, !

a==b==c ??

a == b && b == c

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
System.out.print("Enter the third number: ");
int c=sc.nextInt();
if(a==b && b==c)
System.out.println("All are equal");
else {
int m;
m=a;
if(b>m)
m=b;
if(c>m)
m=c;
System.out.println("Larger number is "+m);
}
sc.close();
}
}

Problem Statement: Take the lengths of 3 sides of a triangle as


input. Check whether a triangle can be drawn or not.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the length of the 1st side:
");
int a=sc.nextInt();
System.out.print("Enter the length of the 2nd side:
");
int b=sc.nextInt();
System.out.print("Enter the length of the 3rd side:
");
int c=sc.nextInt();
if(a+b>c && b+c>a && c+a>b)
System.out.println("Triangle can be drawn");
else
System.out.println("Triangle can NOT be
drawn");
sc.close();
}
}

OR

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the length of the 1st side:
");
int a=sc.nextInt();
System.out.print("Enter the length of the 2nd side:
");
int b=sc.nextInt();
System.out.print("Enter the length of the 3rd side:
");
int c=sc.nextInt();
if(!(a+b>c && b+c>a && c+a>b))
System.out.println("Triangle can NOT be
drawn");
else
System.out.println("Triangle can be drawn");
sc.close();
}
}

De Morgan’s Law:
!(a+b>c && b+c>a && c+a>b) -> (a+b<=c || b+c<=a ||
c+a<=b)

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the length of the 1st side:
");
int a=sc.nextInt();
System.out.print("Enter the length of the 2nd side:
");
int b=sc.nextInt();
System.out.print("Enter the length of the 3rd side:
");
int c=sc.nextInt();
if(a+b<=c || b+c<=a || c+a<=b)
System.out.println("Triangle can NOT be
drawn");
else
System.out.println("Triangle can be drawn");
sc.close();
}
}

You might also like