Java Sample Problem

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 28

K.

Mugundhan
2nd B.SC.IT
JAVA PROGRAMMING
1.How to Print an Integer entered by an user
Import java.util.Scanner;

Public class HelloWorld {

Public static void main(String[] args) {

// Creates a reader instance which takes


// input from standard input – keyboard
Scanner reader = new Scanner(System.in);
System.out.print(“Enter a number: “);

// nextInt() reads the next integer from the keyboard


Int number = reader.nextInt();

// println() prints the following line to the output screen


System.out.println(“You entered: “ + number);
}
}
Output
Enter a number: 10
You entered: 10
2.Program to Add Two Integers
Class Main {
Public static void main(String[] args) {

Int first = 10;


Int second = 20;

// add two numbers


Int sum = first + second;
System.out.println(first + “ + “ + second + “ = “ + sum);
}
}
Output
Enter two numbers:10 20
The sum is: 30
3.Multiply Two Floating-Point Numbers
Public class MultiplyTwoNumbers {

Public static void main(String[] args) {

Float first = 1.5f;


Float second = 2.0f;

Float product = first * second;

System.out.println(“The product is: “ + product);


}
}
Output
The product is: 3.0
4.Find ASCII value of a character
Public class AsciiValue {

Public static void main(String[] args) {

Char ch = ‘a’;
Int ascii = ch;
// You can also cast char to int
Int castAscii = (int) ch;

System.out.println(“The ASCII value of “ + ch + “ is: “ + ascii);


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

The ASCII value of a is: 97


The ASCII value of a is: 97
5. Compute Quotient and Remainder
Public class QuotientRemainder {

Public static void main(String[] args) {

Int dividend = 25, divisor = 4;


Int quotient = dividend / divisor;
Int remainder = dividend % divisor;

System.out.println(“Quotient = “ + quotient);
System.out.println(“Remainder = “ + remainder);
}
}
Output:

Quotient = 6
Remainder = 1
6.Java with Programiz PRO!
Claim Discount Now

Programiz

Search…
Programiz PRO

Java Program to Swap Two Numbers


To understand this example, you should have the knowledge of the following Java programming
topics:

Java Data Types (Primitive)


Java Operators
Example 1: Swap two numbers using temporary variable
Public class SwapNumbers {

Public static void main(String[] args) {

Float first = 1.20f, second = 2.45f;

System.out.println(“—Before swap—“);
System.out.println(“First number = “ + first);
System.out.println(“Second number = “ + second);

// Value of first is assigned to temporary


Float temporary = first;

// Value of second is assigned to first


First = second;

// Value of temporary (which contains the initial value of first) is assigned to second
Second = temporary;

System.out.println(“—After swap—“);
System.out.println(“First number = “ + first);
System.out.println(“Second number = “ + second);
}
}
Output:

--Before swap—
First number = 1.2
Second number = 2.45
--After swap—
First number = 2.45
Second number = 1.2
7. ..
Programiz PRO

Java Program to Check Whether a Number is Even or Odd


To understand this example, you should have the knowledge of the following Java programming
topics:

Java if…else Statement


Java Scanner Class
Example 1: Check whether a number is even or odd using if…else statement
Import java.util.Scanner;

Public class EvenOdd {

Public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

System.out.print(“Enter a number: “);


Int num = reader.nextInt();
If(num % 2 == 0)
System.out.println(num + “ is even”);
Else
System.out.println(num + “ is odd”);
}
}
Output

Enter a number: 12
12 is even
8.Java with Programiz PRO!
Claim Discount Now

Programiz

Search…
Programiz PRO

Java Program to Check Whether an Alphabet is Vowel or Consonant


To understand this example, you should have the knowledge of the following Java programming
topics:

Java if…else Statement


Java switch Statement
9. Check whether an alphabet is vowel or consonant using if..else statement
Public class VowelConsonant {
Public static void main(String[] args) {

Char ch = ‘I’;

If(ch == ‘a’ || ch == ‘e’ || ch == ‘I’ || ch == ‘o’ || ch == ‘u’ )


System.out.println(ch + “ is vowel”);
Else
System.out.println(ch + “ is consonant”);

}
}
Output

I is vowel
10. Java Program to Find Roots of a Quadratic Equation
Public class Main {

Public static void main(String[] args) {

// value a, b, and c
Double a = 2.3, b = 4, c = 5.6;
Double root1, root2;

// calculate the determinant (b2 – 4ac)


Double determinant = b * b – 4 * a * c;
// check if determinant is greater than 0
If (determinant > 0) {

// two real and distinct roots


Root1 = (-b + Math.sqrt(determinant)) / (2 * a);
Root2 = (-b – Math.sqrt(determinant)) / (2 * a);

System.out.format(“root1 = %.2f and root2 = %.2f”, root1, root2);


}

// check if determinant is equal to 0


Else if (determinant == 0) {

// two real and equal roots


// determinant is equal to 0
// so -b + 0 == -b
Root1 = root2 = -b / (2 * a);
System.out.format(“root1 = root2 = %.2f;”, root1);
}

// if determinant is less than zero


Else {

// roots are complex number and distinct


Double real = -b / (2 * a);
Double imaginary = Math.sqrt(-determinant) / (2 * a);
System.out.format(“root1 = %.2f+%.2fi”, real, imaginary);
System.out.format(“\nroot2 = %.2f-%.2fi”, real, imaginary);
}
}
}
Output

Root1 = -0.87+1.30i and root2 = -0.87-1.30i


11.Java Program to Check a Leap Year
Public class Main {

Public static void main(String[] args) {

// year to be checked
Int year = 1900;
Boolean leap = false;

// if the year is divided by 4


If (year % 4 == 0) {

// if the year is century


If (year % 100 == 0) {

// if year is divided by 400


// then it is a leap year
If (year % 400 == 0)
Leap = true;
Else
Leap = false;
}

// if the year is not century


Else
Leap = true;
}

Else
Leap = false;

If (leap)
System.out.println(year + “ is a leap year.”);
Else
System.out.println(year + “ is not a leap year.”);
}
}
Output

1900 is not a leap year.


12.Check if a Number is Positive or Negative using if else
Public class PositiveNegative {

Public static void main(String[] args) {

Double number = 12.3;


// true if number is less than 0
If (number < 0.0)
System.out.println(number + “ is a negative number.”);

// true if number is greater than 0


Else if ( number > 0.0)
System.out.println(number + “ is a positive number.”);

// if both test expression is evaluated to false


Else
System.out.println(number + “ is 0.”);
}
}
Output

12.3 is a positive number.


13.Java Program to Check Alphabet using if else
Public class Alphabet {

Public static void main(String[] args) {

Char c = ‘*’;

If( (c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c <= ‘Z’))
System.out.println(c + “ is an alphabet.”);
Else
System.out.println(c + “ is not an alphabet.”);
}
}
Output

 Is not an alphabet.

14.Sum of Natural Numbers using for loop


Public class SumNatural {

Public static void main(String[] args) {

Int num = 100, sum = 0;

For(int I = 1; I <= num; ++i)


{
// sum = sum + I;
Sum += I;
}

System.out.println(“Sum = “ + sum);
}
}
Output

Sum = 5050
15.Find Factorial of a number using for loop
Public class Factorial {
Public static void main(String[] args) {

Int num = 10;


Long factorial = 1;
For(int I = 1; I <= num; ++i)
{
// factorial = factorial * I;
Factorial *= I;
}
System.out.printf(“Factorial of %d = %d”, num, factorial);
}
}
Output

Factorial of 10 = 3628800
16. Generate Multiplication Table using for loop
Public class MultiplicationTable {

Public static void main(String[] args) {

Int num = 5;
For(int I = 1; I <= 10; ++i)
{
System.out.printf(“%d * %d = %d \n”, num, I, num * i);
}
}
}
Output

5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
17.Display Fibonacci Series Using for Loop
Class Main {
Public static void main(String[] args) {

Int n = 10, firstTerm = 0, secondTerm = 1;


System.out.println(“Fibonacci Series till “ + n + “ terms:”);

For (int I = 1; I <= n; ++i) {


System.out.print(firstTerm + “, “);

// compute the next term


Int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output
Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
18. Find GCD of two numbers using for loop and if statement
Class Main {
Public static void main(String[] args) {

// find GCD between n1 and n2


Int n1 = 81, n2 = 153;

// initially set to gcd


Int gcd = 1;

For (int I = 1; I <= n1 && I <= n2; ++i) {

// check if I perfectly divides both n1 and n2


If (n1 % I == 0 && n2 % I == 0)
Gcd = I;
}

System.out.println(“GCD of “ + n1 +” and “ + n2 + “ is “ + gcd);


}
}
Output
GCD of 81 and 153 is 9
19. LCM using while Loop and if Statement
Public class Main {
Public static void main(String[] args) {

Int n1 = 72, n2 = 120, lcm;

// maximum number between n1 and n2 is stored in lcm


Lcm = (n1 > n2) ? n1 : n2;

// Always true
While(true) {
If( lcm % n1 == 0 && lcm % n2 == 0 ) {
System.out.printf(“The LCM of %d and %d is %d.”, n1, n2, lcm);
Break;
}
++lcm;
}
}
}
Output

The LCM of 72 and 120 is 360.


20.Display uppercased alphabet using for loop
Class Main {
Public static void main(String[] args) {
Char c;

For(c = ‘A’; c <= ‘Z’; ++c)


System.out.print(c + “ “);
}
}
Output

ABCDEFGHIJKLMNOPQRSTUVWXYZ
21.Count Number of Digits in an Integer using while loop
Public class Main {

Public static void main(String[] args) {

Int count = 0, num = 0003452;

While (num != 0) {
// num = num/10
Num /= 10;
++count;
}

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


}
}
Output
Number of digits: 4
22.Reverse a Number using a while loop in Java
Class Main {
Public static void main(String[] args) {

Int num = 1234, reversed = 0;

System.out.println(“Original Number: “ + num);

// run loop until num becomes 0


While(num != 0) {

// get last digit from num


Int digit = num % 10;
Reversed = reversed * 10 + digit;

// remove the last digit from num


Num /= 10;
}

System.out.println(“Reversed Number: “ + reversed);


}
}
Output

Reversed Number: 4321


23.Calculate power of a number using a while loop
Class Main {
Public static void main(String[] args) {

Int base = 3, exponent = 4;

Long result = 1;

While (exponent != 0) {
Result *= base;
--exponent;
}

System.out.println(“Answer = “ + result);
}
}
Output

Answer = 81
24.Java Program to Check Palindrome String
Class Main {
Public static void main(String[] args) {

String str = “Radar”, reverseStr = “”;

Int strLength = str.length();

For (int I = (strLength – 1); I >=0; --i) {


reverseStr = reverseStr + str.charAt(i);
}

If (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + “ is a Palindrome String.”);
}
Else {
System.out.println(str + “ is not a Palindrome String.”);
}
}
}
Output

Radar is a Palindrome String.


25.Program to Check Prime Number using a for loop
Public class Main {

Public static void main(String[] args) {

Int num = 29;


Boolean flag = false;
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.”);
}
}
Output

29 is a prime number.
26.Display Prime Numbers Between two Intervals
Public class Prime {

Public static void main(String[] args) {

Int low = 20, high = 50;

While (low < high) {


Boolean flag = false;

For(int I = 2; I <= low/2; ++i) {


// condition for nonprime number
If(low % I == 0) {
Flag = true;
Break;
}
}

If (!flag && low != 0 && low != 1)


System.out.print(low + “ “);

++low;
}
}
}
Output

23 29 31 37 41 43 47
27.Check Armstrong Number for 3 digit number
Public class Armstrong {

Public static void main(String[] args) {

Int number = 371, originalNumber, remainder, result = 0;

originalNumber = number;

while (originalNumber != 0)
{
Remainder = originalNumber % 10;
Result += Math.pow(remainder, 3);
originalNumber /= 10;
}
If(result == number)
System.out.println(number + “ is an Armstrong number.”);
Else
System.out.println(number + “ is not an Armstrong number.”);
}
}
Output

371 is an Armstrong number.


28.Armstrong Numbers Between Two Integers
Class Main {
Public static void main(String[] args) {

Int low = 999, high = 99999;

For(int number = low + 1; number < high; ++number) {


Int digits = 0;
Int result = 0;
Int originalNumber = number;

// number of digits calculation


While (originalNumber != 0) {
originalNumber /= 10;
++digits;
}
originalNumber = number;

// result contains sum of nth power of its digits


While (originalNumber != 0) {
Int remainder = originalNumber % 10;
Result += Math.pow(remainder, digits);
originalNumber /= 10;
}

If (result == number) {
System.out.print(number + “ “);
}
}
}
}
Output

1634 8208 9474 54748 92727 93084


29.Prime Numbers Between Two Integers
Public class Prime {

Public static void main(String[] args) {

Int low = 20, high = 50;

While (low < high) {


If(checkPrimeNumber(low))
System.out.print(low + “ “);

++low;
}
}

Public static boolean checkPrimeNumber(int num) {


Boolean flag = true;

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

If(num % I == 0) {
Flag = false;
Break;
}
}

Return flag;
}
}
Output

23 29 31 37 41 43 47
30.Armstrong Numbers Between Two Integers
Public class Armstrong {

Public static void main(String[] args) {


Int low = 999, high = 99999;

For(int number = low + 1; number < high; ++number) {

If (checkArmstrong(number))
System.out.print(number + “ “);
}
}

Public static boolean checkArmstrong(int num) {


Int digits = 0;
Int result = 0;
Int originalNumber = num;

// number of digits calculation


While (originalNumber != 0) {
originalNumber /= 10;
++digits;
}

originalNumber = num;

// result contains sum of nth power of its digits


While (originalNumber != 0) {
Int remainder = originalNumber % 10;
Result += Math.pow(remainder, digits);
originalNumber /= 10;
}

If (result == num)
Return true;

Return false;
}
}
Output

1634 8208 9474 54748 92727 93084

You might also like