1.
Add Two Numbers
import [Link];
class AddNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first number: ");
int num1 = [Link]();
[Link]("Enter the second number: ");
int num2 = [Link]();
int sum = num1 + num2;
[Link]("The sum of " + num1 + " and " + num2 + " is: " + sum);
}
}
2. Fibonacci Series
import [Link];
class FibonacciSeries{
public static void main(String[] args){
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of terms in the Fibonacci series: ");
int n = [Link]();
int a = 0, b = 1;
[Link]("Fibonacci Series:");
for(int i = 1; i <= n; i++){
[Link](a + " ");
int next = a + b;
a = b;
b = next;
}
}
}
3. Print Your Name 10 Times
import [Link];
class LoopName{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
int i = 0;
while (i < 10) {
[Link](name);
i++;
}
[Link]();
}
}
4. Factorial of a Number
import [Link];
class Factorial{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
[Link]("Factorial of " + num + " is: " + factorial);
}
}
5. Check Even or Odd
import [Link];
class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
if (number % 2 == 0) {
[Link](number + " is an even number.");
} else {
[Link](number + " is an odd number.");
}
[Link]();
}
}
6. Largest of Three Numbers
import [Link];
class LargestNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first number: ");
int num1 = [Link]();
[Link]("Enter the second number: ");
int num2 = [Link]();
[Link]("Enter the third number: ");
int num3 = [Link]();
int largest = findLargest(num1, num2, num3);
[Link]("The largest number is: " + largest);
}
public static int findLargest(int a, int b, int c) {
if (a >= b && a >= c) {
return a;
} else if (b >= a && b >= c) {
return b;
} else {
return c;
}
}
}
7. Reverse a Number
import [Link];
class ReverseNum{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int rev = 0;
while(num != 0){
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
[Link]("Reversed number: " + rev);
}
}
8. Multiplication Table
import [Link];
class MultTable {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number to display its multiplication table: ");
int number = [Link]();
[Link]("Multiplication Table of " + number + ":");
for (int i = 1; i <= 10; i++) {
[Link](number + " x " + i + " = " + (number * i));
}
[Link]();
}
}
9. Sum of First N Natural Numbers
import [Link];
class FirstNaturalNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a positive integer: ");
int n = [Link]();
[Link]("The first " + n + " natural numbers are:");
for (int i = 1; i <= n; i++) {
[Link](i + " ");
}
[Link]();
}
}
10. Swap Two Numbers
import [Link];
class SwapNum{
public static void main(String args[]){
int a, b;
Scanner sc = new Scanner([Link]);
[Link]("Enter the value of a and b:");
a = [Link]();
b = [Link]();
[Link]("Before swapping: a = " + a + " and b = " + b);
a+=b;
b=a-b;
a=a-b;
[Link]("After swapping: a = " + a + " and b = " + b);
}
}