Q)Write a java program to find out fibonacci series of a given number?
fibonacci series : 0 1 1 2 3 5 8
ex:
import [Link];
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number :");
int n=[Link]();//6
int a=0,b=1,c;
[Link](a+" "+b+" ");
for(int i=2;i<=n;i++)
{
c=a+b;
[Link](c+" ");
a=b;
b=c;
}
}
}
Q)Write a java program to find out given number is prime or not?
prime numbers :
2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97.
ex:
---
import [Link];
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number :");
int n=[Link]();//5
boolean flag=true;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=false;
break;
}
}
if(flag==true)
[Link]("It is prime number");
else
[Link]("IT is not prime number");
}
}
Q)Write a java program to find out given number is perfect or not?
input:
6
output:
It is a perfect number
ex:
import [Link];
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number :");
int n=[Link]();//6
int sum=0;
for(int i=1;i<n;i++)
{
if(n%i==0) // 6/1 6/2 6/3 6/4 6/5
{
sum=sum+i;
}
}
if(n==sum)
[Link]("It is perfect number");
else
[Link]("It is not perfect number");
}
}
Q)Write a java program to find out GCD(Greatest Common Divisor) of two numbers?
input:
12 18
output:
6
ex:
---
class Test
{
public static void main(String[] args)
{
int a=12,b=18,gcd=0;
for(int i=1;i<=12 && i<=18;i++)
{
if(a%i==0 && b%i==0)
{
gcd=i;
}
}
[Link]("GCD of two numbers is ="+gcd);
}
}
Q)Write a java program to display prime numbers from 1 to 100?
prime numbers :
2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97.
class Test
{
public static void main(String[] args)
{
for(int n=2;n<=100;n++)
{
boolean flag=true;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=false;
break;
}
}
if(flag==true)
[Link](n+" ");
}
}
}
Various ways to write java methods
==================================
There are four ways to write methods in java.
1) No returntype with No argument method
2) No returntype with Argument method
3) With returntype with No argument method
4) With return with Argument method
1) No returntype with No argument method
-----------------------------------------
If we don't have arguments then we need to ask inputs inside callie method.
Q)Write a java program to perform sum of two numbers using no returntype with no
argument method?
ex:
import [Link];
class Test
{
public static void main(String[] args)
{
//caller method
sum();
}
//callie method
public static void sum()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the first number :");
int a=[Link]();
[Link]("Enter the second number :");
int b=[Link]();
int c=a+b;
[Link]("sum of two numbers is ="+c);
}
}
Q)Write a java program to find out factorial of a given number using with no
returntype with no argument method?
import [Link];
class Test
{
public static void main(String[] args)
{
//caller method
factorial();
}
//callie method
public static void factorial()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number :");
int n=[Link]();
int fact=1;
for(int i=n;i>=1;i--)
{
fact*=i;
}
[Link]("Factorial of a given number is ="+fact);
}
}
2)No return type with argument method
-----------------------------------
If we have arguments then we need to ask input values inside main method.
Number of arguments depends upon number of inputs.
Q)Write a java program to perform sum of two numbers with no return type with
argument method?
import [Link];
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the first number :");
int a=[Link]();
[Link]("Enter the second number :");
int b=[Link]();
//caller method
sum(a,b);
}
//callie method
public static void sum(int a,int b)
{
int c=a+b;
[Link]("sum of two numbers is ="+c);
}
}
Assignment
==========
Q)Write a java program to find out square of a given number?