0% found this document useful (0 votes)
12 views17 pages

Java Programs for Basic Calculations

The document contains a series of Java programs that perform various tasks such as checking if a number is even or odd, finding the largest of three numbers, calculating profit or loss, determining if a year is a leap year, and more. Each program utilizes the Scanner class for user input and implements conditional statements to execute different functionalities. The programs cover a wide range of basic programming concepts, including loops, conditionals, and arithmetic operations.

Uploaded by

tanishaagar2005
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)
12 views17 pages

Java Programs for Basic Calculations

The document contains a series of Java programs that perform various tasks such as checking if a number is even or odd, finding the largest of three numbers, calculating profit or loss, determining if a year is a leap year, and more. Each program utilizes the Scanner class for user input and implements conditional statements to execute different functionalities. The programs cover a wide range of basic programming concepts, including loops, conditionals, and arithmetic operations.

Uploaded by

tanishaagar2005
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

ASSIGNMENT-2

Q.1
import [Link];
class EvenOdd
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int num=[Link]();
if(num%2==0){
[Link](num+" is even");
}
else
[Link](num+" is odd");
}
}

Q2.
import [Link];
class Largest
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 3 numbers");
int num1=[Link]();
int num2=[Link]();
int num3=[Link]();
if(num1<num2&&num2>num3){
[Link]("Largest number is "+num2);}
else if(num1>num2&&num1>num3){
[Link]("Largest number is "+num1);
}
else{
[Link]("Largest number is "+num3);
}
}
}
Q.3.
import [Link];
class ProfitLoss
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter Cost price");
int CP=[Link]();
[Link]("Enter Selling Price");
int SP=[Link]();
if(CP>SP){
int L=CP-SP;
[Link]("Loss Of RS."+L);}
else{
int P=SP-CP;
[Link]("Profit of Rs."+P);}
}
}

Q.4.
import [Link];
class Divisible{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int a=[Link]();
if(a%5==0&&a%11==0){
[Link](a+ " is divisible by 5 and 11");}
else{
[Link](a+" is not divisible by 5 and 11");}
}
}
Q.5.
import [Link];
class Equal
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 3 numbers");
int num1=[Link]();
int num2=[Link]();
int num3=[Link]();
if(num1==num2&&num2==num3){
[Link]("All numbers are equal");}
else if(num1!=num2&&num2!=num3){
[Link]("All number are different");
}
else{
[Link](" Neither they are equal nor different");
}
}
}

Q.6.
import [Link];
class LeapYear
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter a year");
int a=[Link]();
if(a%100==0&&a%4==0){
if(a%400==0){
[Link](a+" Year is a leap year");}
else
[Link](a+"Not leap year");}
else
[Link](a+ "Year is not a leap year");
}
}

Q.7.
import [Link];
class Zero
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter a floating point number");
float num=[Link]();
if(num==0.0f)
[Link](num+" is Zero");
else if(num>0.0f)
[Link](num+ " is Positive");
else
[Link](num+" is Negative");
}
}

Q.8.
import [Link];
class Same
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 2 floating point number");
float num1=[Link]();
float num2=[Link]();
int a=[Link](num1*1000);
int b=[Link](num2*1000);
if(a==b)
[Link](num1+" is same as "+num2);
else
[Link](num1+" is not same as "+num2);
}
}

Q.9.
import [Link];
class Percentage
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter marks of Physics");
int Phy=[Link]();
[Link]("Enter marks of Chemistry");
int Che=[Link]();
[Link]("Enter marks of Biology");
int Bio=[Link]();
[Link]("Enter marks of Mathematics");
int Math=[Link]();
[Link]("Enter marks of Computer");
int Com=[Link]();
int sum=Phy+Che+Bio+Math+Com;
double per=(sum*100)/500;
[Link]("Percentage"+per);
if(per>=90.0)
[Link]("A");
else if(per>=80)
[Link]("B");
else if(per>=70)
[Link]("C");
else if(per>=60)
[Link]("D");
else if(per>=40)
[Link]("E");
else
[Link]("F");
}
}

Q.10.
import [Link];
class Triangle
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 3 numbers");
double a=[Link]();
double b=[Link]();
double c=[Link]();
if(a==b&&a==c)
[Link]("Equilateral Triangle ");
else if(a==b||b==c||a==c)
[Link]("Isosceles Triangle");
else
[Link]("Scalene Triangle");
}
}
Q.11.
import [Link];
class Character
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter a character");
char ch=[Link]().charAt(0);
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){
[Link](ch+" is character");
}
else
[Link](ch+" is not a character");
}
}

Q.12.
import [Link];
class Vowels
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter an Alphabet");
char ch=[Link]().charAt(0);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||
ch=='O'||ch=='U'){
[Link](ch+" is vowel");
}
else
[Link](ch+" is a consonant");
}
}

Q.13.
import [Link];
class Check
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter a character");
char ch=[Link]().charAt(0);
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
[Link](ch+" is Alphabet");
else if(ch>='1'&&ch<='9')
[Link](ch+" is a digit");
else
[Link](ch+" is special character");
}
}

Q.14.
import [Link];
class UpperLower
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter a Character");
char ch=[Link]().charAt(0);
if(ch>='a'&&ch<='z')
[Link](ch+" is Lowercase");
else if(ch>='A'&&ch<='Z')
[Link](ch+" is a Uppercase");
else
[Link](ch+" is not a alphabet");
}
}

Q.15.
import [Link];
class Rectangle
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter Length");
double l=[Link]();
[Link]("Enter Breath");
double b=[Link]();
double area=l*b;
double perimeter=2*(l+b);
if(area>perimeter)
[Link]("area of rectangle "+area+" is greater than perimeter of
rectangle "+perimeter);
else
[Link]("area of rectangle "+area+" is not greater than perimeter of
rectangle "+perimeter);
}
}

Q.16.
import [Link];
class Day
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 1 for Sunday, 2 for Monday, 3 for Tuesday, 4 for
Wednesday, 5 for Thursday, 6 for Friday ,7 for Saturday ");
int num=[Link]();
switch(num) {
case 1:
[Link]("The day is Sunday");
break;
case 2:
[Link]("The day is Monday");
break;
case 3:
[Link]("The day is Tuesday");
break;
case 4:
[Link]("The day is Wednesday");
break;
case 5:
[Link]("The day is Thursday");
break;
case 6:
[Link]("The day is Friday");
break;
case 7:
[Link]("The day is Saturday");
break;
default:
}
}
}

Q.17.
import [Link];
class Calculator
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 2 numbers for calculation");
int a=[Link]();
int b=[Link]();
[Link]("Enter + for addition,- for subtraction ,x for multiplication,/ for
division ");
char ch=[Link]().charAt(0);
switch(ch) {
case '+':
[Link](a+b);
break;
case '-':
[Link](a-b);
break;
case 'x':
[Link](a*b);
break;
case '/':
[Link](a/b);
break;
default:
}
}
}
Q.18.
import [Link];
class Random
{
public static void main(String args[]){
[Link]("Random number is= "+[Link]());
}
}

Q.19.
import [Link];
class Roots
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 3 numbers");
double a=[Link]();
double b=[Link]();
double c=[Link]();
[Link]("Quadratic equation is ");
[Link](a+"x^2"+b+"x"+c+"=0");
double r=(b*b)-(4*a*c);
if(a==0)
[Link]("Roots can not be calculated ");
else if(r>0){
double root1=(-b+[Link](r))/(2*a);
double root2=(-[Link](r))/(2*a);
[Link]("Roots are "+root1+" "+root2);}
else{
double root1=(-b)/(2*a);
[Link]("Root is "+root1);
}
}
}

Q.20.
import [Link];
class Months
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 1 for January,2 for february,3 for march,4 for april,5for
may,6 for June,7 for July,8 for august,9 for September,10 for october,11 for
November,12 for december ");
int mon=[Link]();
switch(mon){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
[Link]("Month has 31 days");
break;
case 2:
[Link]("Month has 28 days");
break;
case 4:
case 6:
case 9:
case 11:
[Link]("Month has 30 days");
break;
default:
}
}
}
Q.21.
import [Link];
class Alphabet
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link](" Alphabet ");
char ch=[Link]().charAt(0);
switch(ch){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
[Link](ch+" is a vowel");
break;
default:
[Link](ch+" is a consonant");
}
}
}

Q.22.
import [Link];
class Months
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter 1 for January,2 for february,3 for march,4 for april,5for
may,6 for June,7 for July,8 for august,9 for September,10 for october,11 for
November,12 for december ");
int mon=[Link]();
switch(mon){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
[Link]("Month has 31 days");
break;
case 2:
[Link]("Month has 28 days");
break;
case 4:
case 6:
case 9:
case 11:
[Link]("Month has 30 days");
break;
default:
}
}
}

Q.23.
import [Link];
import [Link];
class Game1
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
Random random=new Random();
[Link]("Enter user choice from rock,paper,scissor");
String s=[Link]();
String[] choice={"rock","paper","scissor"};
String cc=choice[[Link](3)];
[Link]("Computer choice "+cc);
if([Link](cc))
[Link]("Its a tie");
else if([Link]("rock")&&[Link]("scissor")||
[Link]("scissor")&&[Link]("paper")||[Link]("paper")&&[Link]("rock"))
[Link]("User is Winner");
else if([Link]("rock")||[Link]("scissor")||[Link]("paper"))
[Link]("Computer is winner");
else
[Link]("Invalid choice");
}
}

Q.24.
import [Link];
import [Link];
class Game2
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
Random random=new Random();
[Link]("Enter user choice from bat,ball,stump");
String s=[Link]();
String[] choice={"bat","ball","stump"};
String cc=choice[[Link](3)];
[Link]("Computer choice "+cc);
if([Link](cc))
[Link]("Its a tie");
else if([Link]("bat")&&[Link]("ball")||[Link]("ball")&&[Link]("stump")||
[Link]("stump")&&[Link]("bat"))
[Link]("User is Winner");
else if([Link]("bat")||[Link]("ball")||[Link]("stump"))
[Link]("Computer is winner");
else
[Link]("Invalid choice");
}
}

Common questions

Powered by AI

The decision structure uses cascading `if-else` statements to compare three numbers. If the first number is less than the second and the second is greater than the third, the second is largest. Similar logic applies to other comparisons. However, it does not handle cases where two or three numbers are equal, potentially leading to incorrect deductions. Introducing `if-else` conditions to specifically check for numerical equality before comparing magnitudes can prevent such errors and ensure accurate outcomes .

The program determines profit or loss by comparing the cost price (CP) with the selling price (SP). If CP is greater than SP, it calculates and prints the loss; otherwise, it calculates and prints the profit. This method is straightforward but sensitive to input accuracy. Computational limitations involve integer overflow for very large values and a lack of handling for floating-point arithmetic precision that might apply in real-world currency computations. Adjusting data types for broader range numbers and including input validation can enhance its efficacy .

The algorithm determines leap years by first checking if the year is divisible by 100 and 4 but not divisible by 400 to reject centurial non-leap years. This comprehensive check addresses typical miscalculations, correctly identifying century rules as per Julian and Gregorian calendar transitions, which is significant in historical analyses requiring precise date alignment for event timelines or periodic calculations in datasets across centuries .

The program calculates the percentage of marks from five subjects, assigns grades A to F based on thresholds mimicking standard educational models. To enhance adaptability, the program could incorporate variable credit weights per subject or percentile-based grading configurations, accommodating diverse systems like curved grading to better reflect relative academic performance across varied institution standards .

The Java program checks if a number is even or odd by using the modulus operator `%` to determine if the remainder is zero when the number is divided by two. If true, it is deemed even; otherwise, it is odd. The logic effectively handles all integer inputs. However, the program could be improved by considering the handling of negative numbers and large integers, ensuring the accuracy of output results for non-standard inputs .

The program checks floating-point numbers directly using `if-else` conditions to classify zero, positive, or negative statuses. Though typically effective, floating-point arithmetic's imprecisions could lead to classification errors in small magnitudes near zero or when handling billionth fractions, where precision representation limits cause inadvertent misclassification. Employing epsilon tolerance methods could mitigate precision issues by defining margins of error for equality checks .

Randomness, through pre-defined choices and randomness functions, injects unpredictability and fairness into outcomes, essential for simulating authentic gameplay experiences against a computer. Enhancements could include predictive algorithms to increase challenge, adaptive difficulty settings reacting to player's historical choices, or introducing novel variables/rules to prolong interest, offering deeper engagement through increased cognitive challenge and wider interaction possibilities .

By using switch-case, the program efficiently maps numeric inputs directly to corresponding month or weekday names, reducing comparisons needed in if-else chains. This is advantageous where specific fixed mappings are required, minimizing syntax complexity and increasing readability, as seen with calendars. In gaming scenarios involving defined levels or state machines with predetermined transitions, switch-case structures offer faster data access and streamlined transitions relative to compound if-else chains .

The program checks divisibility by evaluating the modulus `%` of the number with 5 and 11. If both results yield zero, the number is divisible by both. This double condition check is linear with O(1) complexity per number, as modulus operations are constant time. Although efficient for single checks, batch processing optimization might involve pre-checking numbers or leveraging elementary number theory for sequences, reducing redundant calculations in larger datasets. Yet within individual operations, this remains optimal .

The program classifies numbers using `if-else` conditions based on equality checks, distinguishing between all numbers being equal, all being different, or neither when only two numbers match. Real-world applications might include sorting algorithms where initial conditions optimize branch paths, or automated systems for classifications, where predetermined outcomes must quickly handle diverse datasets by comparing entries distinctly or uniformly .

You might also like