0% found this document useful (0 votes)
16 views8 pages

Java Programs for Basic Algorithms

My projects

Uploaded by

tkghrrbgh2
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)
16 views8 pages

Java Programs for Basic Algorithms

My projects

Uploaded by

tkghrrbgh2
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

Program:

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

}
}

Output:
Program:
public class ReverseNo

{
public static void main(String[] args)
{
int number = 987654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
[Link]("The reverse of the given number
is: " + reverse);
}
}

Output:
Program:
import [Link].*;
class Fibonacci
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
[Link](n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0


and 1 are already printed
{
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;
}

}
}

Output:
Program:
import [Link].*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of
String class
Scanner in = new Scanner([Link]);
[Link]("Enter a string/number to
check if it is a palindrome");
original = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + [Link](i);
if ([Link](reverse))
[Link]("Entered string/number is
a palindrome.");
else
[Link]("Entered string/number
isn't a palindrome.");
}
}

Output:
Program:
public class RightTrianglePattern
{
public static void main(String args[])
{
//i for rows and j for columns
//row denotes the number of rows you want to print
int i, j, row=6;
//outer loop for rows
for(i=0; i<row; i++)
{
//inner loop for columns
for(j=0; j<=i; j++)
{
//prints stars
[Link]("* ");
}
//throws the cursor in a new line after printing
each line
[Link]();

}
}
}

Output:
Program:
import [Link];
public class Operations
{
public static void main(String args[])
{
Scanner y = new Scanner([Link]);
while(true)
{
[Link]("");
[Link]("Enter the two numbers to
perform operations ");
[Link]("Enter the first number : ");
int x = [Link]();
[Link]("Enter the second number : ");
int c = [Link]();
[Link]("Choose the operation you want to
perform ");
[Link]("Choose 1 for ADDITION");
[Link]("Choose 2 for SUBTRACTION");
[Link]("Choose 3 for MULTIPLICATION");
[Link]("Choose 4 for DIVISION");
[Link]("Choose 5 for MODULUS");
[Link]("Choose 6 for EXIT");
int n = [Link]();
switch(n)
{
case 1:
int add;
add = x + c;
[Link]("Result : "+add);
break;
case 2:
int sub;
sub = x - c;
[Link]("Result : "+sub);
break;
case 3:
int mul;
mul = x * c;
[Link]("Result : "+mul);
break;

case 4:
float div;
div = (float) x / c;
[Link]("Result : "+div);
break;
case 5:
int mod;
mod = x % c;
[Link]("Result : "+mod);
break;
case 6:
[Link](0);
}
}
}
}
Output:

You might also like