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

Java Program Example - Print Table of Number

The document contains several Java programs that demonstrate different programming concepts: 1) A program that takes user input for a number and prints if it is even or odd. 2) A program that takes a number from the user and prints its multiplication table up to 10. 3) A program that takes the number of integers from the user, accepts that many numbers as input and prints their sum. 4) Several other programs that reverse a number, find prime numbers between ranges, find the largest of three numbers, and calculate the sum of first n natural numbers.
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)
70 views

Java Program Example - Print Table of Number

The document contains several Java programs that demonstrate different programming concepts: 1) A program that takes user input for a number and prints if it is even or odd. 2) A program that takes a number from the user and prints its multiplication table up to 10. 3) A program that takes the number of integers from the user, accepts that many numbers as input and prints their sum. 4) Several other programs that reverse a number, find prime numbers between ranges, find the largest of three numbers, and calculate the sum of first n natural numbers.
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/ 4

Even or odd

import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int num;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a Number : ");


num = scan.nextInt();

if(num%2 == 0)
{
System.out.print("This is an Even Number");
}
else
{
System.out.print("This is an Odd Number");
}
}
}
/* Java Program Example - Print Table of Number */
import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int num, i, tab;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a Number : ");


num = scan.nextInt();

System.out.print("Table of " + num + " is\n");


for(i=1; i<=10; i++)
{
tab = num*i;
System.out.print(num + " * " + i + " = " + tab + "\n");
}
}
}
/* Java Program Example - Add n Numbers */
import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int i, n, sum=0, num;
Scanner scan = new Scanner(System.in);

System.out.print("How many Number You want to Enter to Add them ?");


n = scan.nextInt();

System.out.print("Enter " +n+ " numbers : ");


for(i=0; i<n; i++)
{
num = scan.nextInt();
sum = sum + num;
}

System.out.print("Sum of all " +n+ " numbers is " +sum);


}
}
/* Java Program Example - Reverse a Number */
import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int num, rev=0, rem;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a Number : ");


num = scan.nextInt();

while(num != 0)
{
rem = num%10;
rev = rev*10 + rem;
num = num/10;
}

System.out.print("Reverse = " +rev);


}
}
/* Java Program Example - Print Prime Numbers */
import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int start, end, i, j, count=0;

/* to print all the prime numbers between any range


* enter the two number (starting and ending number)
*/

Scanner scan = new Scanner(System.in);

System.out.print("Enter the Range :\n");

System.out.print("Enter Starting Number : ");


start = scan.nextInt();
System.out.print("Enter Ending Number : ");
end = scan.nextInt();

System.out.print("Prime Numbers Between " + start + " and " +end+ " is
:\n");
for(i=start; i<=end; i++)
{
count = 0;
for(j=2; j<i; j++)
{
if(i%j == 0)
{
count++;
break;
}
}
if(count == 0)
{
System.out.print(i + " ");
}
}
}
}

/* Java Program Example - Find Largest of Three Numbers */


import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int a, b, c, big;
Scanner scan = new Scanner(System.in);

System.out.print("Enter Three Numbers : ");


a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();

// let a is the largest

big = a;

if(big<b)
{
if(b>c)
{
big = b;
}
else
{
big = c;
}
}
else if(big<c)
{
if(c>b)
{
big = c;
}
else
{
big = b;
}
}
else
{
big = a;
}

System.out.print("Largest Number is " +big);


}
}

Example 3: Program to find sum of first n (entered by user)


natural numbers
import java.util.Scanner;
public class Demo {

public static void main(String[] args) {

int num, count, total = 0;

System.out.println("Enter the value of n:");


//Scanner is used for reading user input
Scanner scan = new Scanner(System.in);
//nextInt() method reads integer entered by user
num = scan.nextInt();
//closing scanner after use
scan.close();
for(count = 1; count <= num; count++){
total = total + count;
}

System.out.println("Sum of first "+num+" natural numbers is: "+total);


}
}

You might also like