0% found this document useful (0 votes)
29 views

All Programming Codding Solution Fo

The document provides solutions to various Java programming problems involving input/output operations, mathematical calculations, conditional statements, loops and more. Multiple solutions are given for each problem using different Java concepts like switch statements, methods of swapping numbers, finding greatest among three numbers etc.

Uploaded by

Vikash Ku Vicky
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

All Programming Codding Solution Fo

The document provides solutions to various Java programming problems involving input/output operations, mathematical calculations, conditional statements, loops and more. Multiple solutions are given for each problem using different Java concepts like switch statements, methods of swapping numbers, finding greatest among three numbers etc.

Uploaded by

Vikash Ku Vicky
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

All Programming Codding Solution for Java Promming Language

Q No.1. WAP to input two numbers and find its sum.

Solution :-

import java.util.Scanner;

public class SumCalculator


{
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 sum = num1 + num2;

System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);

scanner.close();
}
}

Q NO.2. WAP to input principal amount, rate and time. Calculate its simple interest
and compound interest.

Solution :-

import java.util.Scanner;

public class InterestCalculator


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the principal amount: ");


double principal = scanner.nextDouble();

System.out.print("Enter the rate of interest: ");


double rate = scanner.nextDouble();

System.out.print("Enter the time period (in years): ");


double time = scanner.nextDouble();

double simpleInterest = (principal * rate * time) / 100;

double compoundInterest = principal * Math.pow((1 + rate / 100), time) -


principal;

System.out.println("Simple Interest: " + simpleInterest);


System.out.println("Compound Interest: " + compoundInterest);
scanner.close();
}
}

Q NO.3.WAP to input the radius of any circle and find its area and perimeter.

Solution :-
import java.util.Scanner;

public class Circle


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the radius of the circle: ");


double radius = scanner.nextDouble();

// Calculate area
double area = Math.PI * radius * radius;

// Calculate perimeter (circumference)


double perimeter = 2 * Math.PI * radius;

System.out.println("Area of the circle: " + area);


System.out.println("Perimeter of the circle: " + perimeter);
}
}

Q NO.4.WAP to input a number and check it is positive or negative.

Solution :-

import java.util.Scanner;

public class NumberCheck


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = input.nextInt();

if (number > 0)
{
System.out.println("The number is positive.");
}
else if (number < 0)
{
System.out.println("The number is negative.");
}
else
{
System.out.println("The number is zero.");
}

input.close();
}
}

Q NO.5.WAP to input the marks of three subject (Out of 100) and calculate its
percentage, total marks,total distinction in subjects and display fail if he/she
gets marks less than 30 in any one of the subjects.

Solution :-

import java.util.Scanner;

public class MarksCalculator


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter marks for subject 1: ");


int subject1 = input.nextInt();

System.out.print("Enter marks for subject 2: ");


int subject2 = input.nextInt();

System.out.print("Enter marks for subject 3: ");


int subject3 = input.nextInt();

int totalMarks = subject1 + subject2 + subject3;


double percentage = (totalMarks / 300.0) * 100;

System.out.println("Total marks: " + totalMarks);


System.out.println("Percentage: " + percentage + "%");

if (subject1 >= 30 && subject2 >= 30 && subject3 >= 30)


{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}

input.close();
}
}

Q NO.6.WAP to input three numbers and find the greatest


(i) By using nested if statement
(ii) By using Ternary Operator

Solution :- Here's the Java code for finding the greatest of three numbers using
both nested if statements and the ternary operator:
(i) Using nested if statements:

import java.util.Scanner;

public class GreatestNumber

{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.println("Enter three numbers: ");


int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();

int greatest;

if (num1 > num2)


{
if (num1 > num3)
{
greatest = num1;
}
else
{
greatest = num3;
}
}
else

{
if (num2 > num3)
{
greatest = num2;
}
else
{
greatest = num3;
}
}

System.out.println("The greatest number is: " + greatest);


}
}

Solution :-(ii) Using the ternary operator:

import java.util.Scanner;

public class GreatestNumber


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.println("Enter three numbers: ");


int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();

int greatest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 >
num3) ? num2 : num3);

System.out.println("The greatest number is: " + greatest);


}
}

Q NO.7. WAP to input two numbers and perform addition, substraction and
multiplication by using switch statement.

Solution :- A Java program that takes two numbers as input and performs addition,
subtraction, and multiplication using a switch statement: java

import java.util.Scanner;

public class Calculator


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");


double num1 = scanner.nextDouble();

System.out.print("Enter the second number: ");


double num2 = scanner.nextDouble();

System.out.print("Enter the operation (+ for addition, - for subtraction, *


for multiplication): ");
char operation = scanner.next().charAt(0);

double result = 0.0;

switch (operation)
{
case '+':
result = num1 + num2;
System.out.println("The addition of " + num1 + " and " + num2 + "
is: " + result);
break;
case '-':
result = num1 - num2;
System.out.println("The subtraction of " + num1 + " and " + num2 +
" is: " + result);
break;
case '*':
result = num1 * num2;
System.out.println("The multiplication of " + num1 + " and " + num2
+ " is: " + result);
break;
default:
System.out.println("Invalid operation!");
break;
}

scanner.close();
}
}

Q NO.7.WAP to display odd and even numbers between 5 to 50 and also display its sum
separately.

Solution :- A Java program that displays the odd and even numbers between 5 and 50,
along with their respective sums:

public class OddEvenNumbers


{
public static void main(String[] args)
{
int start = 5;
int end = 50;
int oddSum = 0;
int evenSum = 0;

System.out.println("Odd Numbers:");
for (int i = start; i <= end; i++)
{
if (i % 2 != 0)
{
System.out.print(i + " ");
oddSum += i;
}
}
System.out.println("\nSum of odd numbers: " + oddSum);

System.out.println("\nEven Numbers:");
for (int i = start; i <= end; i++)
{
if (i % 2 == 0)
{
System.out.print(i + " ");
evenSum += i;
}
}
System.out.println("\nSum of even numbers: " + evenSum);
}
}

Q NO.8. WAP to display and the sum of all numbers between 15 to 100, which is
divisible by 7.

Solution :-A Java program that displays the numbers between 15 and 100 that are
divisible by 7 and calculates their sum:
public class DivisibleBySeven
{
public static void main(String[] args)
{
int start = 15;
int end = 100;
int sum = 0;

System.out.println("Numbers divisible by 7 between " + start + " and " +


end + ":");

for (int i = start; i <= end; i++)


{
if (i % 7 == 0)
{
System.out.println(i);
sum += i;
}
}

System.out.println("Sum of the numbers: " + sum);


}
}

Q NO.9. WAP to input two numbers and swap them


(I)By using third variable.
(II)Without using third variable (Two methods)

Solution:- A Java program that allows you to input two numbers and swap them using
different methods:

import java.util.Scanner;

public class SwapNumbers


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter the first number: ");


int num1 = input.nextInt();

System.out.print("Enter the second number: ");


int num2 = input.nextInt();

int temp;
temp = num1;
num1 = num2;
num2 = temp;

System.out.println("After swapping:");
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
}
}

import java.util.Scanner;
public class SwapNumbers
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter the first number: ");


int num1 = input.nextInt();

System.out.print("Enter the second number: ");


int num2 = input.nextInt();

num1 = num1 + num2;


num2 = num1 - num2;
num1 = num1 - num2;

System.out.println("After swapping:");
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
}
}

import java.util.Scanner;

public class SwapNumbers


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter the first number: ");


int num1 = input.nextInt();

System.out.print("Enter the second number: ");


int num2 = input.nextInt();

num1 = num1 ^ num2;


num2 = num1 ^ num2;
num1 = num1 ^ num2;

System.out.println("After swapping:");
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
}
}

Q NO.10. WAP to input a number and find all the prime number between 5 to 100.

Solution:-import java.util.Scanner;

public class PrimeNumbers


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

System.out.println("Prime numbers between 5 and 100:");


for (int i = 5; i <= 100; i++)
{
if (isPrime(i))
{
System.out.print(i + " ");
}
}
}

// Function to check if a number is prime


public static boolean isPrime(int number)
{
if (number <= 1)
{
return false;
}

for (int i = 2; i <= Math.sqrt(number); i++)


{
if (number % i == 0
{
return false;
}
}

return true;
}
}

You might also like