Function Program
Function Program
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
Function
Part 1
Program 1
/*Sum of two numbers with help of function and value scanning in main*/
import java.io.*;
class Fun1
void sum(int x,int y)//Function with out return and Formal argument
a=x;
b=y;
s=a+b;
System.out.println("Sum = "+s);
}//end of Function
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
Output
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
Progam2
import java.io.*;
class Fun2
{ int a,b,s=0;
a=x;
b=y;
s=a+b;
Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
System.out.println("Sum = "+s);
}//end of Function
obj1.sum(40,50);
Progaram 3
/*Sum and difference of two numbers with help of two function and value scanning in main*/
import java.io.*;
class Fun3
{ int a,b,s=0,d;
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
a=x;
b=y;
s=a+b;
System.out.println("Sum = "+s);
a=x;
b=y;
d=a-b;
System.out.println("Difference = "+d);
obj.diff(x1,y1);
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
Progam 4
/*Sum of two numbers with help of function and value scanning in Function*/
import java.util.*;
class Fun4
{ int a,b,s=0;
a=obj.nextInt();
b=obj.nextInt();
s=a+b;
System.out.println("Sum = "+s);
}//end of Function
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
Output
Sum = 30
Program 5
/*Sum of two numbers with help of function and value scanning in main return type*/
import java.io.*;
int sum(int x,int y)//Function with out return and Formal argument
a=x;
b=y;
s=a+b;
return(s);
}//end of Function
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
int k=0;
System.out.print("Sum = "+k);
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
Program 6
/*Enter any value and checked if it is odd or even with help of function*/
import java.util.*;
class Function_if
int x;
void odd_even()
{
Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
x=obj.nextInt();
if(x%2==0)
System.out.print("Number is even");
else
System.out.print("Number is odd");
obj1.odd_even();
Output
Number is even
Progam 7
/*Enter ending value and display 1 to ending value with help of function*/
import java.util.*;
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
class Looping
int n,i;
void s()
n=obj.nextInt();
for(i=1;i<=n;i++)
System.out.println(i);
obj.s();
Output
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
10
Progam 8
import java.io.*;
class nested_function
void A()
void B()
A();
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
obj.B();
Output
I am in Function A
I am in Function B
import java.util.*;
class nested_function2
int x,y,sum=0,diff=0;
void add()
x=obj.nextInt();
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
y=obj.nextInt();
sum=x+y;
System.out.println("Sum = "+sum);
void diff()
add();
x=obj1.nextInt();
y=obj1.nextInt();
sum=x-y;
System.out.println("difference = "+sum);
obj2.diff();
79802 96633
MAHADEVI EDUCATON ACADEMY
(GOVT.REGD.)
MCA, BCA, B.TECH and Others (Programming Only and Practical Based)
Output
Sum = 30
difference = -10
79802 96633