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

Java Programs for Common Algorithms

The document contains Java programs demonstrating various algorithms including checking for Armstrong numbers, palindromes, factorial calculation, Fibonacci series generation, even or odd number determination, swapping numbers and strings, reversing strings, checking for prime numbers, finding the largest number in an array, sorting an array, and counting words in a string. Each program is presented with its respective class and main method implementation. The examples illustrate fundamental programming concepts and operations in Java.

Uploaded by

archanaother6
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 views12 pages

Java Programs for Common Algorithms

The document contains Java programs demonstrating various algorithms including checking for Armstrong numbers, palindromes, factorial calculation, Fibonacci series generation, even or odd number determination, swapping numbers and strings, reversing strings, checking for prime numbers, finding the largest number in an array, sorting an array, and counting words in a string. Each program is presented with its respective class and main method implementation. The examples illustrate fundamental programming concepts and operations in Java.

Uploaded by

archanaother6
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

1.

Program For Amstrong

public class Amstrong


{
public static void main(String[] args)
{
int temp,rem,num=153,sum=0;
temp=num;
while(num>0)
{
rem=num%10;
sum=(int) (sum+[Link](rem, 3));
num=num/10;
}
if(temp==sum)
{
[Link]("Amstrong.....");
}
else
{
[Link]("Non-Amstrong....");
}
}
}
[Link] For Palindrome.

public class Palindrome


{
public static void main(String[] args)
{
int num=4514,r,temp,sum=0;
temp=num;
while(num>0)
{
r=num%10;
sum=(sum*10)+r;
num=num/10;
}
if(temp==sum)
{
[Link]("Palindrome");
}
else
{
[Link]("Non-Palindrome");
}
}
}
[Link] For Factorial

public class Factorial


{
public static void main(String[] args)
{
int num=4,fact=1;
if(num==0)
{
[Link]("Factorial is 1");
}
else
{
for(int i=1;i<=num;i++)
{
fact=fact*i;
}
[Link]("Factorial:"+fact);
}
}
}
[Link] For Febonacci Series.

public class Febonacii_Series


{
public static void main(String[] args)
{
int a=0,b=1,c, num=5;
for(int i=0;i<num;i++)
{
[Link](a+" ");
c=a+b;
a=b;
b=c;
}
}
}
[Link] For Even Or Odd

public class Even_no


{
public static void main(String[] args) throws IOException
{
BufferedReader b=new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter number:");
int num=[Link]([Link]());
if(num%2==0)
{
[Link]("Even Number....");
}
else
{
[Link]("Odd Number....");
}
}
}
[Link] For Swapping of two numbers without third variable.

public class Swap_nos


{
public static void main(String[] args)
{
int x,y,z;
x=10;y=20;
[Link]("X: "+x);
[Link]("Y: "+y);
x=x+y;
y=x-y;
x=x-y;
[Link]("X: "+x);
[Link]("Y: "+y);
}
}
[Link] for reverse string

public class ReverseString


{
public static void main(String[] args)
{
String msg="Gauri Kulkarni";
String s="";
for(int i=[Link]()-1;i>=0;i--)
{
s=s+[Link](i);
}
[Link](s);
}
}
[Link] for checking wether no is Prime or not.

public class Prime_No


{
public static void main(String[] args)
{
int num=10;
boolean flag=false;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
flag=true;
break;
}
}
if(!flag)
{
[Link]("Prime
Number...");
}
else
{
[Link]("Non Prime...");
}
}
}
[Link] to find largest numer in array.

public class LargestNo_array


{
public static void main(String[] args)
{
int a[]= {90,10,50,20,60};
int size=[Link];
[Link](size);
int temp;
for(int i=0;i<[Link];i++)
{
for(int j=i+1;j<[Link];j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
[Link](a[size-1]);
}
}
[Link] to sort array.

public class Sort_Array


{
public static void main(String[] args)
{
int a[]={30,50,20,40,10};
int temp;
for(int i=0;i<[Link];i++)
{
for(int j=i+1;j<[Link];j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(int k=0;k<[Link];k++)
{
[Link](a[k]);
}
}
}
[Link] to count words in string.

public class CountWords


{
public static void main(String[] args)
{
String s="Gauri aa bb";
String a[]=[Link]("\\s+");
for(int i=0;i<[Link];i++)
{
[Link](a[i]);
}
[Link]([Link]);
}
}
[Link] for Swapping of two strings without third var

public class Swap_string


{
public static void main(String[] args)
{
String a="Hello";
String b="World";
[Link]("String Before Swapping:");
[Link]("a: "+a);
[Link]("b: "+b);
a=a+b;
b=[Link](0, [Link]()-[Link]());
a=[Link]([Link]());
[Link]("String After Swapping:");
[Link]("a: "+a);
[Link]("b: "+b);
}
}

You might also like