You are on page 1of 31

Java Program – Basics

1. Write a Java program to print ‘Hello World!’ on screen

Public class Hello {

public static void main(String[] args) {

System.out.println("Hello, World!");

2. Write a Java program to print the sum of two numbers

import java.util.Scanner;

class sum {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the two numbers");

int a=sc.nextInt();

int b=sc.nextInt();

int c=a+b;

System.out.println("Sum of " +a+ " and " +b+ " is: " +c);

3. Write a Java program that takes two numbers and display the product of two numbers

import java.util.Scanner;

Public class Mul {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);

System.out.println("Enter the two numbers");

int a=sc.nextInt();

int b=sc.nextInt();

int c=a*b;

System.out.println("Product of " +a+ " and " +b+ " is: " +c);

4. Write a Java program to print the sum, multiply, subtract, divide and remainder of two numbers

import java.util.Scanner;

Public class All {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the two numbers");

int a=sc.nextInt();

int b=sc.nextInt();

int c=a*b;

int z=a+b;

int x=a-b;

float y=a/b;

float w=a%b;

System.out.println("sum of number is:" +z+ "\n subtract of the number: " +x+ "\n Multiplication of the
number is : "+c+ "\n Division of the number is: "+y+" \n reminder is:"+w);

}
5. Write a Java program that takes five numbers as input to calculate and print the average of the

Numbers

import java.util.Scanner;

Public class Avg {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the five numbers");

int a=sc.nextInt();

int b=sc.nextInt();

int c=sc.nextInt();

int d=sc.nextInt();

int e=sc.nextInt();

float f=(a+b+c+d+e)/5;

System.out.println(" Average of the numbers is : "+f);

6. Write a Java program to swap two variables

import java.util.Scanner;

public class Swap

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter the two numbers:");

int a=sc.nextInt();

int b=sc.nextInt();
System.out.println("Before swapping A= "+a+" B= "+b);

int temp=a;

a=b;

b=temp;

System.out.println("After swapping A= "+a+" B= "+b);

7. Write a Java program to convert a decimal number to binary numbers

import java.util.Scanner;

public class Decimal

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter the decimal numbers:");

int a=sc.nextInt();

String b=Integer.toBinaryString(a);

System.out.println("binary number of "+a+" is: "+b);

8. Write a Java program to convert a binary number to decimal number

import java.util.Scanner;

public class Binary

{
public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter the binary numbers:");

String a=sc.nextLine();

int b=Integer.parseInt(a,2);

System.out.println("decimal number of "+a+" is "+b);

10. Write a Java program and compute the sum of the digits of an integer

import java.util.Scanner;

public class Sumd

public static void main(String args[])

int sum=0,n;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number:");

int a=sc.nextInt();

while(a > 0)

n = a % 10;

sum = sum + n;

a = a / 10;

System.out.println("sum of digit is:"+sum);

}
}

11. Write a Java program to compare two numbers

import java.util.Scanner;

public class Comp

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter two numbers:");

int a=sc.nextInt();

int b=sc.nextInt();

if(a>b)

System.out.println(a+ "is greater");

else if(b>a){

System.out.println(b+ "is greater");

if(a==b)

System.out.println("the two numbers are equal");

12. Write a Java program to count the letters, spaces, numbers and other characters of an input

String
import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the string");

String Test = sc.nextLine();

count(Test);

public static void count(String x){

char[] ch = x.toCharArray();

int letter = 0;

int space = 0;

int num = 0;

int otherchat = 0;

for(int i = 0; i < x.length(); i++){

if(Character.isLetter(ch[i])){

letter ++ ;

else if(Character.isDigit(ch[i])){

num ++ ;

else if(Character.isSpaceChar(ch[i])){

space ++ ;

else{

otherchat ++;

}
System.out.println("letter: " + letter);

System.out.println("space: " + space);

System.out.println("number: " + num);

System.out.println("other: " + otherchat);

13. Write a Java program to print the ascii value of a given character

import java.util.Scanner;

public class Asciii

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("enter the charecter:");

char ch=sc.next().charAt(0);

int ascii = ch;

int castAscii = (int) ch;

System.out.println("The ASCII value of " + ch + " is: " + castAscii);

14. Write a Java program that accepts an integer (n) and computes the value of n+nn+nnn

import java.util.Scanner;

public class Int {

public static void main(String[] args)

{
int n;

Scanner sc = new Scanner(System.in);

System.out.print("Input number: ");

n = sc.nextInt();

System.out.printf("%d + %d%d + %d%d%d\n", n, n, n, n, n, n);

15. Write a Java program to display the system time

import java.util.*;

public class GFG {

public static void main(String args[])

Date current_Date = new Date();

System.out.println(current_Date);

16. Write a Java program to print the odd numbers from 1 to 20

import java.util.*;

public class ODD {

public static void main(String[] args){

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

if (i % 2 != 0) {

System.out.println(i);

}
}

17. Write a Java program to convert a string to an integer

import java.util.Scanner;

public class STR

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter the string:");

String s=sc.nextLine();

int j=Integer.parseInt(s);

System.out.println(j);

18. Write a Java program to convert seconds to hour, minute and seconds

import java.util.*;

import java.util.Scanner;

public class Second{

public static void main(String[] args)

Scanner in = new Scanner(System.in);

System.out.print("Enter the seconds: ");

int s = in.nextInt();

int p1 = s % 60;
int p2 = s / 60;

int p3 = p2 % 60;

p2 = p2 / 60;

System.out.print( p2 + ":" + p3 + ":" + p1);

19. Write a Java program to compute the sum of the first 100 prime numbers

import java.util.*;

public class Prime {

public static void main(String[] args)

int sum = 1;

int ctr = 0;

int num = 0;

while (ctr < 100) {

num++;

if (num % 2 != 0) {

if (is_Prime(num)) {

sum += num;

ctr++;

System.out.println("\nSum of the first 100 prime numbers is: "+sum);

public static boolean is_Prime(int num) {

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


if (num % i == 0) {

return false;

return true;

20. Write a Java program to swap the first and last elements of an array and create a new array

import java.util.Arrays;

public class Array {

public static void main(String[] args)

int[] array_nums = {20, 40, 90,40};

System.out.println("Original Array: "+Arrays.toString(array_nums));

int x = array_nums[0];

array_nums[0] = array_nums[array_nums.length-1];

array_nums[array_nums.length-1] = x;

System.out.println("New array after swaping the first and last elements:


"+Arrays.toString(array_nums));

21. Write a Java program to count the number of even and odd elements in a given array

import java.util.Scanner;

public class Array {

public static void main(String[] args)


{

Scanner sc=new Scanner(System.in);

int odd=0,even=0;

System.out.println("enter the array size:");

int n=sc.nextInt();

int[] arr=new int[10];

System.out.println("enter the array elements:");

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

System.out.println("Array elements are:");

for(int i=0;i<n;i++)

System.out.println(arr[i]);

for(int i=0;i<n;i++)

if(arr[i]%2==0)

even++;

}else{

odd++;

System.out.println("Number of even numbers:"+even);

System.out.println("Number of odd numbers:"+odd);

}
22. Write a Java program to compute the square root of a given integer

import java.util.Scanner;

public class Sqrt {

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("enter the positive integer:");

int n=sc.nextInt();

double temp;

double sqrtroot=n/2;

do

temp=sqrtroot;

sqrtroot=(temp+(n/temp))/2;

while((temp-sqrtroot)!= 0);

System.out.println("Sqrroot of the number is:"+sqrtroot);

23. Write a Java program to check if a positive number is a palindrome or not

import java.util.Scanner;

public class Plindrome {

public static void main(String[] args)

Scanner sc=new Scanner(System.in);


System.out.println("enter the positive integer:");

int n=sc.nextInt();

int r,sum=0;

int temp=n;

while(n>0)

r=n%10;

sum=(sum*10)+r;

n=n/10;

if(temp==sum)

System.out.println("The given number is palindrome:");

else

System.out.println("The given number is not a palindrome:");

24. Write a Java program to add two numbers without using any arithmetic operators

import java.util.Scanner;

public class Sum {

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("enter two numbers:");

int n=sc.nextInt();

int b=sc.nextInt();

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

n++;

System.out.println("Sum is:"+n);

25. Write a Java program to add all the digits of a given positive integer

import java.util.Scanner;

public class Sum_d {

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("enter the positive integer:");

int n=sc.nextInt();

int d,sum=0;

while(n>0)

d=n%10;

sum=sum+d;

n=n/10;

System.out.println("Sum of digit is:"+sum);

}
Java Program – Conditional

1. Write a Java program to take three numbers from the user and print the greatest number

import java.util.Scanner;

public class Greatest {

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("enter three numbers:");

int n=sc.nextInt();

int b=sc.nextInt();

int c=sc.nextInt();

if(n>b)

if(n>c)

System.out.println(n+ "is greatest number");

else if(b>c)

System.out.println(b+ "is greatest number");

else

System.out.println(c+ "is greatest number");

}
}

2. Write a Java program to find the number of days in a month

import java.util.Scanner;

public class Month{

public static void main(String[] strings) {

Scanner input = new Scanner(System.in);

int DaysInMonth = 0;

String MonthOfName = "";

System.out.print("Input a month number: ");

int month = input.nextInt();

System.out.print("Input a year: ");

int year = input.nextInt();

switch (month) {

case 1:

MonthOfName = "January";

DaysInMonth = 31;

break;

case 2:

MonthOfName = "February";

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {

DaysInMonth = 29;

} else {

DaysInMonth = 28;

break;
case 3:

MonthOfName = "March";

DaysInMonth = 31;

break;

case 4:

MonthOfName = "April";

DaysInMonth = 30;

break;

case 5:

MonthOfName = "May";

DaysInMonth = 31;

break;

case 6:

MonthOfName = "June";

DaysInMonth = 30;

break;

case 7:

MonthOfName = "July";

DaysInMonth = 31;

break;

case 8:

MonthOfName = "August";

DaysInMonth = 31;

break;

case 9:

MonthOfName = "September";

DaysInMonth = 30;

break;

case 10:
MonthOfName = "October";

DaysInMonth = 31;

break;

case 11:

MonthOfName = "November";

DaysInMonth = 30;

break;

case 12:

MonthOfName = "December";

DaysInMonth = 31;

System.out.print(MonthOfName + " " + year + " has " + DaysInMonth + " days\n");

3. Write a Java program to test a number is positive or negative

import java.util.Scanner;

public class PN{

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a positive integer:");

int n=sc.nextInt();

if(n>=0)

System.out.println("The given number is positive number:");

else

System.out.println("The given number is negative number:");

}
4. Write a Java Program to accept number of week’s day (1-7) and print name of the day

import java.util.Scanner;

public class Day{

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of weak(1-7):");

int n=sc.nextInt();

switch(n)

case 1:System.out.println("SUNDAY");

break;

case 2:System.out.println("MONDAY");

break;

case 3:System.out.println("TUESDAY");

break;

case 4:System.out.println("WEDNESDAY");

break;

case 5:System.out.println("THURSDAY");

break;

case 6:System.out.println("FRIDAY");

break;

case 7:System.out.println("SATURDAY");

break;

}
}

5. Write a Java program that takes a year from user and print whether that year is a leap year or not

import java.util.Scanner;

public class Leap{

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the year:");

int n=sc.nextInt();

if((n%4==0)&&(n%400==0)||(n%100!=0))

System.out.println("The year is leap year:");

else

System.out.println("Not a leap year");

6. Write a Java program to input 5 numbers from keyboard and find their sum and average

import java.util.Scanner;

public class Sumavg

public static void main(String args[])

int s=0,avg=0;

Scanner sc=new Scanner(System.in);

System.out.println("Input the 5 numbers : ");

for (int i=0;i<5;i++)

{
Scanner in = new Scanner(System.in);

int n = in.nextInt();

s +=n;

avg=s/5;

System.out.println("The sum of 5 no is : " +s+"\nThe Average is : " +avg);

7. Write a program in Java to display the first 5 natural numbers

public class Natural{

public static void main(String[] args)

System.out.println ("\nfirst five natural numbers are :");

for(int i=1;i<=5;i++)

System.out.println (i);

8. Write a java program to check vowel or consonant

import java.util.Scanner;

public class CV

public static void main(String args[])

{
int s=0,avg=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter a character:");

char c=sc.next().charAt(0);

if (c== 'a' || c == 'e' || c == 'i' || c == 'o'

|| c == 'u')

System.out.println("It is a Vowel.");

else

System.out.println("It is a Consonant.");

9. Write a Java program to display the n terms of odd natural number and their sum

import java.util.Scanner;

public class Odd {

public static void main(String[] args)

int i,sum=0;

System.out.print("Input number of terms is: ");

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

System.out.println ("\nThe odd numbers are :");

for(i=1;i<=n;i++)

System.out.println (2*i-1);

sum+=2*i-1;

System.out.println ("The Sum of odd Natural Number upto " +n+" terms is: " +sum);
}

10. Write a Java program to display the multiplication table of a given integer

import java.util.Scanner;

public class Mul{

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the integer:");

int n=sc.nextInt();

int sum;

for(int i=1;i<=10;i++)

sum=i*n;

System.out.println(i+"*"+n+"="+sum);

11. Write a Java program that reads an integer and check whether it is negative, zero, or positive

import java.util.Scanner;

public class ZPN{

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);


System.out.println("Enter the number:");

int n=sc.nextInt();

if(n==0)

System.out.println("The number is ZERO");

}else if(n>0) {

System.out.println("The number is POSITIVE");

}else{

System.out.println("The number is NEGATIVE");

12. Write a Java program that reads a positive integer and count the number of digits

import java.util.Scanner;

public class Digit {

public static void main(String[] args) {

int count = 0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the integer:");

int n=sc.nextInt();

while(n!= 0)

n=n/10;

++count;

System.out.println("Number of digits: " + count);


}

13. Write a Java program that accepts three numbers and check All numbers are equal or not

import java.util.Scanner;

public class Equal{

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter 3 number:");

int n=sc.nextInt();

int b=sc.nextInt();

int c=sc.nextInt();

if((n==b) && (b==c))

System.out.println("Three numbers are equal:");

else

System.out.println("Three numbers are NOT equal:");

14. Write a java program that accepts three numbers from the user and check if numbers are in

“increasing” or “decreasing” order.

import java.util.Scanner;

public class ID

public static void main(String args[])

Scanner sc=new Scanner(System.in);


System.out.println("Enter the three numbers:");

int a=sc.nextInt();

int b=sc.nextInt();

int c=sc.nextInt();

if((a<b)&&(b<c))

System.out.println("Increasing order:");

else if((a>b)&&(b>c))

System.out.println("Decreasing order:");

}else{

System.out.println("Neither decreasing or increasing");

15. Write a Java program that determines a student’s grade [take marks of 5 subjects as input, if

total mark is &gt;80% grade is A, 79-60% grade is B, 59-40% grade is C, &lt;40 is F.

import java.util.Scanner;

public class Grade{

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);

int sum=0;

System.out.println("enter the maths mark:");

int n=sc.nextInt();

System.out.println("enter the java mark:");

int b=sc.nextInt();
System.out.println("enter the c mark:");

int c=sc.nextInt();

System.out.println("enter the c++ mark:");

int d=sc.nextInt();

System.out.println("enter the dbms mark:");

int f=sc.nextInt();

sum=((n+b+c+d+f)/5);

if(sum>=80)

System.out.println("Grade is A");

else if((sum>=60) && (sum<=79))

System.out.println("Grade is B");

else if((sum>=40) && (sum<=59))

System.out.println("Grade is C");

else if(sum<40 )

System.out.println("Grade is F");

16. Write a Java program to create a simple calculator

import java.util.Scanner;
public class Calcu{

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);

int sum=0;

System.out.println("Enter two numbers:");

int b=sc.nextInt();

int c=sc.nextInt();

System.out.println("1.ADDITION \n 2.SUBTRACTION \n 3.MULTIPLICATION \n 4.DIVISION:");

int n=sc.nextInt();

switch(n)

case 1:sum=b+c;

System.out.println("RESULT IS:"+sum);

break;

case 2:sum=b-c;

System.out.println("RESULT IS:"+sum);

break;

case 3:sum=b*c;

System.out.println("RESULT IS:"+sum);

break;

case 4:sum=b/c;

System.out.println("RESULT IS:"+sum);

break;

You might also like