You are on page 1of 28

Name:- Prathamesh Suresh Mungse

Sub:- Java Programming


Class:- F.Y MCA
Roll No:-20220201036 Assignment No-1
1.Write a program that asks the user to enter a number and displays whether entered number
is an odd number or even number
import java.util.Scanner;
public class EvenOdd {
    public static void main(String args[])
    {
      Scanner reader = new Scanner(System.in);
      System.out.println("Enetr the NUmber=");
      int num = reader.nextInt();

      if(num%2==0)
      {
        System.out.println(num + "Number is Even");
      }
      else
      {
        System.out.println(num + "Number is Odd");
      }
    }
}

OUTPUT:-

…………………………………………………………………………………………………………………………………………………………...

2.Write a program that asks the user to enter a number and displays the absolute value of that
number.

import java.util.Scanner;
public class AbsoluteValue
{
public static void main(String[] args)
{
int number;
Scanner console = new Scanner(System.in);
System.out.print("Enter an integer: ");
number = console.nextInt();
if (number < 0)
{
number = -number;
}
System.out.println("Absolute value: " + number);
}
}

OUTPUT:-

3. Revenue can be calculated as the selling price of the product times the quantity sold, i.e.
revenue = price × quantity. Write a program that asks the user to enter product price and
quantity and then calculate the revenue. If the revenue is more than 5000 a discount is 10%
offered. Program should display the discount and net revenue.
import java.util.Scanner;

public class Discount


{
    public static void main(String[] args)
    {
        int revenue, price, quantity;
        int dis = 0;
        Scanner console = new Scanner(System.in);
        System.out.print("Enter price of product: ");
        price = console.nextInt();

        System.out.print("Enter quantity of product: ");


        quantity = console.nextInt();
        revenue = price * quantity;

        if (revenue > 5000)


        {
            dis = revenue * 10 / 100;
            revenue = revenue - dis;
        }
        System.out.println("The discount is " + dis);
        System.out.println("The net revenue is " + revenue);
    }
}

OUTPUT:-

4. Write a program that asks the user to enter a numbers in three variables and then displays the
largest number.
import java.util.Scanner;

public class LargestNum


{
    public static void main(String[] args)
    {
        int number1, number2, number3;
        int largest;

        Scanner console = new Scanner(System.in);

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


        number1 = console.nextInt();

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


        number2 = console.nextInt();
        System.out.print("Enter third number: ");
        number3 = console.nextInt();
        if (number1 > number2 && number1 > number3)
        {
            largest = number1;
        }
        else if (number2 > number3)
        {
            largest = number2;
        }
        else
        {
            largest = number3;
        }
        System.out.println("Largest number: " + largest);
    }
}

OUTPUT:-

5. Write a program that prompts the user to input a number. The program should then output
the number and a message saying whether the number is positive, negative, or zero
import java.util.Scanner;

public class Pnz


{
    public static void main(String[] args)
    {
        int number;
        Scanner console = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        number = console.nextInt();
        if (number > 0)
        {
            System.out.println("Number is positive");
        }
        else if (number < 0)
        {
            System.out.println("Number is negative.");
        }
        else
        {
            System.out.println("Number is zero.");
        }
    }
}

OUTPUT:-
6. A triangle is valid if the sum of all the three angles is equal to 180 degrees. Write a program
that asks the user to enter three integers as angles and check whether a triangle is valid or not.
import java.util.Scanner;

public class Triangle


{
    public static void main(String[] args)
    {
        int angle1, angle2, angle3;
        Scanner console = new Scanner(System.in);
        System.out.print("Enter first angle: ");
        angle1 = console.nextInt();

        System.out.print("Enter second angle: ");


        angle2 = console.nextInt();

        System.out.print("Enter third angle: ");


        angle3 = console.nextInt();
        if (angle1 + angle2 + angle3 == 180)
        {
            System.out.println("Triangle is valid");
        }
        else
        {
            System.out.println("Triangle is not valid");
        }
    }
}

OUTPUT:-
7.Leap Year (Any year is input by the user. Write a program to determine whether the year is a leap year or
not. leap Years are any year that can be evenly divided by 4.  A year that is evenly divisible by 100 is a leap
year only if it is also evenly divisible by 400.

Example : 1992 Leap Year, 2000 Leap Year, 1900 NOT a Leap Year
import java.util.Scanner;

public class LeapYear


{
   public static void main(String[] args)
   {
      int year;  
      Scanner console = new Scanner(System.in);
      System.out.print("Enter a year : ");
      year = console.nextInt();
      if ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)))
      {
         System.out.println("A leap year");
      }
      else
      {
         System.out.println("Not a leap year");
      }
   }
}

OUTPUT:-

8. Write a program to calculate the monthly telephone bills as per the following rule: Minimum Rs.
200 for up to 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls.
import java.util.Scanner;

public class Telephone


{
    public static void main(String[] args)
    {
        int numberOfCalls;
        double bill;
        Scanner console = new Scanner(System.in);
        System.out.print("Enter number of calls: ");
        numberOfCalls = console.nextInt();
        if (numberOfCalls <= 100)
        {
            bill = 200;
        }
        else if (numberOfCalls <= 150)
        {
            bill = 200 + (numberOfCalls - 100) * 0.60;
        }
        else if (numberOfCalls <= 200)
        {
            bill = 200 + 50 * 0.60
                    + (numberOfCalls - 150) * 0.50;
        }
        else
        {
            bill = 200 + 50 * 0.60 + 50 * 0.50
                    + (numberOfCalls - 200) * 0.40;
        }
        System.out.println("The bill is Rs. " + bill);
    }
}

OUTPUT:-

9. The marks obtained by a student in 3 different subjects are input by the user. Your program
should calculate the average of subjects. The student gets a grade as per the following rules:
Average Grade (90-100 - A) (80-89 – B) (70-79 – C) (60-69 – D) (0-59 – F)
import java.util.Scanner;

public class GradeCalc


{
    public static void main(String[] args)
    {
        int marks1, marks2, marks3;
        double average;
        char grade;
        Scanner console = new Scanner(System.in);
        System.out.print("Enter marks of subject 1: ");
        marks1 = console.nextInt();

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


        marks2 = console.nextInt();

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


        marks3 = console.nextInt();
        average = (marks1 + marks2 + marks3) / 3.0;

        if (average >= 90)


        {
            grade = 'A';
        }
        else if (average >= 80)
        {
            grade = 'B';
        }
        else if (average >= 70)
        {
            grade = 'C';
        }
        else if (average >= 60)
        {
            grade = 'D';
        }
        else
        {
            grade = 'F';
        }
        System.out.println("Grade is: " + grade);
    }
}

OUTPUT:-

10. Write a program that prompts the user to enter grade. Your program should display the
corresponding meaning of grade as per the following table GradeMeaning A =Excellent, B=Good, C=
Average, D= Deficient, F= Failing.
import java.util.Scanner;

public class GradeMeaning


{
    public static void main(String[] args)
    {
        char grade;
        Scanner console = new Scanner(System.in);
        System.out.print("Enter grade: ");
        grade = console.next().charAt(0);
        switch (grade)
        {
        case 'A':
            System.out.println("Excellent");
            break;
        case 'B':
            System.out.println("Good");
            break;
        case 'C':
            System.out.println("Average");
            break;
        case 'D':
            System.out.println("Deficient");
            break;
        case 'F':
            System.out.println("Failing");
            break;
        default:
            System.out.println("Invalid input");
        }
    }
}

OUTPUT:-

11. Write a program that prompts the user to enter three names. Your program should display the
names in descending order.
import java.util.Scanner;

public class Descending


{
    public static void main(String[] args)
    {
        String name1, name2, name3;
        Scanner console = new Scanner(System.in);
        System.out.print("Enter name 1: ");
        name1 = console.nextLine();

        System.out.print("Enter name 2: ");


        name2 = console.nextLine();

        System.out.print("Enter name 3: ");


        name3 = console.nextLine();
        if (name1.compareTo(name2) > 0
                && name1.compareTo(name3) > 0)
        {
            System.out.println(name1);
            if (name2.compareTo(name3) > 0)
            {
                System.out.println(name2);
                System.out.println(name3);
            }
            else
            {
                System.out.println(name3);
                System.out.println(name2);
            }
        }

        else if (name2.compareTo(name1) > 0


                && name2.compareTo(name3) > 0)
        {
            System.out.println(name2);
            if (name1.compareTo(name3) > 0)
            {
                System.out.println(name1);
                System.out.println(name3);
            }
            else
            {
                System.out.println(name3);
                System.out.println(name1);
            }
        }
        else
        {
            System.out.println(name3);
            if (name1.compareTo(name2) > 0)
            {
                System.out.println(name1);
                System.out.println(name2);
            }
            else
            {
                System.out.println(name2);
                System.out.println(name1);
            }
        }
    }
}

OUTPUT:-

Looping:
Write a program to print numbers from 1 to 10
public class PrintNumbers
{
    public static void main(String[] args)
    {
        for(int i=1; i<=10; i++)
        {
            System.out.println(i);
        }
    }
}

OUTPUT:-

2. Write a program to calculate the sum of first 10 natural number.


public class SumNumbers
{
    public static void main(String[] args)
    {
        int sum = 0;
        for(int i=1; i<=10; i++)
        {
            sum += i;
        }
        System.out.println("Sum: " + sum);
    }
}

OUTPUT:-

3. Write a program that prompts the user to input a positive integer. It should then print the
multiplication table of that number.
import java.util.Scanner;

public class Table


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        int num;
       
        System.out.print("Enter any positive integer: ");
        num = console.nextInt();
               
        System.out.println("Multiplication Table of " + num);
       
        for(int i=1; i<=10; i++)
        {
            System.out.println(num +" x " + i + " = " + (num*i) );
        }
    }
}

OUTPUT:-

4. Write a program to find the factorial value of any number entered through the keyboard.
import java.util.Scanner;
public class Fact
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        int num;
        int fact = 1;
        System.out.print("Enter any positive integer: ");
        num = console.nextInt();
       
        for(int i=1; i<=num; i++)
        {
            fact *= i;
        }
       
        System.out.println("Factorial: "+ fact);
    }
}

OUTPUT:-

5. Two numbers are entered through the keyboard. Write a program to find the value of one
number raised to the power of another. (Do not use Java built-in method)
import java.util.Scanner;

public class PowerDemo


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int base;
        int power;
        int result = 1;
       
        System.out.print("Enter the base number ");
        base = console.nextInt();
       
        System.out.print("Enter the power ");
        power = console.nextInt();

        for(int i = 1; i <= power; i++)


        {
        result *= base;
        }

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


    }
}

OUTPUT:-
6. Write a program that prompts the user to input an integer and then outputs the number with the
digits reversed. For example, if the input is 12345, the output should be 54321.
import java.util.Scanner;

public class ReverseNumber


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int number;
        int reverse = 0;
       
        System.out.print("Enter the number ");
        number = console.nextInt();
       
        int temp = number;
        int remainder = 0;
       
        while(temp>0)
        {
        remainder = temp % 10;
        reverse = reverse * 10 + remainder;
            temp /= 10;
        }

        System.out.println("Reverse of " + number + " is " + reverse);


    }
}

OUTPUT:-

7. Write a program that reads a set of integers, and then prints the sum of the even and odd
integers.
import java.util.Scanner;

public class ReadSetIntegers


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int number;
        char choice;
        int evenSum = 0;
        int oddSum = 0;
       
        do
        {
            System.out.print("Enter the number ");
            number = console.nextInt();
       
            if( number % 2 == 0)
            {
                evenSum += number;
            }
            else
            {
                oddSum += number;
            }
       
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
           
        }while(choice=='y' || choice == 'Y');
       
        System.out.println("Sum of even numbers: " + evenSum);
        System.out.println("Sum of odd numbers: " + oddSum);
    }  
}

OUTPUT:_

8. Write a program that prompts the user to input a positive integer. It should then output a
message indicating whether the number is a prime number
import java.util.Scanner;

public class TestPrime


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int number;
       
        System.out.print("Enter the positive integer ");
        number = console.nextInt();
       
        boolean flag = true;
       
        for(int i = 2; i < number; i++)
    {
        if(number % i == 0)
            {
                flag = false;
                break;
            }
        }

    if(flag && number > 1)


        {
            System.out.println("Number is prime");
        }
    else
        {
            System.out.println("Number is not prime");
        }
       
    }  
}

OUTPUT:-

9. Write a program to calculate HCF of Two given number.


import java.util.Scanner;

public class FindHcf


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int dividend, divisor;
        int remainder, hcf = 0;
       
        System.out.print("Enter the first number ");
        dividend = console.nextInt();
       
        System.out.print("Enter the second number ");
        divisor = console.nextInt();        
       
        do
    {
            remainder = dividend % divisor;
           
            if(remainder == 0)
            {
                hcf = divisor;
            }
        else
            {
                dividend = divisor;
                divisor = remainder;
        }
           
        }while(remainder != 0);

        System.out.println("HCF: " + hcf);


    }  
}
OUTPUT:-

10. Write a do-while loop that asks the user to enter two numbers. The numbers should be added
and the sum displayed. The loop should ask the user whether he or she wishes to perform the
operation again. If so, the loop should repeat; otherwise it should terminate.
import java.util.Scanner;

public class SumAgain


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int number1, number2;
        char choice;
       
        do
        {
            System.out.print("Enter the first number ");
            number1 = console.nextInt();
         
            System.out.print("Enter the second number ");
            number2 = console.nextInt();
           
            int sum = number1 + number2;
            System.out.println("Sum of numbers: " + sum);
       
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
           
            System.out.println();
           
        }while(choice=='y' || choice == 'Y');
    }  
}

OUTPUT:-

11. Write a program to enter the numbers till the user wants and at the end it should display the
count of positive, negative and zeros entered.
import java.util.Scanner;
public class CountNumbers
{
    public static void main(String[] args)
    {
       
Scanner console = new Scanner(System.in);
       
int number,          
       
    countPositive = 0,
       
    countNegative = 0,
       
    countZero = 0;
char choice;
do
        {
            System.out.print("Enter the number ");
            number = console.nextInt();
       
            if(number > 0)
            {
                countPositive++;
            }
            else if(number < 0)
            {
                countNegative++;
            }
            else
            {
                countZero++;
            }
       
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
           
        }while(choice=='y' || choice == 'Y');
       
        System.out.println("Positive numbers: " + countPositive);
        System.out.println("Negative numbers: " + countNegative);
        System.out.println("Zero numbers: " + countZero);
    }  
}

OUTPUT:-

12. Write a program to enter the numbers till the user wants and at the end the program should
display the largest and smallest numbers entered.
import java.util.Scanner;

public class FindMaxMin


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
       
        int number;
        int max = Integer.MIN_VALUE;  // Intialize max with minimum value
        int min = Integer.MAX_VALUE;  // Intialize min with maximum value

        char choice;
   
        do
        {
            System.out.print("Enter the number ");
            number = console.nextInt();
       
            if(number > max)
            {
                max = number;
            }
           
            if(number < min)
            {
                min = number;
            }
       
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
           
        }while(choice=='y' || choice == 'Y');
       
        System.out.println("Largest number: " + max);
        System.out.println("Smallest number: " + min);
    }  
}

OUTPUT:-

13. Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each
digit of the number is equal to the number itself, then the number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
public class ArmstrongNumber
{
    public static void main(String[] args)
    {
        int digit1,  // To hold first digit (Ones) of number
            digit2,  // To hold second digit (Tens) of number
            digit3;  // To hold third digit (Hundreds) of number

    for(int number = 1; number <= 500; number++)


    {
            int temp = number;
        digit1 = temp % 10;

            temp = temp / 10;


            digit2 = temp % 10;
           
            temp = temp / 10;
            digit3 = temp % 10;

        if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == number)


            {
            System.out.println(number);
            }
    }
    }  
}
OUTPUT:-

14. Write a program to print Fibonacci series of n terms where n is input by user : 0 1 1 2 3 5 8 13
24 .....
import java.util.Scanner;

public class FibonacciSeries


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
       
        int number;  // To hold number of terms

        int firstTerm = 0,
            secondTerm = 1,
            thirdTerm;
 
        System.out.print("Enter number of terms of series : ");
        number = console.nextInt();
 
        System.out.print(firstTerm + " " + secondTerm + " ");
 
        for(int i = 3; i <= number; i++)
    {
            thirdTerm = firstTerm + secondTerm;
            System.out.print(thirdTerm + " ");
            firstTerm = secondTerm;
            secondTerm = thirdTerm;
    }
    }  
}

OUTPUT:-

15. Write a program to calculate the sum of following series where n is input by user. 1 + 1/2 + 1/3 +
1/4 + 1/5 +…………1/n
import java.util.Scanner;

public class SumOfSeries


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
       
        int number;  // To hold number of terms

        double sum = 0;

        System.out.print("Enter number of terms of series : ");


        number = console.nextInt();
 
        for(int i = 1; i <= number; i++)
    {
            sum += 1.0/i;
    }
       
        System.out.println("sum: " + sum);
    }  
}

OUTPUT:-

16. Compute the natural logarithm of 2, by adding up to n terms in the series 1 - 1/2 + 1/3 - 1/4 + 1/5
-... 1/n where n is a positive integer and input by user
import java.util.Scanner;

public class Ln2


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
       
        int number;  // To hold number of terms

        System.out.print("Enter number of terms of series : ");


        number = console.nextInt();

        double sum = 0;
        int sign = 1;
       
        for(int i = 1; i <= number; i++)
    {
            sum += (1.0 * sign) / i;
            sign *= -1;
    }
       
        System.out.println("log2: " + sum);
    }
}

OUTPUT:-
17. Write a program that generates a random number and asks the user to guess what the number
is. If the user's guess is higher than the random number, the program should display "Too high, try
again." If the user's guess is lower than the random number, the program should display "Too low,
try again." The program should use a loop that repeats until the user correctly guesses the random
number.
import java.util.Scanner;

public class GuessMyNumber


{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
       
        int number, // To hold the random number
            guess,  // To hold the number guessed by user
            tries = 0; // To hold number of tries
       
        number = (int) (Math.random() * 100) + 1; // get random number between 1 and 100
       
        System.out.println("Guess My Number Game");
        System.out.println();
       
        do
        {
            System.out.print("Enter a guess between 1 and 100 : ");
            guess = console.nextInt();
               
            tries++;
               
        if (guess > number)
        {
            System.out.println("Too high! Try Again");
        }
        else if (guess < number)
        {
            System.out.println("Too low! Try Again");
        }
        else
        {
        System.out.println("Correct! You got it in " + tries + " guesses!");
        }
       
        }while (guess != number);
    }  
}

OUTPUT:-

18. Write a program to print following :


Q1.
package Q18;

public class Q1 {
    public static void main(String[] args) {
        for(int i=1;i<=4;i++){
            for(int j=1;j<=10;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }

Output:-

Q2.

public class Q2 {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++){
 
            for(int j=1;j<=i;j++){
                System.out.print("*");
            }
 
            System.out.println();
        }
    }

Output:-

Q3.
public class Q3 {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int sp = i;sp <= 5;sp++){
System.out.print(" ");

}
for(int j=1;j<=i;j++){
System.out.print("*");
}

System.out.println();
}
}
}
OUTPUT:-

Q4.
public class Q4 {
public static void main(String args[])
{
int i, j, k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print(" ");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
OUTPUT:-

Q5.
public class Q5 {
public static void main(String args[])
{
int i, j, k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print(" ");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print(i);
}
System.out.println("");
}
}
}
OUTPUT:-

Q6.
public class Q6 {
public static void main(String[] args) {
int n = 5;
int z = 1;
for(int i=1;i<=n;i++){
for(int j=n-1;j>=i;j--){
System.out.print(" ");
}
for(int k=z;k>=1;k--){
System.out.print(Math.abs(k-i)+1);
}
z+=2;
System.out.println();
}
}
}
public class Q6 {
public static void main(String[] args) {
int n = 5;
int z = 1;
for(int i=1;i<=n;i++){
for(int j=n-1;j>=i;j--){
System.out.print(" ");
}
for(int k=z;k>=1;k--){
System.out.print(Math.abs(k-i)+1);
}
z+=2;
System.out.println();
}
}
}
OUTPUT:-

Q19. Write a program to compute sinx for given x. The user should supply x and a positive integer n.
We compute the sine of x using the series and the computation should use all terms in the series up
through the term involving xn sin x = x - x 3 /3! + x5 /5! - x 7 /7! + x9 /9! .......

import java.util.Scanner;
public class Q19 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, j, n, fact, sign = -1;
float x, p, sum = 0;
System.out.print("Enter the value of x : ");
x = sc.nextInt();
System.out.print("Enter the value of n : ");
n = sc.nextInt();
for (i = 1; i <= n; i += 2) {
p = 1;
fact = 1;
for (j = 1; j <= i; j++) {
p = p * x;
fact = fact * j;
}
sign = -1 * sign;
sum += sign * p / fact;
}
System.out.println("sin : " + x + " = " + sum);
}
}
OUTPUT:-

Q20. Write a program to compute the cosine of x. The user should supply x and a positive integer n.
We compute the cosine of x using the series and the computation should use all terms in the series
up through the term involving xn cos x = 1 - x 2 /2! + x4 /4! - x 6 /6! ....

import java.util.Scanner;
public class Q20 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, j, n, fact, sign = -1;
float x, p, sum = 0;
System.out.print("Enter the value of x : ");
x = sc.nextInt();
System.out.print("Enter the value of n : ");
n = sc.nextInt();
for (i = 2; i <= n; i += 2) {
p = 1;
fact = 1;
for (j = 1; j <= i; j++) {
p = p * x;
fact = fact * j;
}
sum+=sign*p/fact;
sign=-1*sign;
}
System.out.println("cos : " + x + " = " + (1+sum));
}
}
OUTPUT:-

You might also like