core java Venkatesh
CORE JAVA CHALLENGES
1) Write a program to multiply two numbers and display the result.
Code :
import java.util.Scanner;
class venkat1{
public static void main(String[] args)
{
System.out.println("Question No : 1\n\"Write a program to multiply two numbers and display the result.\"");
System.out.println("\t\t[Multiply two numbers]");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter first number a value :");
int a=sc.nextInt();
System.out.print("\nEnter secound Number b value :");
int b=sc.nextInt();
int result = a * b; // Multiply two numbers /
System.out.println("\nResult = " + result);
}
}
Output:
Question No : 1
"Write a program to multiply two numbers and display the result."
[Multiply two numbers]
Enter first number a value :5
Enter secound Number b value :6
Result = 30
2) Write a program to divide two numbers and display the quotient.
Code :
import java.util.Scanner;
class venkat2 {
public static void main(String[] args)
{
System.out.println("Question No : 2\n\"Write a program to divide two numbers and display the quotient.\"");
System.out.println("\t\t[Divide two numbers]");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter first number a value :");
int a=sc.nextInt();
System.out.print("\nEnter secound Number b value :");
int b=sc.nextInt();
double quotient = a / b; // Divide a by b /
System.out.println("Quotient = " + quotient);
}
}
Output:
Question No : 2
"Write a program to divide two numbers and display the quotient."
[Divide two numbers]
Enter first number a value :5
Enter secound Number b value :2
Quotient = 2.0
3) Write a program to find the remainder when one number is divided by another.
Code :
import java.util.Scanner;
class venkat3 {
public static void main(String[] venkat)
{
System.out.println("Question No : 3\n\"Write a program to find the remainder when one number is divided by another.\"");
System.out.println("\t\t[ Find remainder ]");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter first number a value :");
int a=sc.nextInt();
System.out.print("\nEnter secound Number b value :");
int b=sc.nextInt();
int rem = a % b; // Find remainder /
System.out.println("\nRemainder = " + rem);
}
}
Output:
Question No : 3
"Write a program to find the remainder when one number is divided by another."
[ Find remainder ]
Enter first number a value :6
Enter secound Number b value :4
Remainder = 2
4) Write a program to swap two numbers using a temporary variable.
Code :
import java.util.Scanner;
class venkat4 {
public static void main(String[] args)
{
System.out.println("Question No : 4\n\"Write a program to swap two numbers using a temporary variable.\"");
System.out.println("\t\t[Swap using temporary variable]");
int temp;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter first number a value :");
int a=sc.nextInt();
System.out.print("\nEnter secound Number b value :");
int b=sc.nextInt();
// Swap using temporary variable /
temp = a;
a = b;
b = temp;
System.out.println("\nAfter Swap: \n\ta=" + a + "\n\tb=" + b);
}
}
Output:
Question No : 4
"Write a program to swap two numbers using a temporary variable."
[Swap using temporary variable]
Enter first number a value :6
Enter secound Number b value :5
After Swap:
a=5
b=6
5) Write a program to swap two numbers without using a temporary variable.
Code :
import java.util.Scanner;
class venkat5 {
public static void main(String[] args)
{
System.out.println("Question No : 5\n\"Write a program to swap two numbers without using a temporary variable.\"");
System.out.println("\t\t[Swap without temporary variable]");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter first number a value :");
int a=sc.nextInt();
System.out.print("\nEnter secound Number b value :");
int b=sc.nextInt();
// Swap without temporary variable /
a = a + b;
b = a - b;
a = a - b;
System.out.println("\nAfter Swap: \n\ta=" + a + "\n\tb=" + b);
}
}
Output:
Question No : 5
"Write a program to swap two numbers without using a temporary variable."
[Swap without temporary variable]
Enter first number a value :2
Enter secound Number b value :6
After Swap:
a=6
b=2
6) Write a program to calculate the area of a rectangle (length × width).
Code :
import java.util.Scanner;
class venkat6
{
public static void main(String[] args)
{
System.out.println("Question No : 6\n\"Write a program to calculate the area of a rectangle (length × width).\"");
System.out.println("\t\t[Rectangle area calculation]");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the length value :");
int a=sc.nextInt();
System.out.print("\nEnter the width value :");
int b=sc.nextInt();
int area = a * b; // Area = length × width /
System.out.println("\nArea = " + area);
}
}
Output:
Question No : 6
"Write a program to calculate the area of a rectangle (length ? width)."
[Rectangle area calculation]
Enter the length value :4
Enter the width value :7
Area = 28
7) Write a program to calculate the perimeter of a rectangle.
Code :
import java.util.Scanner;
class venkat7
{
public static void main(String[] args)
{
System.out.println("Question No : 7\n\"Write a program to calculate the perimeter of a rectangle.\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the length value :");
int a=sc.nextInt();
System.out.print("\nEnter the width value :");
int b=sc.nextInt();
int perimeter = 2 * (a + b); // Perimeter = 2 × (length + width) /
System.out.println("\nPerimeter = " + perimeter);
}
}
Output:
Question No : 7
"Write a program to calculate the perimeter of a rectangle."
Enter the length value :4
Enter the width value :7
Perimeter = 22
8) Write a program to calculate the area of a circle (π × r × r).
Code :
import java.util.Scanner;
class venkat8
{
public static void main(String[] args)
{
System.out.println("Question No : 8\n\"Write a program to calculate the area of a circle (π * r * r).\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the radius r value :");
double r =sc.nextDouble();
double area = Math.PI * r * r; // Area = π × r × r /
System.out.println("\nArea ot circle = " + area);
}
}
Output:
Question No : 8
"Write a program to calculate the area of a circle (π * r * r)."
Enter the radius r value :5
Area ot circle = 78.53981633974483
9) Write a program to calculate the circumference of a circle (2 × π × r).
Code :
import java.util.Scanner;
class venkat9
{
public static void main(String[] args)
{
System.out.println("Question No : 9\n\"Write a program to calculate the circumference of a circle (2 * π * r).\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the radius r value :");
double r =sc.nextDouble();
double circumference = 2 * Math.PI * r; // Circumference = 2 × π × r /
System.out.println("\nCircumference = " + circumference);
}
}
Output:
Question No : 9
"Write a program to calculate the circumference of a circle (2 * π * r)."
Enter the radius r value :6
Circumference = 37.69911184307752
10) Write a program to calculate the average of three numbers.
Code :
import java.util.*;
class venkat10
{
public static void main(String[] args) {
System.out.println("Question No : 10\n\"Write a program to calculate the average of three numbers..\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the a value :");
int a=sc.nextInt();
System.out.print("\nEnter the b value :");
int b=sc.nextInt();
System.out.print("\nEnter the c value :");
int c=sc.nextInt();
float avg = (a + b + c) / 3; // Average = (a + b + c)/3 /
System.out.println("\nAverage = " + avg +"%");
}
}
Output:
Question No : 10
"Write a program to calculate the average of three numbers.."
Enter the a value :69
Enter the b value :67
Enter the c value :25
Average = 53.0%
11) Write a program to convert temperature from Celsius to Fahrenheit.
Code :
import java.util.*;
class venkat11
{
public static void main(String[] args)
{
System.out.println("Question No : 11\n\"Write a program to convert temperature from Celsius to Fahrenheit.\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the celcius degree:");
double c=sc.nextDouble();
double f = (c * 9/5) + 32; // Fahrenheit = (C × 9/5) + 32 /
System.out.println("\nFahrenheit = " + f +" (degree)");
}
}
Output:
Question No : 11
"Write a program to convert temperature from Celsius to Fahrenheit."
Enter the celcius degree:30
Fahrenheit = 86.0 (degree)
12) Write a program to convert temperature from Fahrenheit to Celsius.
Code :
import java.util.*;
class venkat12
{
public static void main(String[] args)
{
System.out.println("Question No : 12\n\"Write a program to convert temperature from Fahrenheit to Celsius.\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the Fahrenheit degree:");
double f=sc.nextDouble();
double c = (f - 32) * 5/9; // Celsius = (F - 32) × 5/9 /
System.out.println("\nCelsius = " + c+" (degree)");
}
}
Output:
Question No : 12
"Write a program to convert temperature from Fahrenheit to Celsius."
Enter the Fahrenheit degree:30
Celsius = -1.1111111111111112 (degree)
13) Write a program to calculate the square of a number.
Code :
import java.util.*;
class venkat13
{
public static void main(String[] args)
{
System.out.println("Question No : 13\n\"Write a program to calculate the square of a number.\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter one value:");
int a=sc.nextInt();
int square = a * a; // Square = num × num /
System.out.println("\nSquare = " + square);
}
}
Output:
Question No : 13
"Write a program to calculate the square of a number."
Enter one value:5
Square = 25
14) Write a program to calculate the cube of a number.
Code :
import java.util.Scanner;
public class venkat14
{
public static void main(String[] args)
{
System.out.println("Question No : 14\n\"Write a program to calculate the cube of a number.\"");
Scanner asc=new Scanner(System.in);
System.out.print("\nEnter one value:");
int a=asc.nextInt();
int cube= a*a*a; // Cube = a × a × a /
System.out.println("\nCube = " + cube);
}
}
Output:
Question No : 14
"Write a program to calculate the cube of a number."
Enter one value:6
Cube = 216
15) Write a program to add the digits of a 2-digit number (e.g., 47 → 4+7=11).
Code :
import java.util.*;
class venkat15
{
public static void main(String[] args)
{
System.out.println("Question No : 15\n\"Write a program to add the digits of a 2-digit number (e.g., 47 → 4+7=11).\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter one value:");
int a=sc.nextInt();
int sum = (a / 10) + (a % 10); // Sum = tens + units /
System.out.print("\nSum of digits = " + sum);
}
}
Output:
Question No : 15
"Write a program to add the digits of a 2-digit number (e.g., 47 ? 4+7=11)."
Enter one value:4
Sum of digits = 4
16) Write a program to find the simple interest (SI = P × R × T / 100).
Code :
import java.util.*;
class venkat16
{
public static void main(String[] args)
{
System.out.println("Question No : 16\n\"Write a program to find the simple interest (SI = P * R * T / 100).\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the Investment value :");
double P=sc.nextDouble();
System.out.print("\nEnter the Interest Rate :");
double R=sc.nextDouble();
System.out.print("\nEnter the Time :");
double T=sc.nextDouble();
double SI = (P * R * T) / 100; // SI = (P × R × T)/100 /
System.out.println("\nSimple Interest = " + SI);
}
}
Output:
Question No : 16
"Write a program to find the simple interest (SI = P * R * T / 100)."
Enter the Investment value :2000
Enter the Interest Rate :5
Enter the Time :30
Simple Interest = 3000.0
17) Write a program to find the total and average marks of 5 subjects.
Code :
import java.util.*;
class venkat17
{
public static void main(String[] args)
{
System.out.println("Question No : 17\n\"Write a program to find the total and average marks of 5 subjects.\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the Subject 1 mark :");
int m1=sc.nextInt();
System.out.print("\nEnter the Subject 2 mark :");
int m2=sc.nextInt();
System.out.print("\nEnter the Subject 3 mark :");
int m3=sc.nextInt();
System.out.print("\nEnter the Subject 4 mark :");
int m4=sc.nextInt();
System.out.print("\nEnter the Subject 5 mark:");
int m5=sc.nextInt();
int total = m1 + m2 + m3 + m4 + m5; // Total marks /
double avg = total / 5.0; // Average marks /
System.out.println("\nTotal = " + total);
System.out.println("\nAverage = " + avg);
}
}
Output:
Question No : 17
"Write a program to find the total and average marks of 5 subjects."
Enter the Subject 1 mark :50
Enter the Subject 2 mark :48
Enter the Subject 3 mark :60
Enter the Subject 4 mark :48
Enter the Subject 5 mark:89
Total = 295
Average = 59.0
18) Write a program to calculate the monthly salary when daily wage and working days are given.
Code :
import java.util.*;
class venkat18
{
public static void main(String[] args)
{
System.out.println("Question No : 18\n\"Write a program to calculate the monthly salary when daily wage and working days are given.\"");
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the dailywage :");
int dw=sc.nextInt();
System.out.print("\nEnter the workingDays :");
int wd=sc.nextInt();
int salary = dw* wd; // Monthly Salary = dailyWage × workingDays /
System.out.println("\nMonthly Salary = " + salary);
}
}
Output:
Question No : 18
"Write a program to calculate the monthly salary when daily wage and working days are given."
Enter the dailywage :480
Enter the workingDays :30
Monthly Salary = 14400
* * * * *
Comments
Post a Comment