0% found this document useful (0 votes)
8 views5 pages

Java Programs for Basic Math Operations

The document contains multiple Java classes that implement various mathematical operations. These include calculating Fibonacci numbers, the sum of digits, factorials, grading based on marks, checking for prime numbers, and summing even numbers or all numbers within a given range. Each class utilizes the Scanner class for user input and outputs the results to the console.

Uploaded by

khushi85012
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)
8 views5 pages

Java Programs for Basic Math Operations

The document contains multiple Java classes that implement various mathematical operations. These include calculating Fibonacci numbers, the sum of digits, factorials, grading based on marks, checking for prime numbers, and summing even numbers or all numbers within a given range. Each class utilizes the Scanner class for user input and outputs the results to the console.

Uploaded by

khushi85012
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

Fibonacci

import [Link];
class Fibonacci{
public static void main(String args[]){

Scanner myObj=new Scanner([Link]);


[Link]("enter number of iteration");
int a=[Link]();
if(a==1){
[Link]("1");
}
else{
int n1=1;
int n2=1;
[Link]("1 ");
[Link]("1 ");
for(int i=2;i<a;i++){
int c=n1+n2;
[Link](" ");
[Link](c);
n1=n2;
n2=c;
}
}

SumOfDigit

import [Link];
class sumOfDigit{
public static void main(String args[]){

Scanner myObj=new Scanner([Link]);


[Link]("enter number");
int n=[Link]();
int sum=0;
int rem=0;
while(n!=0){
rem=n%10;
sum=sum+rem;
n=n/10;
}
[Link]("sum of digits is:");
[Link](sum);
}}

Factorial
import [Link];
class Factorial{
public static void main(String args[]){

Scanner myObj=new Scanner([Link]);


[Link]("enter number");
int n=[Link]();
int fact=1;

for(int i=1;i<=n;i++){
fact=fact*i;
}

[Link]("Factorial of ");

[Link](n);
[Link](" is ");
[Link](fact);

}}

Marksheet
import [Link];

class marksheet{
public static void main(String argc[]) {

Scanner myObj = new Scanner([Link]);


[Link]("Grade\n");
[Link]("Enter mark\n");
int a=[Link]();

if(a>=90){
[Link]("A");
}
else if(a>=70){
[Link]("B");
}
else if(a>=50){
[Link]("C");
}
else if(a<=33){
[Link]("FP");
}
else{
[Link]("D");
}

}
}

Prime
import [Link];
class Prime{
public static void main(String args[]){

Scanner myObj=new Scanner([Link]);


[Link]("enter number");
int a=[Link]();
if(a==2){
[Link]("it is a prime number");
}else{
int i=2;
int n=0;
while(i<a)
{

if(a%i==0){
[Link]("not a prime number");
n=1;
break;
}

i++;

if(n==0){
[Link]("it is a prime number");}
}
}}

Sum of even numbers within given range

import [Link];
class sumOfEven{
public static void main(String argc[]) {

Scanner myObj = new Scanner([Link]);

[Link]("Enter first number\n");


int a=[Link]();
[Link]("Enter second number\n");
int b=[Link]();
int sum=0;
for(int i=a;i<=b;i++){
if(i%2==0){
sum=sum+i;}
}
[Link]("sum within given range is:"+sum);

}}

Sum of numbers in given range

import [Link];
class sumOfGivenRange{
public static void main(String argc[]) {

Scanner myObj = new Scanner([Link]);

[Link]("Enter first number\n");


int a=[Link]();
[Link]("Enter second number\n");
int b=[Link]();
int sum=0;
for(int i=a;i<=b;i++){
sum=sum+i;
}
[Link]("sum within given range is:"+sum);

}}

Common questions

Powered by AI

The program uses a for-loop, iterating from the first to the second input number, checking each number's evenness with the modulus operation (i%2==0). If true, the number is added to a cumulative sum. This showcases basic iterative structures in Java, facilitating controlled repeated execution based on numeric ranges and conditions .

The marksheet program employs a series of if-else-if conditional statements to map numeric scores to letter grades. It prioritizes conditions from highest to lowest (A for scores >=90, B for scores >=70, C for scores >=50, FP for scores <=33), and has a catch-all else statement for scores between 34 and 49, assigning a grade of D. This structure ensures each score range is uniquely addressed .

To improve the program for negative and non-consecutive ranges, modifications could include checking if the second input is smaller than the first, swapping values if necessary, and handling negative ranges by adjusting the loop condition or omitting negative sums if unwanted. These changes would ensure accurate results regardless of input order and extend functionality to realistic negative range scenarios .

The sumOfGivenRange program calculates the sum of all integers in the specified range, while the sumOfEven program only sums even integers, using a conditional (i%2==0) to filter out odd numbers. These differences illustrate the application of conditional logic to modify behavior of iterative processes, demonstrating the critical role of logical conditions in decision-making within loops .

The code snippets lack explicit error handling mechanisms for capturing invalid user inputs, such as non-integer values when integer input is expected. To improve, input validation should be added, such as using try-catch blocks to handle exceptions like InputMismatchException, which can guide users to input appropriate data types, enhancing robustness and user experience .

Modifying the factorial program to include step-wise output (printing each multiplication step) would involve adding a print statement inside the loop detailing the current factorial calculation. This could help learners understand iterative processes, as it gives a visual trace of how intermediate results build towards the final factorial, improving comprehension of loop mechanics and multiplication .

The prime-checking algorithm iterates from 2 to the input number minus one, checking divisibility, which can be inefficient for large numbers. Optimizations could include stopping checks at the square root of the number and skipping even divisors after checking for divisibility by 2, reducing unnecessary computations and improving performance .

The provided Fibonacci implementation generates the sequence using iteration rather than recursion, which is common in mathematical definitions of Fibonacci. This approach optimizes computational efficiency, as recursion can lead to increased overhead and potential stack overflow due to deep recursive calls. Consequently, this method is more practical for larger numbers, highlighting a trade-off between mathematical simplicity and computational efficiency .

The Scanner class is used across all programs to facilitate user input by reading input data from standard input (keyboard). It provides methods to parse primitive types and strings using regular expressions, thus allowing users to dynamically provide input, such as the number of iterations for Fibonacci or numbers in a range, making the programs more interactive and flexible .

The sumOfDigit program processes each digit of an integer through iteration, dividing the integer by 10 each time, thus having a time complexity proportional to the number of digits, O(log n). For very large numbers, this could be inefficient, especially in languages or environments where large integer arithmetic is slower, highlighting how integer size impacts computational load .

You might also like