You are on page 1of 33

Name:-Darshan Ajit Wakurde

Rollno:-20220201022
MCA Div-A
—---------------------------------------------------------------------------------------

Question 1
Even or Odd
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.*;

class odd

{ public static void main(String arg[])

Scanner in=new Scanner(System.in);

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

int num=in.nextInt();

if(num%2==0)

{ System.out.println(" Number is Even");

}else

System.out.println("Number is Odd");

}
Question 2
Absolute value
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 abs {


public static void main(String[] args) {
System.out.println("Enter the NUmber");
Scanner in=new Scanner(System.in);
int a=in.nextInt();
System.out.println("The Absolute value is:
"+Math.abs(a));
}
}

Output:
Question 3
Discount and Revenue
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.*;
public class revenew {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("enter the price of product");
int proPrice=in.nextInt();
System.out.println("enter the quantity of product");
int quantity=in.nextInt();

int revenew=proPrice*quantity;

if(revenew>5000){
double rew=revenew*0.1;
revenew-=rew;

}
System.out.println("The revenew generated is
:\n"+revenew);
}
}
Question 4
Largest Number
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 largest {


public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("enter the number");
int a=in.nextInt();
System.out.println("enter the number");
int b=in.nextInt();
System.out.println("enter the number");
int c=in.nextInt();

if(a>b && a>c){


System.out.println("a is largest number a="+a);

}
else if(b>a&&b>c){
System.out.println("b is largest number b="+b);
}
else {
System.out.println("c is largest number c="+c);
}

}
}
Question 5
Positive, negative or zero
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.
public class positive

public static void main(String arg[])

Scanner in=new Scanner(System.in);

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

int num=in.nextInt();

if(num>0)

System.out.println("Number is Positive");

else if(num==0)

System.out.println("Number is Zero");

else

System.out.println("Number is Negative");

}
Question 6
Valid Triangle
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 triange {


public static void main(String[] args) {
Scanner in =new Scanner(System.in);
System.out.println("Enter the number ");
int ang1=in.nextInt();
System.out.println("Enter the number ");
int ang2=in.nextInt();
System.out.println("Enter the number ");
int ang3=in.nextInt();

int add=ang1+ang2+ang3;
if(add==180){
System.out.println("This is Tringle!!");
}
else{
System.out.println("Not a Triangle");
}
}
}
Question 7
Leap Year
Any year is input by the user. Write a program to determine whether
the year is a leap year or
Not.

import java.util.Scanner;

public class leap {


public static void main(String[] args) {
Scanner in=new Scanner (System.in);
System.out.println("Enter the year");
int year=in.nextInt();
if(year%4==0&&(year%100!=0||year%400==0)){
System.out.println("The year is leap year !!!!: "+
year);
}
else{
System.out.println("The year is not leap year: "+year);
}
}
}
Question 8
Telephone Bill
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_bill {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
System.out.println("Enter the calls");
int call=in.nextInt();
double num = 0;
double tepnum;
int bill=200;
if(call<=100){
System.out.println("Bill rs 200");
}
else if(call>100&&call<=150){
num=call-100;
num=num*0.60;
num+=200;
}
else if(call>100&&call<=200) {
num=call-100;
tepnum=num*0.60;
num=call-150;
num=num*0.50;
num+=200+tepnum;
}
else{
num=call-100;
tepnum=num*0.60;
num=call-150;
double tempno2=num*0.50;
num=call-100;
num=num*0.40;
num+=200+tepnum+tempno2;
}
System.out.println("The bill is:"+num);
}
}
output
Question 9
Grade Calculator
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:

import java.util.*;

class grade2

public static void main(String args[])

Scanner in=new Scanner(System.in);

System.out.println("Enter Grade: ");

String a=in.nextLine();

char c=a.charAt(0);

if(c=='A')

System.out.print("Excellent");

else if(c=='B')

System.out.print("Good");

else if(c=='C')

System.out.print("Average");

else if(c=='D')
{

System.out.print("Deficine");

else if(c=='E')

System.out.print("Failing");

else

System.out.print("Invalid Grade");

}
10 Meaning of Grade
Write a program that prompts the user to enter grade. Your
program should display the
corresponding meaning of grade as per the following table

import java.util.*;

class grade2

public static void main(String args[])

Scanner in=new Scanner(System.in);

System.out.println("Enter Grade: ");

String a=in.nextLine();

char c=a.charAt(0);

if(c=='A')

System.out.print("Excellent");

else if(c=='B')

System.out.print("Good");

else if(c=='C')

System.out.print("Average");

else if(c=='D')

System.out.print("Deficine");

}
else if(c=='E')

System.out.print("Failing");

else

System.out.print("Invalid Grade");

}
Question 11
Descending Order Names
Write a program that prompts the user to enter three names.
Your program should display the
names in descending order.

import java.util.Scanner;
import java.util.concurrent.atomic.AtomicIntegerArray;

public class NamesInDecending {


public static int MAX_CHAR = 26;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] str = new String[3];
str[0] = in.nextLine();
str[1] = in.nextLine();
str[2] = in.nextLine();
int k = 0;
while (k < 3) {
int i;

int charCount[] = new int[MAX_CHAR];


int a = str[k].length();
for (i = 0; i < str[k].length(); i++) {
charCount[str[k].charAt(i) - 'a']++;
}
for (i = MAX_CHAR - 1; i >= 0; i--) {
for (int j = 0; j < charCount[i]; j++) {
System.out.print((char) ('a' + i));
}
}
System.out.println();
k++;
}
}
}
Question 2 Write a program to calculate the sum of first 10 natural number.

Code:

import java.util.*;

public class loop2

public static void main(String[] args)

int i, num = 10, sum = 0;

for(i = 1; i <= num; ++i)

sum = sum + i;

System.out.println("Sum of First 10 Natural Numbers is = " + sum);

}
Question 3 Write a program that prompts the user to input a positive integer. It should then print
the multiplication table of that number.

Code:

import java.util.*;

public class loop3

public static void main(String[] args)

Scanner in = new Scanner(System.in);

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

int n=in.nextInt();

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

System.out.println(n+"*"+i+" = "+n*i);

}
Question 4 Write a program to find the factorial value of any number entered through the keyboard.

Code:

import java.util.*;

public class loop4

public static void main(String arg[])

Scanner in=new Scanner(System.in);

System.out.print("Enter Vallue: ");

int num1=in.nextInt();

int fact=1;

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

fact=fact*i;

System.out.println("Factorial of "+num1+" is: "+fact);

}
Question 4
Write a program to find the factorial value of any number entered
through the keyboard.

import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
System.out.println("Enter the number for factorial");
int num=in.nextInt();
int fact=1;

for(int i=1;i<=num;i++){
fact=fact*i;
}
System.out.println("The Factorial is:"+fact);
}
}
Question 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 power {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the num");
int num1=in.nextInt();
System.out.println("Enter the power");
int num2=in.nextInt();
int ans=1;
int i=0;
while(i<num2){
ans*=num1;
i++;
}
System.out.println("The power is:"+num2+"\nThe number is:"+num1+"\nans
is:"+ans);
}
}
Question 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 question6 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the number ");
int num=in.nextInt();
int rev=0;
while(num!=0){
int rem=num%10;
rev=(rev*10)+rem;
num=num/10;
}
System.out.println("The reverse order is:");
System.out.println(rev);
}
}
Question 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 SumofOddEven {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Scanner sc= new Scanner(System.in);
int flag = 1;
int sumodd=0;
int sumeven=0;
while (flag != 0) {
int num= in.nextInt();
if(num%2==0) {
sumeven+=num;
}
else{
sumodd+=num;
}
System.out.println("enter yes or no");
String con1=sc.nextLine();
char con=con1.charAt(0);
if(con=='n'){
flag=0;
System.out.println("Sum of ood is: "+sumeven);
System.out.println("Sum of ood is: "+sumodd);
}
else{
flag=1;
}
}
}
}

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

import javax.sound.midi.Soundbank;
import java.util.Scanner;
public class Hcf {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int Hcf=0;
System.out.println("Enter the Number");
int num1=in.nextInt();
System.out.println("Enter the Number");
int num2=in.nextInt();
for(int i=1;i<=num1||i<=num2;i++){
if(num1%i==0 &&num2%i==0){
Hcf=i;
}
}
System.out.println("The Hcf is: "+Hcf);
}
}
Question 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 IntegerCount {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int flag= 1;
int pos = 0;
int neg = 0;
int ze = 0;
do {
Scanner sc = new Scanner(System.in);
System.out.println("Enter thr number");
int num = in.nextInt();
if (num > 0) {
pos++;
} else if (num < 0) {
neg++;
} else {
ze++;
}
String con1=in.next();
char con=con1.charAt(0);
if(con=='n'){
flag=0;
System.out.println("positive: "+pos+" negative: "+neg+"
zero: "+ze);
}
else{
flag=1;
}
}while(flag!=0);
}
}

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 )

import java.util.Scanner;
public class amstrong {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter thr number");
int num=in.nextInt();
int temp=num;
double ans=0;
double count=0;

while(num!=0){
num=num/10;
count++;
}
num=temp;
while(num!=0) {
double rem = num % 10;
num = num / 10;
ans+=Math.pow(rem,count);
}
System.out.println(ans);
num=temp;
if(num==ans){
System.out.println("The Number is Amstrong: "+ans);
}
else{
System.out.println("The number is not Armstrong: "+num);
}
}
}

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 fibonacci {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("enter the number");
int num=in.nextInt();
int i=0;
int next=1;
int temp=0;
System.out.println("Fibonacci series is: ");
System.out.println(i);
System.out.println(next);

while(i<num){
temp=next;
next=next+i;

System.out.println(next);
i=temp;
}
}
}

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 NumberFractional {
public static void main(String[] args) {
double sum=0;
Scanner in=new Scanner(System.in);
int num=in.nextInt();
for (double i=1;i<=num;i++)
{
sum=sum+1/i;
}
System.out.println("The sum is: "+sum);
}
}
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 twoaddfact{


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);
}
}

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 GuessGame {
public static void main(String[] args) {
int flag=1;
int a=17;
Scanner in=new Scanner(System.in);
while(flag!=0){
System.out.println("Enter the Number");
int num=in.nextInt();
if(num>a){
System.out.println("The number is grater");
} else if (num<a) {
System.out.println("The number is Smaller");
}
else{
System.out.println("Congrats you got it");
flag=0;
}
}
}
}

Write a program to print following :

public class patterns {


public static void main(String[] args) {
int i=0;
int j=0;
int k=0;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
System.out.print("*");
}
System.out.println();
}
//------------------------------------------------------------
for(i=0;i<=5;i++){
for(j=0;j<i;j++){
System.out.print("*");
}
System.out.println();
}
System.out.println();
int num=5;
int num2=num;
//--------------------------------------------------------------------
for(i=0;i<=num-1;i++){
k=0;
for(j=0;j<num;j++) {
while (k < num2-1) {
System.out.print(" ");
k++;
j = k;
}

System.out.print("*");
}
System.out.println();
num2--;
}

//------------------------------------------------------------------------
System.out.println();
int n=5;
for( i=0;i<=n;i++){
for(j=n-i;j>0;j--){
System.out.print(" ");
}
for(j=0;j<=i;j++){
System.out.print("* ");
}
System.out.println();
}

System.out.println();
//--------------------------------------------------------

for( i=1;i<=n;i++){
for(j=n-i;j>0;j--){
System.out.print(" ");
}
for(j=1;j<=i;j++){
System.out.print(i+" ");
}
System.out.println();
}
}
}
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 - x3/3! + x5/5! - x7/7! + x9/9! .......

import java.util.Scanner;

public class sin{

public static void main(String[] args) {

Scanner in = new Scanner(System.in);


int i, j, n, fact, sign = -1;

float x, p, sum = 0;

System.out.println("Enter the value of x : ");

x = in.nextFloat();

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

n = in.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);

}
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 - x2/2! + x4/4! - x6/6! .....

import java.util.Scanner;

public class cos {


public static void main(String[] args) {
Scanner in=new Scanner(System.in);

int i,j,n,fact,sign=-1;

float x, p,sum=0;
System.out.println("Enter the value of x : ");;

x=in.nextInt();

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

n=in.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);
}
}

You might also like