Important Java Programs for Beginners | Grade, Fibonacci, Armstrong, Pattern & More
Chapter 1: Programs
Question 1: WAP to check if a number input by the user is even or odd.
Solution:
import java.util.Scanner; class EvenOdd { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int n = sc.nextInt(); if(n % 2 == 0) System.out.println("Even Number"); else System.out.println("Odd Number"); } }
Input: n=8
Output: Even Number
Question 2: WAP to check if a letter input by user is vowel or consonant.
Solution:
import java.util.Scanner;
class VowelConsonant
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a letter: ");
char ch = sc.next().charAt(0);
ch = Character.toLowerCase(ch);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
System.out.println("Vowel");
else
System.out.println("Consonant");
}
}
Input: ch='S'
Output: ConsonantQuestion 3: WAP to swap values of two variables without creating third variable.
Solution:
import java.util.Scanner; class SwapWithoutThird { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); int a = sc.nextInt(); System.out.print("Enter second number: "); int b = sc.nextInt(); a = a + b; b = a - b; a = a - b; System.out.println("After Swapping:"); System.out.println("First Number = " + a); System.out.println("Second Number = " + b); } }Input: a=10,b=20
Output: After Swapping:
First Number: 20
Second number: 10
Question 4: Write a function in Java that accepts length, height and width of a cuboid and returns the surface area and volume.
Solution:
import java.util.Scanner; public class Cuboid { double surfaceArea(double l, double h, double w) { return 2 * (l * w + w * h + h * l); } double volume(double l, double h, double w) { return l * h * w; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); Cuboid obj = new Cuboid(); System.out.print("Enter Length: "); double length = sc.nextDouble(); System.out.print("Enter Height: "); double height = sc.nextDouble(); System.out.print("Enter Width: "); double width = sc.nextDouble(); double sa = obj.surfaceArea(length, height, width); double vol = obj.volume(length, height, width); System.out.println("Surface Area = " + sa); System.out.println("Volume = " + vol); } }Input: Enter Length: 5
Enter Height: 4
Enter Width: 3
Output: Surface Area = 94.0
Volume = 60.0
Question 5: WAP that takes principal amount, interest rate and duration as input from user and print the simple interest.
Solution:
import java.util.Scanner; public class SimpleInterest { public static void main(String args[]) { Scanner sc = new Scanner(System.in); double p, r, t, si; System.out.print("Enter Principal Amount: "); p = sc.nextDouble(); System.out.print("Enter Rate of Interest: "); r = sc.nextDouble(); System.out.print("Enter Time Duration: "); t = sc.nextDouble(); si = (p * r * t) / 100; System.out.println("Simple Interest = " + si); } }Input: Enter Principal Amount: 10000
Enter Rate of Interest: 5
Enter Time Duration: 2
Output: Simple Interest = 1000.0
Question 6: WAP that takes total marks and obtained marks of student and print the grade A++, A+, A, B+, B, C, D according to following slab.
A++ >90
A+ >75
A >60
B+ >50
B >45
C >35
D <35
Solution:
import java.util.Scanner; public class GradeCalculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter total marks: "); double total = sc.nextDouble(); System.out.print("Enter obtained marks: "); double obtained = sc.nextDouble(); double percentage = (obtained / total) * 100; System.out.println("Percentage: " + percentage); if (percentage > 90) { System.out.println("Grade: A++"); } else if (percentage > 75) { System.out.println("Grade: A+"); } else if (percentage > 60) { System.out.println("Grade: A"); } else if (percentage > 50) { System.out.println("Grade: B+"); } else if (percentage > 45) { System.out.println("Grade: B"); } else if (percentage > 35) { System.out.println("Grade: C"); } else { System.out.println("Grade: D"); } sc.close(); } }
Input: Enter total marks: 500
Enter obtained marks: 460
Percentage: 92.0
Output: Grade: A++
Question 7: Write a calculator program that takes as input two numbers and one operator to perform mathematical operation and prints the calculated result.
Solution:
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = sc.nextDouble(); System.out.print("Enter second number: "); double num2 = sc.nextDouble(); System.out.print("Enter operator (+, -, *, /): "); char op = sc.next().charAt(0); double result; switch(op) { case '+': result = num1 + num2; System.out.println("Result = " + result); break; case '-': result = num1 - num2; System.out.println("Result = " + result); break; case '*': result = num1 * num2; System.out.println("Result = " + result); break; case '/': if (num2 != 0) { result = num1 / num2; System.out.println("Result = " + result); } else { System.out.println("Error: Division by zero"); } break; default: System.out.println("Invalid operator"); } sc.close(); } }
Input: Enter first number: 10
Enter second number: 5
Enter operator (+, -, *, /): *
Output: Result = 50.0
Question 8: WAP in Java to display fibonacci series up to 10 numbers.
Solution:
public class Fibonacci { public static void main(String[] args) { int n = 10; int a = 0, b = 1; System.out.println("Fibonacci Series up to 10 numbers:"); for (int i = 1; i <= n; i++) { System.out.print(a + " "); int next = a + b; a = b; b = next; } } }0 1 1 2 3 5 8 13 21 34
Output: Fibonacci Series up to 10 numbers:
Question 9: WAP to check if the given number is a palindrome number.
Solution:
import java.util.Scanner; public class PalindromeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int num = sc.nextInt(); int original = num; int reverse = 0; while (num != 0) { int digit = num % 10; reverse = reverse * 10 + digit; num = num / 10; } if (original == reverse) { System.out.println("Palindrome Number"); } else { System.out.println("Not a Palindrome Number"); } sc.close(); } }
Input: Enter a number: 121
Output: Palindrome Number
Question 10: WAP to check if the given number is an armstrong number.
Solution:
import java.util.Scanner; public class ArmstrongNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int num = sc.nextInt(); int original = num; int sum = 0; int digits = 0; // Count digits int temp = num; while (temp != 0) { digits++; temp /= 10; } temp = num; // Calculate sum of digits raised to power while (temp != 0) { int digit = temp % 10; sum += Math.pow(digit, digits); temp /= 10; } if (sum == original) { System.out.println("Armstrong Number"); } else { System.out.println("Not an Armstrong Number"); } sc.close(); } }
Input: Enter a number: 123
Output: Not an Armstrong Number
Question 11: Write a method which accepts three number and return average.
Solution:
public class AverageCalculator { public static double findAverage(double a, double b, double c) { return (a + b + c) / 3; } public static void main(String[] args) { double result = findAverage(10, 20, 30); System.out.println("Average = " + result); } }
Output: Average = 20.0
Question 12: WAP to produce following output.
***************
Solution:
public class StarPattern { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { // rows for (int j = 1; j <= i; j++) { // columns System.out.print("*"); } System.out.println(); // move to next line } } }
Output:
* ** *** **** *****
Question 13: Design a class Student which stores data about students of your class like their name, test marks, grade etc. and methods to operate on the data.
Solution:
import java.util.Scanner; public class Student { String name; int marks; char grade; // Method to input data public void input() { Scanner sc = new Scanner(System.in); System.out.print("Enter name: "); name = sc.nextLine(); System.out.print("Enter marks: "); marks = sc.nextInt(); } // Method to calculate grade public void calculateGrade() { if (marks > 90) { grade = 'A'; } else if (marks > 75) { grade = 'B'; } else if (marks > 60) { grade = 'C'; } else if (marks > 50) { grade = 'D'; } else { grade = 'F'; } } // Method to display data public void display() { System.out.println("Name: " + name); System.out.println("Marks: " + marks); System.out.println("Grade: " + grade); } public static void main(String[] args) { Student s = new Student(); s.input(); s.calculateGrade(); s.display(); } }
Input: Enter name: Sumit
Enter marks: 82
Output: Name: Sumit
Marks: 82
Grade: B
Comments
Post a Comment