JAVA Programming for beginners
Write a program to print your name |
Write a program to read the price of an item in the decimal form (like 75.95) and print the output in paisa (like 7595) |
Write a program to convert the given temeperature in Farenheit to Celsius using the following formula C = (F – 32) × 5/9 |
Write a program to determine sum of the following series for given value of n 1-1/22 + 1/32 – ……..+1/n2 |
Write a program to find the sum of digits of a given integer number. |
Write a program to find the reverse of a given integer number. |
Write a program to find the factorial of a given integer number using recursion. |
Write a program of sum of series (1+x+x2+x3+x4+…..up to nth terms). |
Write a program to calculate the simple interest (si) while your own inputs are principle b and c are constants. |
Write a program to find the roots of the quadratic equation ax2 +bx+c=0 where a, b and c are constants. |
Write a program to show Fibonacci series up to n-th terms using recursion |
Write a print all prime number within a given range. |
Write a program to calculate GCD/HCF of two numbers. |
1. Write a program to print your name
import java.util.*;
public class PrintName {
public static void main(String[] args) {
String name;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
name=sc.nextLine();
System.out.println("My name is"+name);
}
}
2. Write a program to read the price of an item in the decimal form (like 75.95) and print the output in paisa (like 7595)
import java.util.Scanner;
public class DecimalToPaisa {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the price in decimal form (e.g., 75.95): ");
double price = scanner.nextDouble();
int paisa = (int) (price * 100);
System.out.println("Price in paisa: " + paisa);
scanner.close();
}
}
3. Write a program to convert the given temeperature in Farenheit to Celsius using the following formula
C = (F - 32) × 5/9
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = scanner.nextDouble();
double celsius = (fahrenheit - 32) * 5/9;
System.out.println("Temperature in Celsius: " + celsius);
scanner.close();
}
}
4. Write a program to determine sum of the following series for given value of n
1-1/22 + 1/32 - ……..+1/n2
import java.util.Scanner;
public class SumOfSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
double sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
sum -= 1.0 / (i * i);
} else {
sum += 1.0 / (i * i);
}
}
System.out.println("Sum of the series: " + sum);
scanner.close();
}
}
5. Write a program to find the sum of digits of a given integer number.
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer number: ");
int number = scanner.nextInt();
int sum = 0;
while (number != 0) {
sum += number % 10;
number /= 10;
}
System.out.println("Sum of digits: " + sum);
scanner.close();
}
}
6. Write a program to find the reverse of a given integer number.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer number: ");
int number = scanner.nextInt();
int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
System.out.println("Reversed number: " + reversed);
scanner.close();
}
}
7. Write a program to find the factorial of a given integer number using recursion.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int n = scanner.nextInt();
long factorial = calculateFactorial(n);
System.out.println("Factorial of " + n + " = " + factorial);
scanner.close();
}
public static long calculateFactorial(int n) {
if (n == 0) {
return 1;
} else {
return n * calculateFactorial(n - 1);
}
}
}
8. Write a program of sum of series (1+x+x2+x3+x4+…..up to nth terms).
import java.util.Scanner;
public class SumOfSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of x: ");
double x = scanner.nextDouble();
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
double sum = 0;
for (int i = 0; i <= n; i++) {
sum += Math.pow(x, i);
}
System.out.println("Sum of the series: " + sum);
scanner.close();
}
}
9. Write a program to calculate the simple interest (si) while your own inputs are principle ,rate of interest and period.
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args) {
double principal, rate, time;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the principal amount: ");
principal = scanner.nextDouble();
System.out.print("Enter the rate of interest: ");
rate = scanner.nextDouble();
System.out.print("Enter the time period (in years): ");
time = scanner.nextDouble();
double simpleInterest = (principal * rate * time) / 100;
System.out.println("Simple Interest: " + simpleInterest);
scanner.close();
}
}
10. Write a program to find the roots of the quadratic equation ax2 +bx+c=0 where a, b and c are constants.
import java.util.Scanner;
public class QuadraticEquation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = scanner.nextDouble();
System.out.print("Enter the value of b: ");
double b = scanner.nextDouble();
System.out.print("Enter the value of c: ");
double c = scanner.nextDouble();
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Root 1 = " + root1);
System.out.println("Root 2 = " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("Root = " + root);
} else {
System.out.println("Roots are imaginary.");
}
scanner.close();
}
}
11. Write a program to show Fibonacci series up to n-th terms using recursion
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
scanner.close();
}
public static int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
12. Write a print all prime number within a given range.
import java.util.Scanner;
public class PrimeNumbersInRange {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the lower bound: ");
int lowerBound = scanner.nextInt();
System.out.print("Enter the upper bound: ");
int upperBound = scanner.nextInt();
System.out.println("Prime numbers between " + lowerBound + " and " + upperBound + ":");
for (int num = lowerBound; num <= upperBound; num++) {
if (isPrime(num)) {
System.out.print(num + " ");
}
}
scanner.close();
}
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
13. Write a program to calculate GCD/HCF of two numbers.
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
int gcd = findGCD(num1, num2);
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);
scanner.close();
}
public static int findGCD(int num1, int num2) {
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
}