Wednesday, 24 July 2013

To find if a given number is a perfect cube or not.



import java.io.*;
class cube{
    public static void main(String args[])throws IOException
    {
        BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("ENTER A NUMBER");
        int n=Integer.parseInt(input.readLine());
        int found=0;
        for(int i=2;i<=(n/2);i++)
        {
            if(n%i==0)
            {
                if((i*i*i)==n)
                found=1;
            }
        }
        if(found==1)
            System.out.println("Perfect Cube");
        else
            System.out.println("Not A perfect cube");
       
    }
}

Sunday, 7 July 2013

To Check Armstrong Number in Java

/*
Java Prog to check whether given number is Armstrong or not

Hint-> when sum of cubes of individual digit is equal to the numbr itself
       Example 153 = 1^3+5^3+3^3;

*/


import java.io.*;


class Armstrong
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

int rem,sum,cpy;

System.out.println("ENTER THE NUMBER TO BE CHECKED");
int num = Integer.parseInt(input.readLine());

cpy=num;
sum=0;

while(num!=0)
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(sum==cpy)
System.out.println("THE NUMER "+cpy+" IS AN AMSTRONG NUMBER");
else
System.out.println("THE NUMER "+cpy+" IS NOT AN AMSTRONG NUMBER");
}
}

Tuesday, 2 July 2013

program to Print Diamond pattern in java

/*
To Print Diamond pattern
for n=4
    *
  * *
 * * *
* * * *
 * * *
  * *
   *
*/




import java.io.*;


class Pattern
{
public static void main(String args[]) throws IOException
{

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.println("ENTER THE NO OF ROWS TO PRINT PATTERN");
int m = Integer.parseInt(input.readLine());
int n=m+(m-1);

for(int i=1;i<=n;i++)
{
for(int j=1;j<=(n-i);j++)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("* ");
}
System.out.println();
}


for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" ");
}
for(int k=1;k<=(n-i);k++)
{
System.out.print("* ");
}
System.out.println();
}

}
}

C++ program for pattern(Pyramid)

/*
To Print Patterns (Pyramid)

*
**
***
****
......
n......n


*/



#include<iostream.h>
#include<conio.h>
Void main()
{

clrscr();

cout<<"Enter the no of rows for which pattern required";
cin>>n;

for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*";
}
}

getch();
}

Java Prog to print pyramid

/*
To Print Patterns (Pyramid)

   *
  * *
 * * *
* * * *
......
n......n


*/


import java.io.*;


class Pattern
{
public static void main(String args[]) throws IOException
{

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.println("ENTER THE NO OF ROWS TO PRINT PATTERN");
int n = Integer.parseInt(input.readLine());


for(int i=1;i<=n;i++)
{
for(int j=1;j<=(n-i);j++)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("* ");
}
System.out.println();
}

}
}

c++ prog to print pyramid

/*
To Print Patterns (Pyramid)

   *
  * *
 * * *
* * * *
......
n......n


*/


#include<iostream.h>
#include<conio.h>
Void main()
{

clrscr();

cout<<"Enter the no of rows upto which pattern is to be printed";
cin>>n;

for(int i=1;i<=n;i++)
{
for(int j=1;j<=(n-i);j++)
{
cout<<" ";
}
for(int k=1;k<=i;k++)
{
cout<<"* ";
}
cout<<"/n";
}


getch();
}

Pattern in Java

/*
To Print Patterns (Pyramid)

*
**
***
****
......
n......n


*/


import java.io.*;


class Pattern
{
public static void main(String args[]) throws IOException
{

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.println("ENTER THE NO OF ROWS TO PRINT PATTERN");
int n = Integer.parseInt(input.readLine());


for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}

}
}

Fibonacci Series in C++

/*
To Print n Fibonacci Numbers (Fibonacci Series)

1 1 2 3 5 8 13 21 ......n .

Fibonacci Series is also believed to start from 0 in many colleges depending upon the acceptance .
To modify this prog to print 0 1 1 2 3 5 8 21 ......n.
Change the value of x=0 .
*/



#include<iostream.h>
#include<conio.h>
Void main()
{
int x,y,z;
clrscr();
cout<<"Enter the no upto which series required";
cin>>n;
x=1;
y=1;
cout<<"THE FOLLOWING ARE THE FIRST "<<n<<" FIBONACCI NUMBERS";
cout<<x<<" "<<y<<" ";
for(int i=1;i<=(n-2);i++)
{
z=x+y;
cout<<z<<"\t";
x=y;
y=z;
}
getch();
}

Fibonacci Series in Java

/*
To Print n Fibonacci Numbers (Fibonacci Series)

1 1 2 3 5 8 13 21 ......n .

Fibonacci Series is also believed to start from 0 in many colleges depending upon the acceptance .
To modify this prog to print 0 1 1 2 3 5 8 21 ......n.
Change the value of x=0 .
*/


import java.io.*;

class Fibo
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

int x,y,z;

System.out.println("ENTER THE NO UPTO WHICH FIBONACCI SERIES REQUIRED");
int n = Integer.parseInt(input.readLine());

System.out.println("The First "+ n +" Fibonacci Numbers are as follows");

x=1;
y=1;

System.out.print(x+" "+y+" ");


for(int i=1;i<=(n-2);i++)
{
z=x+y;
System.out.print(z+" ");
x=y;
y=z;
}

}
}