0% found this document useful (0 votes)
4 views7 pages

Java 16

The document contains several Java programs that demonstrate basic programming concepts such as calculating Fibonacci series, checking for prime numbers, determining perfect numbers, finding the GCD of two numbers, and displaying prime numbers within a range. It also explains different ways to write methods in Java, including methods with and without return types and arguments. Additionally, it provides examples of methods for summing two numbers and calculating the factorial of a given number.

Uploaded by

s73741575
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java 16

The document contains several Java programs that demonstrate basic programming concepts such as calculating Fibonacci series, checking for prime numbers, determining perfect numbers, finding the GCD of two numbers, and displaying prime numbers within a range. It also explains different ways to write methods in Java, including methods with and without return types and arguments. Additionally, it provides examples of methods for summing two numbers and calculating the factorial of a given number.

Uploaded by

s73741575
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

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 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;

System.out.print(a+" "+b+" ");

for(int i=2;i<=n;i++)
{
c=a+b;
System.out.print(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 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");
}
}

Q)Write a java program to find out given number is perfect or not?

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;

for(int i=1;i<=12 && i<=18;i++)


{
if(a%i==0 && b%i==0)
{
gcd=i;
}
}
System.out.println("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)
System.out.print(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 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);
}
}

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 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?

You might also like