Java Program Example - Print Table of Number

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy