0% found this document useful (0 votes)
13 views

javapgms

simple java programs

Uploaded by

Balajanani .R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

javapgms

simple java programs

Uploaded by

Balajanani .R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

//even numbers between two limits

import java.util.Scanner;

public class EvenNumbers {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter the lower limit: ");


int lowerLimit = scanner.nextInt();

System.out.println("Enter the upper limit: ");


int upperLimit = scanner.nextInt();

System.out.println("Even numbers between " + lowerLimit + " and " + upperLimit + ":");

for (int i = lowerLimit; i <= upperLimit; i++) {


if (i % 2 == 0) {
System.out.println(i);
}
}

scanner.close();
}
}

//odd numbers between two limits

import java.util.Scanner;

public class EvenNumbers {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the lower limit: ");

int lowerLimit = scanner.nextInt();

System.out.println("Enter the upper limit: ");

int upperLimit = scanner.nextInt();

System.out.println("Odd numbers between " + lowerLimit + " and " + upperLimit + ":");

for (int i = lowerLimit; i <= upperLimit; i++) {

if (i % 2 != 0) {

System.out.println(i);

scanner.close();

}
Prime or not

public class Main {

public static void main(String[] args) {

int num = 29;


boolean flag = false;

// 0 and 1 are not prime numbers


if (num == 0 || num == 1) {
flag = true;
}

for (int i = 2; i <= num / 2; ++i) {

// condition for nonprime number


if (num % i == 0) {
flag = true;
break;
}
}

if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}

//numbers which is divisible by 3 between two limits

import java.util.Scanner;

public class divbythree {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter the lower limit: ");


int lowerLimit = scanner.nextInt();

System.out.println("Enter the upper limit: ");


int upperLimit = scanner.nextInt();

System.out.println("Even numbers between " + lowerLimit + " and " + upperLimit + ":");

for (int i = lowerLimit; i <= upperLimit; i++) {


if (i % 3 == 0) {
System.out.println(i);
}
}

scanner.close();
}
}

// sum of n numbers using while loop

public class SumNatural {

public static void main(String[] args) {

int num = 50, i = 1, sum = 0;

while(i <= num)


{
sum += i;
i++;
}

System.out.println("Sum = " + sum);


}
}

//sum of even numbers

public class SumofEven5 {

private static Scanner sc;

public static void main(String[] args)

{ int number, i = 2, evenSum = 0;

sc = new Scanner(System.in);

System.out.print(" Please Enter any Number : ");

number = sc.nextInt();

while(i <= number)

evenSum = evenSum + i;

i = i + 2;

}
System.out.println("\n The Sum of Even Numbers upto " + number + " =
" + evenSum);

//sum of odd numbers

public class sumofodd {

private static Scanner sc;

public static void main(String[] args)

{ int number, i = 1, oddsum = 0;

sc = new Scanner(System.in);

System.out.print(" Please Enter any Number : ");

number = sc.nextInt();

while(i <= number)

{ oddsum = oddsum + i;

i = i + 2;

System.out.println("\n The Sum of odd Numbers upto " + number + " = "
+ oddsum);

//do while loop

public class dowhileexample {


public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}

You might also like