If Else in Java Code: Class

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

class HelloWorld

{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
---------------------------------------------------------
/ If else in Java code
import java.util.Scanner;

class IfElse {
public static void main(String[] args) {
int marksObtained, passingMarks;

passingMarks = 40;

Scanner input = new Scanner(System.in);

System.out.println("Input marks scored by you");

marksObtained = input.nextInt();

if (marksObtained >= passingMarks) {


System.out.println("You passed the exam.");
}
else {
System.out.println("Unfortunately, you failed to pass the exam.");
}
}
}
--------------------------------------------------------

//Java for loop program


class ForLoop {
public static void main(String[] args) {
int c;

for (c = 1; c <= 10; c++) {


System.out.println(c);
}
}
}

import java.util.Scanner;

class WhileLoop {
public static void main(String[] args) {
int n;

Scanner input = new Scanner(System.in);


System.out.println("Input an integer");

while ((n = input.nextInt()) != 0) {


System.out.println("You entered " + n);
System.out.println("Input an integer");
}
System.out.println("Out of loop");
}
}

class Alphabets
{
public static void main(String args[])
{
char ch;

for( ch = 'a' ; ch <= 'z' ; ch++ )


System.out.println(ch);
}
}

Printing alphabets using a while loop (Only the body of the


main method is shown):

char c = 'a';

while (c <= 'z') {


System.out.println(c);
c++;
}

Using a do while loop:

char c = 'A';
do {
System.out.println(c);
c++;
} while (c <= 'Z');

import java.util.Scanner;

class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;

Scanner in = new Scanner(System.in);

System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);

System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);

System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string "+s);
}
}

import java.util.Scanner;

class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");

for ( c = 1 ; c <= 10 ; c++ )


System.out.println(n+"*"+c+" = "+(n*c));
}
}

import java.util.Scanner;

class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();

if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
}
}

Swapping using temporary or third variable

import java.util.Scanner;

class SwapNumbers
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);

x = in.nextInt();
y = in.nextInt();

System.out.println("Before Swapping\nx = "+x+"\ny = "+y);

temp = x;
x = y;
y = temp;

System.out.println("After Swapping\nx = "+x+"\ny = "+y);


}
}

Swapping without temporary variable

import java.util.Scanner;

class SwapNumbers
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);

x = in.nextInt();
y = in.nextInt();

System.out.println("Before Swapping\nx = "+x+"\ny = "+y);

x = x + y;
y = x - y;
x = x - y;
System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}
}

import java.util.Scanner;

class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;

System.out.println("Enter an integer to calculate it's factorial");


Scanner in = new Scanner(System.in);

n = in.nextInt();

if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1; c <= n; c++)
fact = fact*c;

System.out.println("Factorial of "+n+" is = "+fact);


}
}
}

import java.util.Scanner;

class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, remainder, digits = 0;

Scanner in = new Scanner(System.in);


System.out.println("Input a number to check if it is an Armstrong number");
n = in.nextInt();

temp = n;

// Count number of digits

while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;

while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}

if (n == sum)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " isn't an Armstrong number.");
}

static int power(int n, int r) {


int c, p = 1;

for (c = 1; c <= r; c++)


p = p*n;

return p;
}
}

import java.util.*;

class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to reverse");


original = in.nextLine();

int length = original.length();

for ( int i = length - 1 ; i >= 0 ; i-- )


reverse = reverse + original.charAt(i);

System.out.println("Reverse of entered string is: "+reverse);


}
}

import java.util.*;

class Palindrome
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome");


original = in.nextLine();

int length = original.length();

for ( int i = length - 1; i >= 0; i-- )


reverse = reverse + original.charAt(i);

if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string isn't a palindrome.");

}
}

import java.util.Scanner;

class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;

System.out.println("Enter a number to reverse");


Scanner in = new Scanner(System.in);
n = in.nextInt();

while(n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

System.out.println("Reverse of the number is " + reverse);


}
}

To find Strong Number


import java.util.scanner;

public class strongnumber

public static void

main(string args[])

strongnumber ss=new

strongnumber();

int a,b,r,s=0;

scanner sl=new scanner(system.in);

system.out.println(“Enter a number”);

b=sl.nextInt();
a=b;

while(b>0)

r=b%10;

s=s+ss.fact(r);

b=b/10;

if(a==s)

system.out.println(a+” is a stong number);

else

system.out.println(a+”is not a strong number);

int fact(int i)

int f,j;

f=1;

for(j=i;j>0;j--)

f=f*j;

return f;

GCD of Two Numbers:

import java.util.P;

public class gcdoftwonumbers

{
public static void

main(string args[])

int a,b,gcd;

a=b=gcd=0;

scanner s=new scanner(system.in);

system.out.println(“enter two num);

a=s.nextInt();

b=s.next.Int();

while(a!=0)

gcd=a;

a=b%a;

b=gcd;

system.out.println(“GCD”+gcd);

You might also like