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

Java Programs for Basic Algorithms

The document contains Java programs that demonstrate various basic programming concepts, including compound interest calculation, checking odd or even numbers, determining the day of the week, calculating factorials, summing digits, identifying prime numbers, generating multiplication tables, producing Fibonacci series, reversing numbers, finding the largest of two numbers, calculating simple interest, counting digits, summing natural numbers, printing even numbers, and reversing an array. Each program is structured with a main method and utilizes the Scanner class for user input. The examples illustrate fundamental programming techniques and logic in Java.

Uploaded by

tripathidhruv96
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)
8 views3 pages

Java Programs for Basic Algorithms

The document contains Java programs that demonstrate various basic programming concepts, including compound interest calculation, checking odd or even numbers, determining the day of the week, calculating factorials, summing digits, identifying prime numbers, generating multiplication tables, producing Fibonacci series, reversing numbers, finding the largest of two numbers, calculating simple interest, counting digits, summing natural numbers, printing even numbers, and reversing an array. Each program is structured with a main method and utilizes the Scanner class for user input. The examples illustrate fundamental programming techniques and logic in Java.

Uploaded by

tripathidhruv96
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

1.

Compound Interest
Java Program:
import [Link].*;
class CI {
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
double p=[Link](), r=[Link](), t=[Link]();
double ci=p*[Link]((1+r/100),t)-p;
[Link](ci);
}
}

2. Check Odd or Even


import [Link].*;
class OddEven{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
if(n%2==0) [Link]("Even");
else [Link]("Odd");
}
}

3. Day of Week using Switch


import [Link].*;
class Day{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int d=[Link]();
switch(d){
case 1:[Link]("Sunday");break;
case 2:[Link]("Monday");break;
case 3:[Link]("Tuesday");break;
case 4:[Link]("Wednesday");break;
case 5:[Link]("Thursday");break;
case 6:[Link]("Friday");break;
case 7:[Link]("Saturday");break;
}
}
}

4. Factorial
import [Link].*;
class Fact{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link](), f=1;
for(int i=1;i<=n;i++) f*=i;
[Link](f);
}
}

5. Sum of Digits
import [Link].*;
class SumDigit{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link](), s=0;
while(n>0){ s+=n%10; n/=10; }
[Link](s);
}
}

6. Prime Number
import [Link].*;
class Prime{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link](), c=0;
for(int i=1;i<=n;i++) if(n%i==0) c++;
if(c==2) [Link]("Prime");
else [Link]("Not Prime");
}
}

7. Table of a Number
import [Link].*;
class Table{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
for(int i=1;i<=10;i++)
[Link](n*i);
}
}

8. Fibonacci Series
class Fib{
public static void main(String[] args){
int a=0,b=1;
for(int i=1;i<=10;i++){
[Link](a+" ");
int c=a+b; a=b; b=c;
}
}
}

9. Reverse a Number
import [Link].*;
class Rev{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link](), r=0;
while(n>0){ r=r*10+n%10; n/=10; }
[Link](r);
}
}

10. Largest of Two Numbers


import [Link].*;
class Large{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int a=[Link](), b=[Link]();
[Link](a>b?a:b);
}
}

11. Simple Interest


import [Link].*;
class SI{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
double p=[Link](), r=[Link](), t=[Link]();
[Link]((p*r*t)/100);
}
}

12. Count Digits


import [Link].*;
class Count{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link](), c=0;
while(n>0){ c++; n/=10; }
[Link](c);
}
}

13. Sum of Natural Numbers


import [Link].*;
class SumN{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link](), s=0;
for(int i=1;i<=n;i++) s+=i;
[Link](s);
}
}

14. Even Numbers 1 to 100


class Even{
public static void main(String[] args){
for(int i=2;i<=100;i+=2)
[Link](i+" ");
}
}

15. Array Reverse


class ArrRev{
public static void main(String[] args){
int a[]={10,20,30,40,50};
for(int i=[Link]-1;i>=0;i--)
[Link](a[i]+" ");
}
}

Common questions

Powered by AI

To adapt the prime number checker to handle multiple inputs, the program could be modified to accept a list or array of integers instead of a single integer input. Then, it would iterate over each number in the list, applying the same logic to determine primality, and store results in a secondary array or list. Finally, it would output the primality status next to each input number, possibly in a tabular or serialized format . This batch processing would allow for efficient, simultaneous evaluation and display of results.

The program determines if a number is odd or even by reading an integer input and calculating its remainder when divided by 2. If the remainder is 0, it prints "Even"; otherwise, it prints "Odd" . The computational cost of this operation is O(1) because it involves a single arithmetic computation and a conditional check.

The program takes user input for the principal amount (p), the rate of interest (r), and the time period (t), and then uses the formula for compound interest: CI = p * (1 + r/100)^t - p, to calculate and print the compound interest . For handling more sophisticated scenarios, the program could be expanded to include options for different compounding intervals (e.g., monthly, quarterly), support different interest rates for different periods, and incorporate input validation and error handling routines.

The switch-case statement in the program assigns each integer input (from 1 to 7) to a corresponding day of the week by sequentially matching the input with case labels (1 for "Sunday", 2 for "Monday", etc.) and printing the associated day when a match occurs . This provides a clear, direct mapping from numeric input to day output.

The computational efficiency of calculating the sum of natural numbers up to n using a loop is O(n), as it iterates through all numbers from 1 to n summing them . However, the sum can be more efficiently computed using the mathematical formula n(n + 1)/2, which has a constant time complexity, O(1), thereby significantly reducing computational time for large n.

The provided Java program checks if a number is prime by counting the number of divisors it has, with a loop running from 1 to the number itself, and concludes the number is prime if it has exactly two divisors . This is inefficient for large numbers. To optimize, one can check divisibility from 2 to the square root of the number, as any factor larger than its square root would have a complementary factor smaller than the square root, thus reducing the computation time significantly.

The program prints even numbers from 1 to 100 by starting a loop at 2 and incrementing by 2 on each iteration, thus directly stepping through even numbers only until it reaches 100 . Alternative methods could involve using a conditional within a loop that iterates through all numbers between 1 and 100, checking each for evenness, though this would be less efficient.

The program reverses the contents of an array by iterating backwards from the last element to the first, printing each element . Unlike reversing a number, where digits are manipulated via arithmetic operations, array elements are accessed via indexing, which allows direct backward traversal rather than digit extraction.

The algorithm calculates the factorial of a given number by initializing a variable f to 1 and multiplying it by each integer from 1 to n progressively . The time complexity of this approach is O(n) as it involves a single for-loop that iterates n times, where n is the input number.

The program reverses a number by repeatedly taking the last digit (using modulus operation), appending it to a result, and removing this digit from the original number (using integer division) until no digits remain . For someone unfamiliar with programming: imagine peeling off each digit from the end of a number and placing them one by one in a new reversed order.

You might also like