KEMBAR78
If and Else | PDF | Computer Engineering | Computer Science
0% found this document useful (0 votes)
14 views8 pages

If and Else

The document contains various Java code snippets demonstrating basic programming concepts such as leap year calculation, division, conditional statements, and user input handling. Each snippet includes conditional logic to determine outcomes based on specific criteria, such as checking for even or odd numbers, identifying months, and validating passwords. The examples illustrate fundamental programming constructs like if-else statements, loops, and user interaction through the console.

Uploaded by

BlazeSpace YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

If and Else

The document contains various Java code snippets demonstrating basic programming concepts such as leap year calculation, division, conditional statements, and user input handling. Each snippet includes conditional logic to determine outcomes based on specific criteria, such as checking for even or odd numbers, identifying months, and validating passwords. The examples illustrate fundamental programming constructs like if-else statements, loops, and user interaction through the console.

Uploaded by

BlazeSpace YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Leap Year

int a,b;
a=2020;
b= a%4;
if (b==0.000000)
{
System.out.println("Its an Leap year");
}
else
{
System.out.println("Its not an Leap year");
}
}

Division
import java.util.Scanner;
public class CalculationDivision {

public static void main(String[] args) {


Scanner scan = new Scanner (System.in);
System.out.println("Input First Integer");
float a = scan.nextInt();
System.out.println("Input Second Integer");
float b = scan.nextInt();

if (b==0)
{
System.out.println("This Division is not possible");
}
else
{
System.out.printf("Quotient of both the numbers after dividing is = %f\n",a/b);
}
}

Basic
int x;
x=10;
if (x==10)
{
System.out.println("Value is equal to 10\n");
}
else
{
System.out.println("Value is not equal to 10");
}
}

Password cracker
public static void main(String[] args) {
int a;

a=1234;

if (a==1234)
{
System.out.println("Correct Password");
}
else
{
System.out.println("Incorrect Password");
}
}

What is Greatest
int a,b;

a=1;
b=29;
if (a>b)
{
System.out.println("A is Greater than B");
}
else
{
System.out.println("A is Smaller than B");
}
}

Odd or even

public class OddOrEven {

public static void main(String[] args) {


float a,b;
a=12;
b=a%2;

if (b==0.00000)
{
System.out.println("Its an Even number");
}
else
{
System.out.println("Its and Odd number");
}
}

If, Else if, Else

Month Finder
import java.util.Scanner;
public class MonthFinder {

public static void main(String[] args) {


Scanner scan = new Scanner (System.in);
System.out.println("Input a number between 1 to 12 to get the month name");
int a;
a = scan.nextInt();
if (a==1)
{
System.out.println("Month name- January");
}
else if (a==2)
{
System.out.println("Month name- February");
}
else if (a==3)
{
System.out.println("Month name- March");
}
else if (a==4)
{
System.out.println("Month name- April");
}
else if (a==5)
{
System.out.println("Month name- May");
}
else if (a==6)
{
System.out.println("Month name- June");
}
else if (a==7)
{
System.out.println("Month name- July");
}
else if (a==8)
{
System.out.println("Month name- August");
}
else if (a==9)
{
System.out.println("Month name- September");
}
else if (a==10)
{
System.out.println("Month name- October");
}
else if (a==11)
{
System.out.println("Month name- November");
}
else if (a==12)
{
System.out.println("Month name- December");
}
else
{
System.out.println("Invalid");
}
}

Positive or Negative

public class Positive {

public static void main(String[] args) {

int a;

a=0;

if (a>0)
{
System.out.println("Its a positive Integer");
}
else if (a==0)
{
System.out.println("Its 0");
}
else
{
System.out.println("Its a Negative Integer");
}
}

Color Choose
x='G';

if (x=='r')
{
System.out.println("Your color is red");
}
else if (x=='b')
{
System.out.println("Your color is Blue");
}
else if (x=='g')
{
System.out.println("Your color is Green");
}
else
{
System.out.println("Invalid color");
}
}

Greatest Between 5

public class GreatestBetween5 {

public static void main(String[] args) {


int a,b,c,d,e;
a= 31000000;
b=200000;
c=-100;
d=50000;
e=1000000000;

if (a>b && a>c && a>d && a>e)


{
System.out.println("A is greater");
}
else if (b>a && b>c && b>d && b>e)
{
System.out.println("B is Greater");
}
else if (c>a && c>b && c>d && c>e)
{
System.out.println("C is Greater");
}
else if (d>a && d>b && d>c && d>e)
{
System.out.println("D is Greater");
}
else
{
System.out.println("E is Greater");
}
}

If, Elseif, Else Basics

public class MultipleCondiotions {

public static void main(String[] args) {


int x;
x=100;
if (x==10)
{
System.out.println("Value is equal to 10\n");
}
else if(x==20)
{
System.out.println("Value is equal to 20");
}
else if(x==30)
{
System.out.println("Value is equal to 30");
}
else
{
System.out.println("Invalid");
}
}

Hour, Minute, Second

int seconds = 86400;

int s = seconds % 60;


int h = seconds / 3600;
int m = (seconds / 60)%60;

if (h<12)
{
System.out.println(h + ":" + m + ":" + s + "am");
}
else if (h==12)
{
System.out.println(h + ":" + m + ":" + s + " pm");
}
else if (h==24)
{
h=h-24;
System.out.println(h + ":" + m + ":" + s + " am");
System.out.println("A day is Over");
}
else if (seconds==seconds*2)
{
h=h-h;
System.out.println();
}
/*else if(seconds==86400*30)
{
System.out.println(h + ":" + m + ":" + s + " am");
System.out.println("A Month is Over");
}
else if(seconds==86400*365)
{
System.out.println(h + ":" + m + ":" + s + " am");
System.out.println("A Month is Over");
}
else if(seconds==86400*7)
{
System.out.println(h + ":" + m + ":" + s + " am");
System.out.println("A Month is Over");
}*/
else
{
h=h-12;
System.out.println(h + ":" + m + ":" + s + " pm");
}
}
}

Vowel or consonant
char z;

z='a';

if(z=='a')
{
System.out.println("Its an Vowel");
}
else if(z=='e')
{
System.out.println("Its an Vowel");
}
else if(z=='i')
{
System.out.println("Its an Vowel");
}
else if(z=='o')
{
System.out.println("Its an Vowel");
}
else if(z=='u')
{
System.out.println("Its an Vowel");
}
else
{
System.out.println("Its an Consonant");
}
}

You might also like