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

IX-SANSKAR_COMPUTER

The document contains a series of Java programming exercises that cover various concepts such as finding the second largest number, swapping values without a third variable, printing even numbers, checking for perfect squares, and calculating areas and perimeters of shapes. Each exercise includes a code snippet, input prompts, and expected output. The exercises are designed to enhance programming skills and understanding of basic algorithms.

Uploaded by

officialrudra527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

IX-SANSKAR_COMPUTER

The document contains a series of Java programming exercises that cover various concepts such as finding the second largest number, swapping values without a third variable, printing even numbers, checking for perfect squares, and calculating areas and perimeters of shapes. Each exercise includes a code snippet, input prompts, and expected output. The exercises are designed to enhance programming skills and understanding of basic algorithms.

Uploaded by

officialrudra527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Q1.

Write a program to input 3 integer


values and print the second largest
number using Math functions.
import java.util.*;

class secondlargest

{
public static void main(String args[])

{
Scanner s1 = new Scanner(System.in);

int a, b, c, maxi, mini, total, scndlrgst;

System.out.println("Enter the three integer Numbers");

System.out.print("Enter First Number: ");

a = s1.nextInt();

System.out.print("Enter Second Number: ");

b = s1.nextInt();

System.out.print("Enter Third Number: ");

c = s1.nextInt();

maxi =(Math.max(a,Math.max(b,c)));

mini =(Math.min(a,Math.min(b,c)));

total = a+b+c;

scndlrgst = total - maxi - mini ;

System.out.println("The Second Largest Number = " +scndlrgst);


}

}
OUTPUT :

Q2. Write a program to input 2 integers


and swap their values without using third
variable. Display the values before and
after swapping.
import java.util.*;
public class SwapWithoutThirdVariable
{
public static void main(String[] args)
{
int n1, n2;
Scanner s1 = new Scanner(System.in);
System.out.print("Enter the first integer:");
n1 = s1.nextInt();
System.out.print("Enter the second integer:");
n2 = s1.nextInt();
System.out.println("BEFORE SWAPPING :--");
System.out.println("First integer :" + n1);
System.out.println("Second integer:" + n2);
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
System.out.println("AFTER SWAPPING :--");
System.out.println("First integer :" + n1);
System.out.println("Second integer:" + n2);
}
}

OUTPUT :
Q3. Write a program to print first 10 even
numbers .

class FirstTenEvenNumbers
{
public static void main(String[] args)
{
System.out.println("The FIRST 10 Even Numbers are:");
for (int i=1; i<=10; i++)
{
int even_num = 2 * i;
System.out.println(even_num);
}
}
}
OUTPUT:
Q4. Write a program to input a double
variable and check whether a number is a
perfect square or not.
import java.util.*;

class perfect_square

public static void main(String[] args)

Scanner s1 = new Scanner(System.in);

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

double num = s1.nextDouble();

double sqrt = Math.sqrt(num);

if(sqrt==Math.floor(sqrt))

System.out.println(num+" is a perfect square.");

else

System.out.println(num+ " is not a perfect square.");

}
OUTPUT:

Q5. Write a program that implements a


rectangle class has fields: length, breadth,
area and perimeter. It has to obtain values of
length and breadth, to calculate area and
perimeter.
import java.util.*;

class Rectangle

public static void main(String[]args)

Scanner s1= new Scanner(System.in);

System.out.print("Enter the length of Rectangle: ");

double l= s1.nextDouble();

System.out.print("Enter the breadth of Rectangle: ");

double b= s1.nextDouble();

System.out.println("length: "+l);

System.out.println("breadth: "+b);

double peri= 2*(l+b);

System.out.println("Perimeter: "+peri);

double ar= l*b;

System.out.println("Area: "+ar);

}
OUTPUT:
Q6. Write a program to input range M and
N and display all the prime numbers
between this range (both M and N
inclusive).

import java.util.Scanner;
class Prime_num_range
{
public static void main(String[]args)
{
Scanner s1 = new Scanner(System.in);
System.out.print("Enter the starting range (M): ");
int M = s1.nextInt();
System.out.print("Enter the ending range (N): ");
int N = s1.nextInt();
for(int i=M; i<=N; i++)
{
int count = 0;
int num = i;
for(int j=1; j<=i; j++)
{
if(i%j==0)
count++;
}
if(count==2)
System.out.println(num+"");
}
}
}
OUTPUT:
Q7. Write a menu driven program to input 2
numbers and print the result according to
the choice . Choice must be input as char
that inputs arithmetical operators to
complete the task.

import java.util.*;
class Operator
{
public static void main(String[] args)
{
Scanner s1 = new Scanner(System.in);
System.out.print("Enter first number :");
double num1 = s1.nextDouble();
System.out.print("Enter second number:");
double num2 = s1.nextDouble();
System.out.println("Choose an operation:");
System.out.println(" + : Addition");
System.out.println(" - : Subtraction");
System.out.println(" x : Multiply");
System.out.println(" / : Division");

System.out.print("Enter your choice (+, -, x or /): ");


char choice = s1.next().charAt(0);
switch (choice)
{
case '+':
System.out.println("Result: " + (num1 + num2));
break;
case '-':
System.out.println("Result: " + (num1 - num2));
break;
case 'x':
System.out.println("Result: " + (num1*num2));
break;
case '/':
System.out.println("Result: " + (num1/num2));
break;
default:
System.out.println("Error: Invalid choice. Please choose an
appropriate operator”);
}
}
}

OUTPUT:
Q8. A

salesman working in a company “Mehra and


Sons” gets the commission on the sales
done by him depending on the following
conditions:
SALES COMMISSION
Upto RS 10000 10%
<=20000 15%
<=50000 30%
>50000 40%

import java.util.*;
class Sales_Commission
{
public static void main(String[] args)
{
Scanner s1 = new Scanner(System.in);
System.out.print("Enter the total sales amount (in RS): ");
double sales = s1.nextDouble();
if (sales <= 10000)
{
System.out.println("Total Sales: "+sales);
System.out.println("Commission Rate = 10%");
System.out.println("Commission amount = "+sales*0.10);
}
else if (sales <= 20000)

{
System.out.println("Total Sales: "+sales);
System.out.println("Commission Rate = 15%");
System.out.println("Commission amount = "+sales*0.15);}
else if (sales <= 50000)
{
System.out.println("Total Sales: "+sales);
System.out.println("Commission Rate = 30%");
System.out.println("Commission amount = "+sales*0.30);}
else
{
System.out.println("Total Sales: RS "+sales);
System.out.println("Commission Rate: 40%");
System.out.println("Commission Earned: "+sales*0.40);
}
}
}
OUTPUT:

Q9. Write a class that inputs the weight in


kilograms and convert and display the result

in grams.

import java.util.*;
class WeightConverter
{
public static void main(String[] args)
{
Scanner s1 = new Scanner(System.in);
System.out.print("Enter weight in kilograms: ");
double kilo = s1.nextDouble();
double gram = kilo*1000;
System.out.println("Weight in Kilogram: "+kilo+" kg");
System.out.println("Weight in Grams: "+gram+" g");
}
}

OUTPUT:
Q10. Write a program to input a character
and display the ASCII code of the character.

import java.util.Scanner;

public class AsciiValue

public static void main(String[] args)

Scanner s1 = new Scanner(System.in);

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

char ch = s1.next().charAt(0);

int ascii = (int) ch;

System.out.println("The ASCII value of '" + ch + "' is: " + ascii);

}
OUTPUT:
Q11. Write a program to input a 4-digit
number and display each of its digits in a
new line.

import java.util.*;

class Each_line

public static void main(String[] args)

Scanner s1 = new Scanner(System.in);

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

int number = s1.nextInt();

int digit1 = number / 1000;

int digit2 = (number / 100) % 10;

int digit3 = (number / 10) % 10;

int digit4 = number % 10;

System.out.println("Digits of the number are:");

System.out.println(digit1);

System.out.println(digit2);

System.out.println(digit3);

System.out.println(digit4);

}
OUTPUT:
Q12. Write a program to input a number and
check whether the number is Niven number
or not.

import java.util.Scanner;

public class NivenNumber

public static int sumOfDigits(int n)

int sum = 0;

while (n > 0) {

sum += n % 10;

n /= 10;

return sum;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt();

int sum = sumOfDigits(n);

if (n % sum == 0)
{

System.out.println(n + " is a Niven number.");

else

System.out.println(n + " is not a Niven number.");

}
OUTPUT:
Q13. Write a menu driven program to input
numbers from 1 to 7 as case values and print
weeks days according to the number. Display
message for wrong input accordingly.

import java.util.*;

class Week_Days

public static void main(String[]args)

Scanner s1 = new Scanner(System.in);

System.out.println("Enter the Number from 1 to 7 to get Week Days: ");

int day = s1.nextInt();

switch(day)

case 1:

System.out.println("Sunday");

break;

case 2:

System.out.println("Monday");

break;

case 3:

System.out.println("Tuesday");

break;
case 4:

System.out.println("Wednesday");

break;

case 5:

System.out.println("Thrusday");

break;

case 6:

System.out.println("Friday");

break;

case 7:

System.out.println("Saturday");

break;

default :

System.out.println("Invalid day Number! Try again.");

}
OUTPUT:

Q14. Design a class Perfect to input a


number and check whether the number is
Perfect or not. Display the appropriate
messages.
import java.util.*;
class perfect
{
public static void main(String[]args)
{
Scanner s1 = new Scanner(System.in);
int num;
int fact;
int sum=0;
System.out.println("Enter a number ");
System.out.println("To check whether it is Prime or not");
num = s1.nextInt();
for(int i=1; i<num; i++)
{
if(num%i==0)
{
fact = i;
sum = sum+fact;
}
}
if(sum==num)
System.out.println(num+" is a Perfect number");
else
System.out.println(num+" is not a Perfect number");
}
}
OUTPUT:
Q15. Write a program to input all three sides
of a triangle and calculate and display the
area of the triangle using Heron’s formula.
import java.util.*;
class triangle
{
public static void main(String[]args)
{
Scanner s1 = new Scanner(System.in);
System.out.println("Enter the values of Trianlge");
System.out.print("First Side :");
double a = s1.nextDouble();
System.out.print("Second Side:");
double b = s1.nextDouble();
System.out.print("Third Side :");
double c = s1.nextDouble();
double half = (a+b+c)/2;
double area = Math.sqrt((half-a)*(half-b)*(half-c));
System.out.println("The area of Trianlge = "+area);
}
}
OUTPUT:

You might also like