Professional Documents
Culture Documents
=====================================
There are four ways to declare methods in java.
Q)Write a java program to perfrom sum of two numbers using no returntype with no
argument method?
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();
//logic
int c=a+b;
System.out.println("sum of two numbers is ="+c);
}
}
Q)Write a java program to perform swapping of two numbers using no returntype with
no argument method?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
//caller
swap();
}
//callie method
public static void swap()
{
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();
//logic
a=a+b;
b=a-b;
a=a-b;
Q)Write a java program to perform sum of two numbers using no returntype with
argument method?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//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);
}
}
Q)Write a java program to find out given number is even or odd by using no
returntype 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 number :");
int n=sc.nextInt();
//caller method
find(n);
}
//callie method
public static void find(int n)
{
if(n%2==0)
System.out.println("It is even number");
else
System.out.println("It is odd number");
}
}
3)With returntype with No argument method
==========================================
A returntype is completely depends upon output datatype.
Q)Write a java program to perform sum of two numbers using with returntype with no
argument method?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
//caller method
int k=sum();
System.out.println("sum of two numbers is ="+k);
}
//callie method
public static int 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();
//logic
int c=a+b;
return c;
}
}
Q)Write a java program to find out area of a circle using with returntype with no
argument method?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
float k=circle();
System.out.println("Area of a circle is ="+k);
}
//callie method
public static float circle()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the radius :");
int r=sc.nextInt();
//logic
float area=3.14f*r*r;
return area;
}
}
Q)Write a java program to perform sum of two numbers by using with returntype 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
System.out.println("sum of two numbers is ="+sum(a,b));
}
//callie method
public static int sum(int a,int b)
{
int c=a+b;
return c;
}
}
Q)Write a java program to check given number is even or odd using with returntype
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 number :");
int n=sc.nextInt();
//caller method
System.out.println(find(n));
}
//callie method
public static String find(int n)
{
if(n%2==0)
return "It is even number";
else
return "It is odd number";
}
}