You are on page 1of 14

Write a Program to find Even and Odd Number?

package com.practice.revision;

import java.util.Scanner;

public class EvenandOdd {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
int oddoreven = scanner.nextInt();

if(oddoreven%2==0) System.out.println(oddoreven+" is even");


else System.out.println(oddoreven+" is odd");

}
}

Output: Output:
Enter a number: Enter a number:
6 11
6 is even 11 is odd

Write a program to find Even and Odd without using % operator?


package com.practice.revision; Output:
import java.util.Scanner; Enter a number:
5
public class EvenandOdd1 { 5 is odd

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in); Output:
System.out.println("Enter a number:");
int oddeven = scanner.nextInt(); Enter a number:
26
if((oddeven&1)==0) System.out.println(oddeven+" is even "); 26 is even
else System.out.println(oddeven+" is odd");
}
}

Explainatin on(if((oddeven&1)==0))
For Odd Number: 13 binary numbers  0........0001101
&(operator)
And 1 binary numbers---0........0000001
0......0000001

For Odd Number: 12 binary numbers  0........0001100


&(operator)
And 1 binary numbers---0........0000001
0......0000000
Write a program to add only Even number up to nth place?

package com.practice.revision;

import java.util.Scanner; Output:

public class AddOnlyEven { Enter the Number:


public static void main(String[] args) { 6
Scanner scanner = new Scanner(System.in); Addition of Even Number till 6: 12
System.out.println("Enter the Number:");
int number =scanner.nextInt();
int addeven =0;
for(int i=1;i<=number;i++) {
if(i%2==0) addeven =addeven+i;
}
System.out.println("Addition of Even Number till "+number+": "+addeven);
}
}
Write a program to find Biggest Number?
package com.practice.revision; Output:
import java.util.Scanner; Enter the two Number:
12
public class BiggestNumber { 26
public static void main(String[] args) { 26 is biggest Number
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the two Number:");
int number1 = scanner.nextInt();
int number2 = scanner.nextInt();

if(number1>number2) System.out.println(number1+" is biggest Number");


else if(number2>number1) System.out.println(number2+" is biggest Number");
else System.out.println(number1+" and "+ number2 +" both are equal");
}

Write a program to find Leap year or not?

package com.practice.revision;

import java.util.Scanner;

public class LeapYear {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Year:");
int year = scanner.nextInt();

if(year%400==0||(year%4==0)&&(year%100!=0)) System.out.println(year+" is leap year");


else System.out.println(year+" is not leap year");
}
}
Output: Output:
Enter the Year: Enter the Year:
2013 2008
2013 is not leap year 2008 is leap year
Write a program to currency required to despatch?
Output:
package com.practice.revision;
Enter the Curreny:
import java.util.Scanner; 8900
2000 * 4 = 8000
public class CurrencyDespatch { 500 * 1 = 500
public static void main(String[] args) { 200 * 2 = 400
Scanner scanner = new Scanner(System.in); Remaining Money cannot given:₹0
System.out.println("Enter the Curreny:");
int money = scanner.nextInt();
if(money>=2000) {
System.out.println("2000 * "+money/2000+" = "+2000*(money/2000));
money=money%2000;
}
if(money>=500) {
System.out.println("500 * "+money/500+" = "+500*(money/500));
money=money%500;
}
if(money>=200) {
System.out.println("200 * "+money/200+" = "+200*(money/200));
money=money%200;
}
if(money>=100) {
System.out.println("100 * "+money/100+" = "+100*(money/100));
money=money%100;
}
if(money<100) {
System.out.println("Remaining Money cannot given:₹"+money);
}
}
}
Write a program to generate electricity bill :
1. First 50 units are free.
2. For next 50 units rs 3 per unit.
3. For next 50 units rs 5 per unit.
4. For next 50 units rs 8 per unit.
5. After 200 units rs 13 per unit.
If last month bill is not paid, add 10% interest for last month Amount.
If last month bill is paid , give 12% discount for the present Month.
package com.practice.revision;

import java.util.Scanner; Output:

public class ElectricityBillCalculation { Enter Unit:


100
public static void main(String[] args) { Enter bill pending amount:
Scanner scanner = new Scanner(System.in); 0
System.out.println("Enter Unit:"); Amount to be pay = ₹ 132.0

int unit = scanner.nextInt();

System.out.println("Enter bill pending amount: ");


double pending =scanner.nextDouble();
double amount =0;
if(unit<=50) { Output:
amount=0;
} Enter Unit:
else if(unit<=100) { 100
//unit=unit-50; Enter bill pending amount:
amount=(unit-50)*3; 100
} Amount to be pay = ₹ 260.0
else if(unit<=150) {
//unit=unit-100;
amount=50*0+50*3+(unit-100)*5;
}
else if(unit<=200) {
//unit=unit-150;
amount=50*0+50*3+50*5+(unit-150)*8;
}
else {
//unit =unit-200;
amount=50*0+50*3+50*5+50*8+(unit-200)*13;
}

if(pending!=0)
amount=amount+pending+(pending*(10.0/100.0));
else
amount=amount-(amount*(12.0/100.0));

System.out.println("Amount to be pay = ₹ "+amount);

}
}
Write a program to find addition of all natural number?
package com.practice.revision;
Output:
import java.util.Scanner;
Enter the Natural Number:
public class AdditionofNaturalNumbr { 6
public static void main(String[] args) { Addition from 1 to 6: 21
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Natural Number:");
int naturalno = scanner.nextInt();
int addition = 0;
for(int i =1;i<=naturalno;i++) {
addition = addition+i;
}
System.out.println("Addition from 1 to "+naturalno+": "+addition);
}
}

Write a program to find the factorial of the Number?


Output:
package com.practice.revision;
Enter the Factorial Number:
import java.util.Scanner; 4
Factorial of 4: 24
public class FactorialOfNumber {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Factorial Number:");
int factorial = scanner.nextInt();
int factorialMulitiple = 1;

for (int i = 1; i <= factorial; i++) {


factorialMulitiple = factorialMulitiple * i;
}
System.out.println("Factorial of "+factorial+": "+factorialMulitiple);
}
}
Write a program to print Fibonacci Series of given Range?

package com.practice.revision; Output:


//Range
import java.util.Scanner; Enter the Range for fibonacci
Series:
public class FibonacciSeries { 10
public static void main(String[] args) { 0
Scanner scanner = new Scanner(System.in); 1
System.out.println("Enter the Range for fibonacci Series:"); 1
int range =scanner.nextInt(); 2
int firstno = 0; 3
int secondno = 1; 5
int thirdno = 1; 8
for(int i=1;i<=range;i++) { 13
System.out.println(firstno); 21
firstno =secondno; 34
secondno =thirdno;
thirdno =firstno+secondno;

}
}
}
Write a program to print Fibonacci Series of given Upper Limit?

package com.practice.revision;
Output:

Enter the UpperLimit:


import java.util.Scanner;
84
0
//Upper limit
1
public class FibonacciSeries2 {
1
public static void main(String[] args) {
2
Scanner scanner = new Scanner(System.in);
3
System.out.println("Enter the UpperLimit:");
5
int upperlimit = scanner.nextInt();
8
int firstno = 0;
13
int secondno = 1;
21
int thirdno = 1;
34
55
while (firstno <= upperlimit) {
System.out.println(firstno);
firstno = secondno;
secondno = thirdno;
thirdno = firstno + secondno;
}

}
}

Write a program to print Fibonacci Series of given UpperLimit and LowerLimit?

package com.practice.revision;

import java.util.Scanner;
//lower limit and upper limit
public class FibonacciSeries3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the LowerLimit: \r\nEnter the UpperLimit: ");
int lowerlimit = scanner.nextInt();
int upperlimit = scanner.nextInt();
int firstno = 0;
int seconddno = 1;
int thirdno = 1;
for (; firstno <= upperlimit;) { Output:
if (firstno >= lowerlimit) {
System.out.println(firstno); Enter the LowerLimit:
} Enter the UpperLimit:
firstno = seconddno; 20
100
seconddno = thirdno;
thirdno = firstno + seconddno; 21
34
}
} 55
89
}
Write a program to print Fibonacci Series value of given nth place?

package com.practice.revision;

import java.util.Scanner; Output:

public class FibonacciSeries4 { Enter the nth place:


public static void main(String[] args) { 10
Scanner scanner = new Scanner(System.in); 34
System.out.println("Enter the nth place :");
int nthplace = scanner.nextInt();
int firstno = 0;
int secondno = 1;
int thirdno = 1;
for (int i = 1; i <= nthplace; i++) {
if (i == nthplace) {
System.out.println(firstno);
}
firstno = secondno;
secondno = thirdno;
thirdno = firstno + secondno;

}
}

}
Write a program to find number is prime number or not?
Output:
package com.practice.revision;
Enter a number:
import java.util.Scanner; 8
8 is not a Prime Number
public class PrimeNumber { Output:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Enter a number:
System.out.println("Enter a number:");
int isprimeno = scanner.nextInt();
7
if(isprimeno<=1) { 7 is a Prime Number
System.out.println(isprimeno+" not a Prime Number");
return;
}
for(int i =2;i<=isprimeno;i++) {
if(isprimeno%i==0) {
if(isprimeno==i) {
System.out.println(isprimeno+" is a Prime Number");
break;
}
else {
System.out.println(isprimeno+" is not a Prime Number");
break;
}
}
}
}
}
Write a program to find number is prime number or not? Output:
package com.practice.revision;
Enter a Number:
12
import java.util.Scanner;
12 is not a Prime Number
public class PrimeNumber5 {

public static void main(String[] args) {


Output:
Scanner scanner = new Scanner(System.in);
Enter a Number:
System.out.println("Enter a Number: ");
7
int isprime = scanner.nextInt();
7 is a Prime Number
int count=0;
for(int i=1;i<=isprime;i++) {
if(isprime%i==0) count++;
}
if(count==2) {
System.out.println(isprime+" is a Prime Number");
}
else {
System.out.println(isprime+" is not a Prime Number");
}
}
}
Write a program to find number is prime number or not?

If n = 10
1 2 3 4 5 6 7 8 9 10

Output:
This half not requires iterating
Enter a Number:
7
package com.practice.revision; 7 is a Prime Number

import java.util.Scanner;
// this program reduces iteration Output:
public class PrimeNumber2 { Enter a Number:
public static void main(String[] args) { 8
Scanner scanner = new Scanner(System.in); 8 is not a Prime Number
System.out.println("Enter a Number:");
int isprimeno = scanner.nextInt();
if(isprimeno<=1){
System.out.println(isprimeno+" is not a Prime Number");
return;
}
for(int i =2;i<=(isprimeno/2);i++) {
if(isprimeno%i==0) {
System.out.println(isprimeno+" is not a Prime Number");
return;
}
}
System.out.println(isprimeno+" is a Prime Number");
}
}
Write a program to print prime number or not at given upperrange and lowerrange?

package com.practice.revision;

import java.util.Scanner;
Enter lower Range:
public class PrimeNumber3 { Enter the Upper
public static void main(String[] args) { Range:
Scanner scanner = new Scanner(System.in); 10
System.out.println("Enter lower Range: \r\n" + "Enter the Upper Range:"); 50
int lowerrange = scanner.nextInt(); 11
int upperrange = scanner.nextInt(); 13
outer: for (int i = lowerrange; i <= upperrange; i++) { 17
if (i <= 1) 19
continue; 23
for (int j = 2; j <= (i / 2); j++) { 29
if (i % j == 0) 31
continue outer; 37
} 41
System.out.println(i); 43
} 47
}
}
Write a program to print prime number or not at given upperrange and lowerrange in Reverse way?

package com.practice.revision;

import java.util.Scanner;

public class PrimeNumber4 { Output:


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); Enter Lower Range:
System.out.println("Enter Lower Range: \r\nEnter Upper Range: "); Enter Upper Range:
int lowerrange = scanner.nextInt(); 10
int upperrange = scanner.nextInt(); 50
47
Outerloop: for (int i = upperrange; i >= lowerrange; i--) { 43
if (i <= 1) 41
continue; 37
for (int j = 2; j <= (i / 2); j++) { 31
if (i % j == 0) 29
continue Outerloop; 23
} 19
System.out.println(i); 17
} 13
} 11
}
Write a program to find LCM of two numbers?

package com.practice.revision; Output:

import java.util.Scanner; Enter Two Number:


5
public class LCMofTwoNumber { 7
public static void main(String[] args) { LCM of 5 and 7 : 35
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Two Number:");
int firstno = scanner.nextInt();
int secondno = scanner.nextInt();
int lcm = 1;
while (true) {
if (lcm % firstno == 0 && lcm % secondno == 0)
break;
lcm++;
}
System.out.println("LCM of "+firstno+" and "+secondno+" : "+lcm);
}
}
Write a program to find LCM of two numbers?

package com.practice.revision; Output:

Enter Two Number:


import java.util.Scanner;
3
4
public class LCMofTwoNumber2 {
LCM of 3 and 4 : 12
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Two Number:");
int firstno = scanner.nextInt();
int secondno = scanner.nextInt();
int lcm = firstno > secondno ? firstno : secondno;
while (true) {
if (lcm % firstno == 0 && lcm % secondno == 0)
break;
lcm++;
}
System.out.println("LCM of " + firstno + " and " + secondno + " : " + lcm);
}
}

You might also like