1.
Read a single integer from the user and print it
Program
import [Link];
public class ReadInteger {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num;
[Link]("Enter a number: ");
num = [Link]();
[Link]("Number is: " + num);
}
}
Input
Enter a number: 10
Output
Number is: 10
2. Read two numbers in a single line and print their sum
Program
import [Link];
public class SumTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a,b;
[Link]("Enter two numbers:");
a = [Link]();
b = [Link]();
[Link]("Sum = " + (a+b));
}
}
Input
Enter two numbers:
57
Output
Sum = 12
3. Read two numbers in separate lines and print their product
Program
import [Link];
public class ProductNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a,b;
[Link]("Enter first number:");
a = [Link]();
[Link]("Enter second number:");
b = [Link]();
[Link]("Product = " + (a*b));
}
}
Input
Enter first number: 3
Enter second number: 4
Output
Product = 12
4. Read a string input and print it in uppercase
Program
import [Link];
public class UpperCaseString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String str;
[Link]("Enter a string:");
str = [Link]();
[Link]("Uppercase: " + [Link]());
}
}
Input
Enter a string:
hello
Output
Uppercase: HELLO
5. Read a name and age and display greeting
Program
import [Link];
public class Greeting {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String name;
int age;
[Link]("Enter name:");
name = [Link]();
[Link]("Enter age:");
age = [Link]();
[Link]("Hello " + name + ", you are " + age + " years old");
}
}
Input
Enter name: Hari
Enter age: 20
Output
Hello Hari, you are 20 years old
6. Read three integers and print the largest number
Program
import [Link];
public class LargestNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a,b,c;
[Link]("Enter three numbers:");
a = [Link]();
b = [Link]();
c = [Link]();
int largest = a;
if(b > largest)
largest = b;
if(c > largest)
largest = c;
[Link]("Largest number = " + largest);
}
}
Input
Enter three numbers:
583
Output
Largest number = 8
7. Read a number and print whether it is even or odd
Program
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num;
[Link]("Enter a number:");
num = [Link]();
if(num % 2 == 0)
[Link]("Even number");
else
[Link]("Odd number");
}
}
Input
Enter a number: 7
Output
Odd number
8. Read a number and print its square and cube
================================================
Program
Java
import [Link];
public class SquareCube {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num;
[Link]("Enter a number:");
num = [Link]();
[Link]("Square = " + (num * num));
[Link]("Cube = " + (num * num * num));
}
}
Input
Enter a number:
3
Output
Square = 9
Cube = 27
9. Read length and width and print the area of rectangle
==========================================================
Program
Java
import [Link];
public class RectangleArea {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int length, width;
[Link]("Enter length:");
length = [Link]();
[Link]("Enter width:");
width = [Link]();
int area = length * width;
[Link]("Area of rectangle = " + area);
}
}
Input
Enter length: 5
Enter width: 4
Output
Area of rectangle = 20
10. Read principal, rate, time and calculate simple interest
=============================================================
=
Program
Java
import [Link];
public class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double p, r, t, si;
[Link]("Enter principal:");
p = [Link]();
[Link]("Enter rate:");
r = [Link]();
[Link]("Enter time:");
t = [Link]();
si = (p * r * t) / 100;
[Link]("Simple Interest = " + si);
}
}
Input
Enter principal: 1000
Enter rate: 5
Enter time: 2
Output
Simple Interest = 100.0
11. Print numbers from 1 to N
===============================
Program
Java
import [Link];
public class PrintNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter N:");
n = [Link]();
for(int i = 1; i <= n; i++) {
[Link](i + " ");
}
}
}
Input
Enter N: 5
Output
12345
12. Temperature conversion Celsius to Fahrenheit
==================================================
Program
Java
import [Link];
public class TemperatureConvert {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double c, f;
[Link]("Enter temperature in Celsius:");
c = [Link]();
f = (c * 9/5) + 32;
[Link]("Fahrenheit = " + f);
}
}
Input
Enter temperature in Celsius:
25
Output
Fahrenheit = 77.0
13. Print sum of first N natural numbers
=========================================
Program
Java
import [Link];
public class SumNaturalNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n, sum = 0;
[Link]("Enter N:");
n = [Link]();
for(int i = 1; i <= n; i++) {
sum = sum + i;
}
[Link]("Sum = " + sum);
}
}
Input
Enter N:
5
Output
Sum = 15
14. Print multiplication table of a number
============================================
Program
Java
import [Link];
public class MultiplicationTable {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter a number:");
n = [Link]();
for(int i = 1; i <= 10; i++) {
[Link](n + " x " + i + " = " + (n * i));
}
}
}
Input
Enter a number:
5
Output
5x1=5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
15. Print factorial of a number
=================================
Program
Java
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n, fact = 1;
[Link]("Enter a number:");
n = [Link]();
for(int i = 1; i <= n; i++) {
fact = fact * i;
}
[Link]("Factorial = " + fact);
}
}
Input
Enter a number:
5
Output
Factorial = 120
16. Generate Fibonacci series up to N terms
=============================================
Program
Java
import [Link];
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n, a = 0, b = 1, c;
[Link]("Enter number of terms:");
n = [Link]();
for(int i = 1; i <= n; i++) {
[Link](a + " ");
c = a + b;
a = b;
b = c;
}
}
}
Input
Enter number of terms:
5
Output
01123
17. Count the number of digits in a number
============================================
Program
Java
import [Link];
public class CountDigits {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num, count = 0;
[Link]("Enter a number:");
num = [Link]();
while(num != 0) {
num = num / 10;
count++;
}
[Link]("Number of digits = " + count);
}
}
Input
Enter a number:
1234
Output
Number of digits = 4
18. Reverse a number
======================
Program
Java
import [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num, reverse = 0;
[Link]("Enter a number:");
num = [Link]();
while(num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
[Link]("Reversed number = " + reverse);
}
}
Input
Enter a number:
123
Output
Reversed number = 321
19. Check whether a number is palindrome
==========================================
Program
Java
import [Link];
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num, reverse = 0, temp;
[Link]("Enter a number:");
num = [Link]();
temp = num;
while(num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
if(temp == reverse)
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}
Input
Enter a number:
121
Output
Palindrome
20. Check whether a number is prime
=====================================
Program
Java
import [Link];
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num, count = 0;
[Link]("Enter a number:");
num = [Link]();
for(int i = 1; i <= num; i++) {
if(num % i == 0)
count++;
}
if(count == 2)
[Link]("Prime Number");
else
[Link]("Not a Prime Number");
}
}
Input
Enter a number:
7
Output
Prime Number
21. Find the largest of three numbers
=======================================
Program
import [Link];
public class LargestThree {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a,b,c;
[Link]("Enter three numbers:");
a=[Link]();
b=[Link]();
c=[Link]();
int max=a;
if(b>max)
max=b;
if(c>max)
max=c;
[Link]("Largest number = "+max);
}
}
Input
Enter three numbers:
583
Output
Largest number = 8
22. Swap two numbers using third variable
===========================================
Program
import [Link];
public class SwapNumbers {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int a,b,temp;
[Link]("Enter two numbers:");
a=[Link]();
b=[Link]();
temp=a;
a=b;
b=temp;
[Link]("After swap:");
[Link]("a="+a);
[Link]("b="+b);
}
}
Input
Enter two numbers:
5 10
Output
After swap
a=10
b=5
23. Swap two numbers without third variable
=============================================
Program
import [Link];
public class SwapWithoutTemp {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int a,b;
[Link]("Enter two numbers:");
a=[Link]();
b=[Link]();
a=a+b;
b=a-b;
a=a-b;
[Link]("After swap:");
[Link]("a="+a);
[Link]("b="+b);
}
}
Input
Enter two numbers
5 10
Output
After swap
a=10
b=5
24. Find sum of digits of a number
====================================
Program
import [Link];
public class SumDigits {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int num,sum=0;
[Link]("Enter number:");
num=[Link]();
while(num!=0)
{
sum=sum+(num%10);
num=num/10;
}
[Link]("Sum of digits = "+sum);
}
}
Input
Enter number
123
Output
Sum of digits = 6
25. Check Armstrong number
============================
Program
import [Link];
public class Armstrong {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int num,temp,sum=0;
[Link]("Enter number:");
num=[Link]();
temp=num;
while(num!=0)
{
int r=num%10;
sum=sum+r*r*r;
num=num/10;
}
if(temp==sum)
[Link]("Armstrong Number");
else
[Link]("Not Armstrong");
}
}
Input
153
Output
Armstrong Number
26. Check Perfect Number
==========================
Program
import [Link];
public class PerfectNumber {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int num,sum=0;
[Link]("Enter number:");
num=[Link]();
for(int i=1;i<num;i++)
{
if(num%i==0)
sum=sum+i;
}
if(sum==num)
[Link]("Perfect Number");
else
[Link]("Not Perfect Number");
}
}
Input
6
Output
Perfect Number
27. Find LCM of two numbers
=============================
Program
import [Link];
public class LCM {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int a,b,lcm;
[Link]("Enter two numbers:");
a=[Link]();
b=[Link]();
lcm=(a>b)?a:b;
while(true)
{
if(lcm%a==0 && lcm%b==0)
break;
lcm++;
}
[Link]("LCM = "+lcm);
}
}
Input
46
Output
LCM = 12
28. Find GCD of two numbers
=============================
Program
import [Link];
public class GCD {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int a,b,gcd=1;
[Link]("Enter two numbers:");
a=[Link]();
b=[Link]();
for(int i=1;i<=a && i<=b;i++)
{
if(a%i==0 && b%i==0)
gcd=i;
}
[Link]("GCD = "+gcd);
}
}
Input
8 12
Output
GCD = 4
29. Print prime numbers between 1 and N
=========================================
Program
import [Link];
public class PrimeRange {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n;
[Link]("Enter N:");
n=[Link]();
for(int i=2;i<=n;i++)
{
int count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
[Link](i+" ");
}
}
}
Input
10
Output
2357
30. Sum of even numbers between 1 and N
=========================================
Program
import [Link];
public class SumEven {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n,sum=0;
[Link]("Enter N:");
n=[Link]();
for(int i=1;i<=n;i++)
{
if(i%2==0)
sum=sum+i;
}
[Link]("Sum = "+sum);
}
}
Input
10
Output
Sum = 30
31. Read N elements into an array and print them
=================================================
Program
import [Link];
public class ArrayPrint {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter number of elements:");
n = [Link]();
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link]("Array elements:");
for(int i=0;i<n;i++)
[Link](arr[i]+" ");
}
}
Input
Enter number of elements:
5
12345
Output
Array elements:
12345
32. Find sum of elements in an array
=====================================
Program
import [Link];
public class ArraySum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n,sum=0;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
for(int i=0;i<n;i++)
sum=sum+arr[i];
[Link]("Sum = "+sum);
}
}
Input
5
12345
Output
Sum = 15
33. Find largest element in an array
=====================================
Program
import [Link];
public class LargestArray {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
int max=arr[0];
for(int i=1;i<n;i++)
{
if(arr[i]>max)
max=arr[i];
}
[Link]("Largest = "+max);
}
}
Input
5
28416
Output
Largest = 8
34. Find smallest element in an array
======================================
Program
import [Link];
public class SmallestArray {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
int min=arr[0];
for(int i=1;i<n;i++)
{
if(arr[i]<min)
min=arr[i];
}
[Link]("Smallest = "+min);
}
}
Input
5
28416
Output
Smallest = 1
35. Reverse an array
=====================
Program
import [Link];
public class ReverseArray {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link]("Reversed array:");
for(int i=n-1;i>=0;i--)
[Link](arr[i]+" ");
}
}
Input
5
12345
Output
Reversed array
54321
36. Find average of array elements
===================================
Program
import [Link];
public class AverageArray {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n,sum=0;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
for(int i=0;i<n;i++)
sum=sum+arr[i];
double avg=(double)sum/n;
[Link]("Average = "+avg);
}
}
Input
5
12345
Output
Average = 3.0
37. Count even numbers in array
================================
Program
import [Link];
public class CountEven {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n,count=0;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
count++;
}
[Link]("Even numbers = "+count);
}
}
Input
5
12346
Output
Even numbers = 3
38. Count odd numbers in array
================================
Program
import [Link];
public class CountOdd {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n,count=0;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
count++;
}
[Link]("Odd numbers = "+count);
}
}
Input
5
12345
Output
Odd numbers = 3
39. Check number in array
==========================
Program
import [Link];
public class CheckElement {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n,search,flag=0;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link]("Enter element to search:");
search=[Link]();
for(int i=0;i<n;i++)
{
if(arr[i]==search)
flag=1;
}
if(flag==1)
[Link]("Element found");
else
[Link]("Element not found");
}
}
Input
5
12345
3
Output
Element found
40. Linear Search
==================
Program
import [Link];
public class LinearSearch {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n,search,pos=-1;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link]("Enter element:");
search=[Link]();
for(int i=0;i<n;i++)
{
if(arr[i]==search)
pos=i;
}
[Link]("Position = "+pos);
}
}
Input
5
12345
4
Output
Position = 3
41. Sort array elements in ascending order
============================================
Program
import [Link];
public class SortArray {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
[Link]("Sorted array:");
for(int i=0;i<n;i++)
[Link](arr[i]+" ");
}
}
Input
5
52413
Output
Sorted array
12345
42. Count characters without space
===================================
Program
import [Link];
public class CountCharacters {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str;
[Link]("Enter string:");
str=[Link]();
str=[Link](" ","");
[Link]("Characters = "+[Link]());
}
}
Input
hello world
Output
Characters = 10
43. Count vowels in string
============================
Program
import [Link];
public class CountVowels {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str;
int count=0;
[Link]("Enter string:");
str=[Link]();
for(int i=0;i<[Link]();i++)
{
char c=[Link](i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'
||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
count++;
}
[Link]("Vowels = "+count);
}
}
Input
education
Output
Vowels = 5
44. Reverse a string
=====================
Program
import [Link];
public class ReverseString {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str;
[Link]("Enter string:");
str=[Link]();
String rev="";
for(int i=[Link]()-1;i>=0;i--)
rev=rev+[Link](i);
[Link]("Reversed = "+rev);
}
}
Input
hello
Output
Reversed = olleh
45. String Palindrome
======================
Program
import [Link];
public class StringPalindrome {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str,rev="";
[Link]("Enter string:");
str=[Link]();
for(int i=[Link]()-1;i>=0;i--)
rev=rev+[Link](i);
if([Link](rev))
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}
Input
madam
Output
Palindrome
46. Count words in sentence
=============================
Program
import [Link];
public class CountWords {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str;
[Link]("Enter sentence:");
str=[Link]();
String words[]=[Link](" ");
[Link]("Word count = "+[Link]);
}
}
Input
Java is easy
Output
Word count = 3
47. Replace spaces with hyphen
================================
Program
import [Link];
public class ReplaceSpaces {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str;
[Link]("Enter string:");
str=[Link]();
str=[Link](" ","-");
[Link](str);
}
}
Input
java programming language
Output
java-programming-language
48. Find frequency of character
=================================
Program
import [Link];
public class CharFrequency {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str;
char ch;
int count=0;
[Link]("Enter string:");
str=[Link]();
[Link]("Enter character:");
ch=[Link]().charAt(0);
for(int i=0;i<[Link]();i++)
{
if([Link](i)==ch)
count++;
}
[Link]("Frequency = "+count);
}
}
Input
banana
a
Output
Frequency = 3
49. Iterate the given string
==============================
Program
import [Link];
public class IterateString {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str;
[Link]("Enter string:");
str=[Link]();
for(int i=0;i<[Link]();i++)
[Link]([Link](i));
}
}
Input
java
Output
j
a
v
a
50. Find second largest element in array
==========================================
Program
import [Link];
public class SecondLargest {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
int first=0,second=0;
for(int i=0;i<n;i++)
{
if(arr[i]>first)
{
second=first;
first=arr[i];
}
else if(arr[i]>second)
{
second=arr[i];
}
}
[Link]("Second largest = "+second);
}
}
Input
5
52836
Output
Second largest = 6
51. Remove duplicate elements from array
==========================================
Program
import [Link];
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n;
[Link]("Enter size:");
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
for(int i=0;i<n;i++)
{
boolean duplicate=false;
for(int j=0;j<i;j++)
{
if(arr[i]==arr[j])
duplicate=true;
}
if(!duplicate)
[Link](arr[i]+" ");
}
}
}
Input
6
122344
Output
1234
52. Print the patterns
================================
(i)
Pattern
*
**
***
****
*****
Program
import [Link];
public class Pattern1 {
public static void main(String[] args) {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
}
}
Input
5
Output
*
**
***
****
*****
(ii)
Pattern
*****
****
***
**
*
Program
public class Pattern2 {
public static void main(String[] args) {
int n = 5;
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
}
}
Input
5
Output
*****
****
***
**
*
(iii)
Pattern
*****
*****
*****
*****
*****
Program
public class Pattern3 {
public static void main(String[] args) {
int rows = 5;
int cols = 5;
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= cols; j++) {
[Link]("* ");
}
[Link]();
}
}
}
Input
5
Output
*****
*****
*****
*****
*****