0% found this document useful (0 votes)
20 views9 pages

Oop Java Code

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

Oop Java Code

Code related PDF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DES PUNE UNIVERSITY

Computer Engineering and Technology


Program: [Link]. Computer Science and Engineering

Academic Year: 2023-24 Year: Second Year Term: II


Roll No.: 42 Name: Sameer Vinayak sorte
Subject: 1
Assignment No.: 42 Title: assignment 1
Date:

Code:-

1) Program to Check Whether a Number is Positive or Negative

import [Link];

public class PositiveNegativeChecker {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

if (number > 0) {

[Link]("The number is positive.");

} else if (number < 0) {

[Link]("The number is negative.");

} else {

[Link]("The number is zero.");

}
[Link]();

2) Program to Calculate the Sum of Natural Numbers

import [Link];

public class SumOfNaturalNumbers {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

Assignment By: Page 1 of 9


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: [Link]. Computer Science and Engineering

[Link]("Enter the limit: ");

int limit = [Link]();

int sum = 0;

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

sum += i;

[Link]("The sum of natural numbers up to " + limit + " is: " + sum);

[Link]();

3)

import [Link];

public class Factorial {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

int factorial = 1;

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

factorial *= i;

[Link]("Factorial of " + number + " is: " + factorial);

[Link]();

Assignment By: Page 2 of 9


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: [Link]. Computer Science and Engineering

4)

import [Link];

public class MultiplicationTable {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the number for the multiplication table: ");

int number = [Link]();

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

[Link](number + " x " + i + " = " + (number * i));

[Link]();

5)

import [Link];

public class FibonacciSeries {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the number of terms in the Fibonacci series: ");

int terms = [Link]();

int a = 0, b = 1;

[Link]("Fibonacci Series:");

for (int i = 0; i < terms; i++) {

[Link](a + " ");

int next = a + b;

a = b;

b = next;

Assignment By: Page 3 of 9


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: [Link]. Computer Science and Engineering

[Link]();

6)

import [Link];

public class GCD {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter first number: ");

int num1 = [Link]();

[Link]("Enter second number: ");

int num2 = [Link]();

int gcd = 1;

for (int i = 1; i <= num1 && i <= num2; i++) {

if (num1 % i == 0 && num2 % i == 0) {

gcd = i;

[Link]("GCD of " + num1 + " and " + num2 + " is: " + gcd);

[Link]();

7)

import [Link];

public class LCM {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter first number: ");

Assignment By: Page 4 of 9


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: [Link]. Computer Science and Engineering

int num1 = [Link]();

[Link]("Enter second number: ");

int num2 = [Link]();

int lcm = (num1 * num2) / findGCD(num1, num2);

[Link]("LCM of " + num1 + " and " + num2 + " is: " + lcm);

[Link]();

public static int findGCD(int num1, int num2) {

while (num2 != 0) {

int temp = num2;

num2 = num1 % num2;

num1 = temp;

return num1;

8)

import [Link];

public class ReverseNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

int reversedNumber = 0;

while (number != 0) {

int digit = number % 10;

reversedNumber = reversedNumber * 10 + digit;

number /= 10;

Assignment By: Page 5 of 9


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: [Link]. Computer Science and Engineering

[Link]("Reversed number: " + reversedNumber);

[Link]();

9)

import [Link];

public class PalindromeCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

int originalNumber = number;

int reversedNumber = 0;

while (number != 0) {

int digit = number % 10;

reversedNumber = reversedNumber * 10 + digit;

number /= 10;

if (originalNumber == reversedNumber) {

[Link]("Palindrome");

} else {

[Link]("Not Palindrome");

[Link]();

10)

import [Link];

Assignment By: Page 6 of 9


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: [Link]. Computer Science and Engineering

public class PrimeCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

boolean isPrime = true;

if (number <= 1) {

isPrime = false;

} else {

for (int i = 2; i <= [Link](number); i++) {

if (number % i == 0) {

isPrime = false;

break;

if (isPrime) {

[Link]("Prime");

} else {

[Link]("Not Prime");

[Link]();

11)

import [Link];

public class ScannerMethods {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

Assignment By: Page 7 of 9


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: [Link]. Computer Science and Engineering

[Link]("Enter an integer: ");

int num = [Link]();

[Link]("You entered: " + num);

[Link]("Enter a double: ");

double dbl = [Link]();

[Link]("You entered: " + dbl);

[Link](); // consume newline character left by nextInt() or nextDouble()

[Link]("Enter a string: ");

String str = [Link]();

[Link]("You entered: " + str);

[Link]();

Assignment By: Page 8 of 9


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: [Link]. Computer Science and Engineering

Assignment By: Page 9 of 9

You might also like