Java 16
Java 16
fibonacci series : 0 1 1 2 3 5 8
ex:
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();//6
int a=0,b=1,c;
for(int i=2;i<=n;i++)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
prime numbers :
ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();//5
boolean flag=true;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=false;
break;
}
}
if(flag==true)
System.out.println("It is prime number");
else
System.out.println("IT is not prime number");
}
}
input:
6
output:
It is a perfect number
ex:
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();//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)
System.out.println("It is perfect number");
else
System.out.println("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;
prime numbers :
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)
System.out.print(n+" ");
}
}
}
Q)Write a java program to perform sum of two numbers using no returntype with no
argument method?
ex:
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
//caller method
sum();
}
//callie method
public static void sum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt();
System.out.println("Enter the second number :");
int b=sc.nextInt();
int c=a+b;
System.out.println("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 java.util.Scanner;
class Test
{
public static void main(String[] args)
{
//caller method
factorial();
}
//callie method
public static void factorial()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();
int fact=1;
for(int i=n;i>=1;i--)
{
fact*=i;
}
System.out.println("Factorial of a given number is ="+fact);
}
}
Q)Write a java program to perform sum of two numbers with no return type with
argument method?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt();
System.out.println("Enter the second number :");
int b=sc.nextInt();
//caller method
sum(a,b);
}
//callie method
public static void sum(int a,int b)
{
int c=a+b;
System.out.println("sum of two numbers is ="+c);
}
}
Assignment
==========
Q)Write a java program to find out square of a given number?