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

Java Programs Complete

The document contains a collection of Java programs with solutions, covering basic programming concepts such as input/output, arithmetic operations, control structures, and mathematical calculations. Each program is presented with its code and a brief description of its functionality, including tasks like adding numbers, checking for prime numbers, and converting temperatures. This serves as a comprehensive guide for beginners to learn Java programming through practical examples.

Uploaded by

jhalina3255
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 views5 pages

Java Programs Complete

The document contains a collection of Java programs with solutions, covering basic programming concepts such as input/output, arithmetic operations, control structures, and mathematical calculations. Each program is presented with its code and a brief description of its functionality, including tasks like adding numbers, checking for prime numbers, and converting temperatures. This serves as a comprehensive guide for beginners to learn Java programming through practical examples.

Uploaded by

jhalina3255
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

Java Programs with Solutions (Complete Version)

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

2. Add Two Numbers


import [Link];
public class AddTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link](), b = [Link]();
[Link]("Sum: " + (a + b));
}
}

3. Swap Two Numbers


public class Swap {
public static void main(String[] args) {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
[Link]("a=" + a + ", b=" + b);
}
}

4. Check Even or Odd


import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num = [Link]();
[Link](num % 2 == 0 ? "Even" : "Odd");
}
}

5. Positive or Negative
import [Link];
public class PosNeg {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
if (n > 0) [Link]("Positive");
else if (n < 0) [Link]("Negative");
else [Link]("Zero");
}
}

6. Largest of Three Numbers


import [Link];
public class Largest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link](), b = [Link](), c = [Link]();
int max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
[Link]("Largest: " + max);
}
}

7. Multiplication Table
import [Link];
public class Table {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= 10; i++)
[Link](n + " x " + i + " = " + (n * i));
}
}

8. Factorial
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), fact = 1;
for (int i = 1; i <= n; i++) fact *= i;
[Link]("Factorial: " + fact);
}
}

9. Fibonacci Series
import [Link];
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), a = 0, b = 1, c;
[Link](a + " " + b);
for (int i = 2; i < n; i++) {
c = a + b;
[Link](" " + c);
a = b;
b = c;
}
}
}

10. Prime Number


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

11. Reverse a Number


import [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num = [Link](), rev = 0;
while(num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
[Link]("Reversed: " + rev);
}
}

12. Palindrome Number


import [Link];
public class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), r, sum = 0, temp = n;
while(n > 0){
r = n % 10;
sum = sum * 10 + r;
n /= 10;
}
[Link](temp == sum ? "Palindrome" : "Not Palindrome");
}
}
13. Armstrong Number
import [Link];
public class Armstrong {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), temp = n, sum = 0, r;
while(n > 0){
r = n % 10;
sum += [Link](r, 3);
n /= 10;
}
[Link](temp == sum ? "Armstrong" : "Not Armstrong");
}
}

14. Sum of Digits


import [Link];
public class SumDigits {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), sum = 0;
while(n != 0) {
sum += n % 10;
n /= 10;
}
[Link]("Sum: " + sum);
}
}

15. Count Digits


import [Link];
public class CountDigits {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), count = 0;
while(n != 0){
count++;
n /= 10;
}
[Link]("Digits: " + count);
}
}

16. Simple Interest


import [Link];
public class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
float p = [Link](), r = [Link](), t = [Link]();
float si = (p * r * t) / 100;
[Link]("Simple Interest: " + si);
}
}

17. Leap Year


import [Link];
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int year = [Link]();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
[Link]("Leap Year");
else
[Link]("Not Leap Year");
}
}

18. ASCII Value


import [Link];
public class ASCII {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
char ch = [Link]().charAt(0);
[Link]("ASCII: " + (int)ch);
}
}

19. Area of Circle


import [Link];
public class CircleArea {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double r = [Link]();
[Link]("Area: " + ([Link] * r * r));
}
}

20. Celsius to Fahrenheit


import [Link];
public class TempConvert {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double c = [Link]();
double f = (c * 9/5) + 32;
[Link]("Fahrenheit: " + f);
}
}

You might also like