0% found this document useful (0 votes)
18 views4 pages

Basic Java Programs

Va

Uploaded by

fathimackm2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Basic Java Programs

Va

Uploaded by

fathimackm2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Basic Java Programs with Solutions

1. Hello World
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Output: Hello, World!

2. Sum of Two Numbers


import [Link];
class Sum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
[Link]("Sum: " + (a + b));
}
}
Input: 5 7 Output: Sum: 12

3. Check Even or Odd


import [Link];
class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num = [Link]();
if(num % 2 == 0)
[Link](num + " is Even");
else
[Link](num + " is Odd");
}
}
Input: 9 Output: 9 is Odd

4. Factorial of a Number
import [Link];
class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
long fact = 1;
for(int i=1; i<=n; i++){
fact *= i;
}
[Link]("Factorial: " + fact);
}
}
Input: 5 Output: Factorial: 120

5. Reverse a Number
import [Link];
class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int rev = 0;
while(n != 0) {
rev = rev*10 + n%10;
n = n/10;
}
[Link]("Reversed: " + rev);
}
}
Input: 1234 Output: Reversed: 4321

6. Palindrome Check
import [Link];
class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String str = [Link]();
String rev = new StringBuilder(str).reverse().toString();
if([Link](rev))
[Link](str + " is Palindrome");
else
[Link](str + " is Not Palindrome");
}
}
Input: madam Output: madam is Palindrome

7. Fibonacci Series
import [Link];
class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int a = 0, b = 1;
[Link]("Fibonacci: ");
for(int i=1; i<=n; i++){
[Link](a + " ");
int next = a + b;
a = b;
b = next;
}
}
}
Input: 6 Output: Fibonacci: 0 1 1 2 3 5

8. Largest of Three Numbers


import [Link];
class Largest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int c = [Link]();
int max = a;
if(b > max) max = b;
if(c > max) max = c;
[Link]("Largest: " + max);
}
}
Input: 12 7 15 Output: Largest: 15

9. Prime Number Check


import [Link];
class Prime {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
boolean prime = true;
if(n <= 1) prime = false;
else {
for(int i=2; i<=n/2; i++){
if(n % i == 0){
prime = false;
break;
}
}
}
if(prime)
[Link](n + " is Prime");
else
[Link](n + " is Not Prime");
}
}
Input: 17 Output: 17 is Prime

10. Simple Calculator


import [Link];
class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double a = [Link]();
double b = [Link]();
char op = [Link]().charAt(0);
double result;
switch(op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/': result = a / b; break;
default: [Link]("Invalid operator"); return;
}
[Link]("Result: " + result);
}
}
Input: 10 5 * Output: Result: 50.0

You might also like