0% found this document useful (0 votes)
4 views10 pages

Java Programs

Uploaded by

shinieee1234
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)
4 views10 pages

Java Programs

Uploaded by

shinieee1234
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

1.

Write a Java Program to Find the


Largest of Two Numbers
Program
import [Link];

public class LargestNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");


int a = [Link]();

[Link]("Enter second number: ");


int b = [Link]();

if (a > b)
[Link]("Largest number = " + a);
else
[Link]("Largest number = " + b);

[Link]();
}
}

Output
Enter first number: 25
Enter second number: 15
Largest number = 25
2. Write a Java Program to Check Whether
a Number is Even or Odd
Program
import [Link];

public class EvenOdd {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();

if (num % 2 == 0)
[Link]("Even Number");
else
[Link]("Odd Number");

[Link]();
}
}

Output
Enter a number: 12
Even Number

3(a). Write a Java Program to Swap Two Numbers Using


Third Variable
Program
import [Link];

public class SwapUsingThirdVariable {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");


int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();

int temp = a;
a = b;
b = temp;

[Link]("After Swapping:");
[Link]("a = " + a);
[Link]("b = " + b);

[Link]();
}
}

Output
Enter first number: 10
Enter second number: 20

After Swapping:
a = 20
b = 10

3(b). Write a Java Program to Swap Two Numbers Without


Using Third Variable
Program
import [Link];

public class SwapWithoutThirdVariable {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");


int a = [Link]();

[Link]("Enter second number: ");


int b = [Link]();

a = a + b;
b = a - b;
a = a - b;
[Link]("After Swapping:");
[Link]("a = " + a);
[Link]("b = " + b);

[Link]();
}
}

output
Enter first number: 15
Enter second number: 25

After Swapping:
a = 25
b = 15

4. Write a Java Program to Find the Factorial of a Number


Program
import [Link];

public class Factorial {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int n = [Link]();

long fact = 1;

for (int i = 1; i <= n; i++) {


fact = fact * i;
}

[Link]("Factorial = " + fact);

[Link]();
}
}
Output
Enter a number: 5
Factorial = 120

5. Write a Java Program to Generate Fibonacci Series


Program
import [Link];

public class Fibonacci {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter number of terms: ");


int n = [Link]();

int a = 0;
int b = 1;

[Link]("Fibonacci Series: ");

for (int i = 1; i <= n; i++) {


[Link](a + " ");

int c = a + b;
a = b;
b = c;
}

[Link]();
}
}

Output
Enter number of terms: 6
Fibonacci Series: 0 1 1 2 3 5
6. Write a Java Program to Check Whether a Number is
Prime
Program
import [Link];

public class PrimeNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int n = [Link]();

boolean isPrime = true;

if (n <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime)
[Link]("Prime Number");
else
[Link]("Not a Prime Number");

[Link]();
}
}

Output
Enter a number: 13
Prime Number
7. Write a Java Program to Reverse a Number
Program
import [Link];

public class ReverseNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();

int reverse = 0;

while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}

[Link]("Reversed Number = " + reverse);

[Link]();
}
}

Output
Enter a number: 1234
Reversed Number = 4321

8. Write a Java Program to Check Whether a Number is


Armstrong
Program
import [Link];

public class ArmstrongNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();

int original = num;


int sum = 0;

while (num != 0) {
int digit = num % 10;
sum = sum + (digit * digit * digit);
num = num / 10;
}

if (sum == original)
[Link]("Armstrong Number");
else
[Link]("Not an Armstrong Number");

[Link]();
}
}

Output
Enter a number: 153
Armstrong Number

9. Write a Java Program to Generate Multiplication Table


Program
import [Link];

public class MultiplicationTable {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int n = [Link]();

for (int i = 1; i <= 10; i++) {


[Link](n + " x " + i + " = " + (n * i));
}

[Link]();
}
}

Output
Enter a number: 5

5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

10. Write a Java Program to Display a Star Pattern Using


Loops
Program
public class StarPattern {
public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {


[Link]("* ");
}

[Link]();
}
}
}

Output
*
**
***
****
*****

You might also like