Java Sample Problem
Java Sample Problem
Java Sample Problem
Mugundhan
2nd B.SC.IT
JAVA PROGRAMMING
1.How to Print an Integer entered by an user
Import java.util.Scanner;
Char ch = ‘a’;
Int ascii = ch;
// You can also cast char to int
Int castAscii = (int) ch;
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
System.out.println(“—Before swap—“);
System.out.println(“First number = “ + first);
System.out.println(“Second number = “ + 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
Enter a number: 12
12 is even
8.Java with Programiz PRO!
Claim Discount Now
Programiz
Search…
Programiz PRO
Char ch = ‘I’;
}
}
Output
I is vowel
10. Java Program to Find Roots of a Quadratic Equation
Public class Main {
// value a, b, and c
Double a = 2.3, b = 4, c = 5.6;
Double root1, root2;
// year to be checked
Int year = 1900;
Boolean leap = false;
Else
Leap = false;
If (leap)
System.out.println(year + “ is a leap year.”);
Else
System.out.println(year + “ is not a leap year.”);
}
}
Output
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.
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) {
Factorial of 10 = 3628800
16. Generate Multiplication Table using for loop
Public class MultiplicationTable {
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) {
// 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
ABCDEFGHIJKLMNOPQRSTUVWXYZ
21.Count Number of Digits in an Integer using while loop
Public class Main {
While (num != 0) {
// num = num/10
Num /= 10;
++count;
}
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) {
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
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 {
++low;
}
}
}
Output
23 29 31 37 41 43 47
27.Check Armstrong Number for 3 digit number
Public class Armstrong {
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
If (result == number) {
System.out.print(number + “ “);
}
}
}
}
Output
++low;
}
}
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 {
If (checkArmstrong(number))
System.out.print(number + “ “);
}
}
originalNumber = num;
If (result == num)
Return true;
Return false;
}
}
Output