Lakkad Keyur R.

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 34

1.

/*Write a Java program to calculate the circumference of a circle


value that should be

taken from the Scanner class.

*/

import java.util.Scanner;

public class assi1{

public static void main(String[] args) {

Scanner prg = new Scanner(System.in);

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

double radius = prg.nextDouble();

double cir = 2 * Math.PI * radius;

System.out.println("The cir of the circle is: " + cir);

}
2./*Write a Java program to swap two numbers without taking the
third variable value

that should be taken from the Scanner class.*/

import java.util.Scanner;

public class assi2{

public static void main(String[] args)

Scanner prg = new Scanner(System.in);

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

int num1 = prg.nextInt();

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

int num2 = prg.nextInt();


System.out.println("Before swapping: num1 = " + num1 + ", num2 =
" + num2);

num1 = num1 + num2;

num2 = num1 - num2;

num1 = num1 - num2;

System.out.println("After swapping: num1 = " + num1 + ", num2 = "


+ num2);

3./*Write a Java program to take student details rollno, name, surname,


and 5 subject

marks and display the total and percentage. All values should be taken
from the

Scanner class.*/

import java.util.Scanner;

public class assi3

{
public static void main(String[]args)

Scanner prg = new Scanner(System.in);

System.out.print("enter roll number :");

int rollno = prg.nextInt();

System.out.print("enter student's frist name :");

String firstname = prg.next();

System.out.ptint("enter student's last name :");

String lastname = prg.next();

System.out.print("enter marks for 5 subject :");

int[]mark = new int[5];

int totalMark = 0;

for (int i=0; i<marks.length;i++){

System.out.print("enter marks for subject " +(i+1)+":");

mark[i]=prg.nextInt();

totalMark +=marks[i];
}

double percentage = (double) totalMark / (marks.length *100)*100;

System.out.println("\nStudent Details:");

System.out.println("Roll Number: " + rollNo);

System.out.println("Name: " + firstName + " " + lastName);

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

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

3./*Write a Java program to swap two numbers without taking the


third variable value

that should be taken from the Scanner class.*/

import java.util.Scanner;

public class assi2{

public static void main(String[] args)


{

Scanner prg = new Scanner(System.in);

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

int num1 = prg.nextInt();

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

int num2 = prg.nextInt();

System.out.println("Before swapping: num1 = " + num1 + ", num2 =


" + num2);

num1 = num1 + num2;

num2 = num1 - num2;

num1 = num1 - num2;

System.out.println("After swapping: num1 = " + num1 + ", num2 = "


+ num2);

}
4./*Write a Java program that will convert a float datatype into an int
data type. Perform

Type conversion */

public class assi4{

public static void main(String[] args)

int intValue = 42;

double doubleValue = (double) intValue;

System.out.println("Original integer value: " + intValue);

System.out.println("Converted double value: " + doubleValue);

5./*Write a Java program to demonstrate the use of arithmetic


operators */

public class q5{

public static void main(String[] args)


{

int num1 = 20;

int num2 =50;

int sum = num1 + num2;

System.out.println("Addition: " + sum);

int difference = num1 - num2;

System.out.println("Subtraction: " + difference);

int product = num1 * num2;

System.out.println("Multiplication: " + product);

double divisionResult = (double) num1 / num2;

System.out.println("Division: " + divisionResult);

int remainder = num1 % num2;

System.out.println("Modulus (Remainder): " + remainder);

int x = 5;
x++;

System.out.println("After increment: " + x);

int y = 8;

y--;

System.out.println("After decrement: " + y);

6./*Write a Java program to demonstrate the use of relational


operators */

public class assi6 {

public static void main(String[] args) {

int num1 = 50;

int num2 = 20;

boolean isGreater = num1 > num2;

System.out.println(num1 + " is greater than " + num2 + ": " +


isGreater);
boolean isLess = num1 < num2;

System.out.println(num1 + " is less than " + num2 + ": " + isLess);

boolean isGreaterOrEqual = num1 >= num2;

System.out.println(num1 + " is greater than or equal to " + num2 +


": " + isGreaterOrEqual);

boolean isLessOrEqual = num1 <= num2;

System.out.println(num1 + " is less than or equal to " + num2 + ": "


+ isLessOrEqual);

boolean isEqual = num1 == num2;

System.out.println(num1 + " is equal to " + num2 + ": " + isEqual);

boolean isNotEqual = num1 != num2;

System.out.println(num1 + " is not equal to " + num2 + ": " +


isNotEqual);

}
7./*. Write a Java program to demonstrate the use of conditional
operators.

*/

public class assi7{

public static void main(String[] args) {

int num1 = 20;

int num2 = 10;

boolean conditionAND = (num1 > 0) && (num2 < 10);

System.out.println("Conditional AND: " + conditionAND);

boolean conditionOR = (num1 > 0) || (num2 > 10);

System.out.println("Conditional OR: " + conditionOR);

boolean conditionNOT = !(num1 == num2);

System.out.println("Conditional NOT: " + conditionNOT);

}
8./*. Write a Java program to demonstrate the use of logical
operators.

*/

public class assi8{

public static void main(String[] args)

int num1 = 20;

int num2 = 10;

boolean bool1 = true;

boolean bool2 = false;

boolean resultAND1 = (num1 > 0) && (num2 < 10);

boolean resultAND2 = bool1 && bool2;

System.out.println("Logical AND 1: " + resultAND1);

System.out.println("Logical AND 2: " + resultAND2);

boolean resultOR1 = (num1 > 0) || (num2 > 10);


boolean resultOR2 = bool1 || bool2;

System.out.println("Logical OR 1: " + resultOR1);

System.out.println("Logical OR 2: " + resultOR2);

boolean resultNOT1 = !(num1 == num2);

boolean resultNOT2 = !bool1;

System.out.println("Logical NOT 1: " + resultNOT1);

System.out.println("Logical NOT 2: " + resultNOT2);

9./*Write a Java program to demonstrate the use of bitwise


operators. */

public class assi9{

public static void main(String[] args) {

int num1 = 20;

int num2 = 10;


int resultAND = num1 & num2;

System.out.println("Bitwise AND: " + resultAND);

int resultOR = num1 | num2;

System.out.println("Bitwise OR: " + resultOR);

int resultXOR = num1 ^ num2;

System.out.println("Bitwise XOR: " + resultXOR);

int resultNOT1 = ~num1;

int resultNOT2 = ~num2;

System.out.println("Bitwise NOT (num1): " + resultNOT1);

System.out.println("Bitwise NOT (num2): " + resultNOT2);

int leftShiftResult = num1 << 2;

System.out.println("Bitwise left shift: " + leftShiftResult);

int rightShiftResult = num1 >> 2;

System.out.println("Bitwise right shift: " + rightShiftResult);

}
}

10./*Write a Java program to find a maximum of three numbers


using conditional

operators */

import java.util.Scanner;

public class assi10{

public static void main(String[] args) {

Scanner prg = new prg(System.in);

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

double num1 = prg.nextDouble();

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

double num2 = prg.nextDouble();

double result = (num1 > num2) ? (num1 * num2) : (num1 / num2);


System.out.println("Result: " + result);

prg.close();

11./*Write a program to check whether the blood donor is eligible


or not for donating

blood. The conditions laid down are as under. Use if statement. a) Age
should be

above 18 yrs but not more than 55 yrs. b) Weight should be more than
45kgs.

*/

import java.util.Scanner;

public class assi12 {

public static void main(String[] args) {

Scanner prg = new Scanner(System.in);


System.out.println("Enter the age of the blood donor: ");

int age = prg.nextInt();

System.out.println("Enter the weight of the blood donor (in kgs): ");

double weight = prg.nextDouble();

if (age > 18 && age <= 55 && weight > 45) {

System.out.println("The blood donor is eligible for donating


blood.");

} else {

System.out.println("The blood donor is not eligible for donating


blood.");

prg.close();

12./*Write a program to implement a calculator using a switch case.


*/
import java.util.Scanner;

public class assi13 {

public static void main(String[] args) {

Scanner prg = new Scanner(System.in);

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

double num1 = prg.nextDouble();

System.out.println("Enter the operator (+, -, *, /): ");

char operator = prg.next().charAt(0);

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

double num2 = prg.nextDouble();

double result;

switch (operator) {

case '+':

result = num1 + num2;


break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

if (num2 == 0) {

System.out.println("Error: Cannot divide by zero!");

return;

result = num1 / num2;

break;

default:

System.out.println("Invalid operator!");

return;

System.out.println("Result: " + result);


// prg.close();

13./*Write a Program to Check Whether the Entered Year is Leap


Year or not.

*/

import java.util.Scanner;

public class assi14{

public static void main(String[] args) {

Scanner prg = new Scanner(System.in);

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

int year = prg.nextInt();

boolean isLeapYear = false;


if (year % 4 == 0) {

if (year % 100 == 0) {

isLeapYear = (year % 400 == 0);

} else {

isLeapYear = true;

if (isLeapYear) {

System.out.println(year + " is a leap year.");

} else {

System.out.println(year + " is not a leap year.");

prg.close();

14./*. Write a Java program to display Floyd’s triangle.


1

23

456

7 8 9 10…….. so */

import java.util.Scanner;

public class assi15 {

public static void main(String[] args) {

Scanner prg = new Scanner(System.in);

System.out.println("Enter the number of rows for Floyd's triangle:


");

int rows = prg.nextInt();

int num = 1;

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(num + " ");


num++;

System.out.println();

// prg.close();

15./*Write a Java Program to find the sum of all integers greater


than 100 and less than

200 that are divisible by 7. */

public class assi16 {

public static void main(String[] args) {

int start = 101;

int end = 199;

int divisor = 7;
int sum = 0;

for (int num = start; num <= end; num++) {

if (num % divisor == 0) {

sum += num;

System.out.println("Sum of integers between 100 and 200


(inclusive) that are divisible by 7: " + sum);

16./*Write a Java program to check whether the entered number is


palindrome or not.

*/

import java.util.Scanner;

public class assi17 {

public static void main(String[] args) {


Scanner prg = new Scanner(System.in);

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

int number = prg.nextInt();

int reversedNumber = 0;

int originalNumber = number;

while (number != 0) {

int digit = number % 10;

reversedNumber = reversedNumber * 10 + digit;

number /= 10;

if (originalNumber == reversedNumber) {

System.out.println(originalNumber + " is a palindrome


number.");

} else {

System.out.println(originalNumber + " is not a palindrome


number.");

}
//scanner.close();

17./*. Write a Java Program to calculate the students 3 subject


marks and calculate a

percentage.

If the percentage greater then 70 give grade “Distinction”

If the percentage greater then 60 give grade “First Class”

If the percentage greater then 50 give grade “Second Class”

If Below 35 then give grade “Fail”

*/

import java.util.Scanner;

public class assi18 {

public static void main(String[] args) {

Scanner prg = new Scanner(System.in);


System.out.println("Enter the marks of three subjects:");

System.out.print("Subject 1: ");

double subject1Marks = prg.nextDouble();

System.out.print("Subject 2: ");

double subject2Marks = prg.nextDouble();

System.out.print("Subject 3: ");

double subject3Marks = prg.nextDouble();

double totalMarks = subject1Marks + subject2Marks +


subject3Marks;

double percentage = (totalMarks / 300) * 100;

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

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

String grade;
if (percentage >= 70) {

grade = "Distinction";

} else if (percentage >= 60) {

grade = "First Class";

} else if (percentage >= 50) {

grade = "Second Class";

} else {

grade = "Fail";

System.out.println("Grade: " + grade);

// scanner.close();

18./*Write a Java program to find a factorial number using a do-


while loop */
import java.util.Scanner;

public class assi19 {

public static void main(String[] args) {

Scanner prg = new Scanner(System.in);

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

int number = prg.nextInt();

if (number < 0) {

System.out.println("Factorial is not defined for negative


numbers.");

} else {

int factorial = 1;

int i = 1;

do {

factorial *= i;

i++;

} while (i <= number);


System.out.println("Factorial of " + number + " is: " + factorial);

// scanner.close();

19./*Write a Java program to generates first n prime numbers */

import java.util.Scanner;

public class assi20 {

public static void main(String[] args) {

Scanner prg = new Scanner(System.in);

System.out.print("Enter the value of n: ");

int n = prg.nextInt();

if (n <= 0) {
System.out.println("Invalid input! Please enter a positive
number.");

} else {

System.out.println("First " + n + " prime numbers:");

int count = 0;

int num = 2;

while (count < n) {

if (isPrime(num)) {

System.out.print(num + " ");

count++;

num++;

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;

You might also like