0% found this document useful (0 votes)
9 views15 pages

Function Program

The document outlines various Java programming exercises focused on functions, including summing and differentiating numbers, checking odd/even values, and displaying sequences. It provides code examples for each program, demonstrating different function calls and object instantiations. The document is associated with Mahadevi Education Academy, which offers computer education for various academic levels.

Uploaded by

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

Function Program

The document outlines various Java programming exercises focused on functions, including summing and differentiating numbers, checking odd/even values, and displaying sequences. It provides code examples for each program, demonstrating different function calls and object instantiations. The document is associated with Mahadevi Education Academy, which offers computer education for various academic levels.

Uploaded by

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

MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

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

{ int a,b,s=0;//Actual Argument

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

public static void main(int x1,int y1)

Fun1 obj=new Fun1();//create 1st object

obj.sum(x1,y1);//Function called in main

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

Output

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

Progam2

/*Same function call in two objects*/

import java.io.*;

class Fun2

{ int a,b,s=0;

void sum(int x,int y)//Function with out return

a=x;

b=y;

s=a+b;
Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

System.out.println("Sum = "+s);

}//end of Function

public static void main(int x1,int y1)

Fun2 obj=new Fun2();//create 1st object

Fun2 obj1=new Fun2();//create 2nd object

obj.sum(x1,y1);//Function called in main

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;

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

void sum(int x,int y)//Function with out return

a=x;

b=y;

s=a+b;

System.out.println("Sum = "+s);

}//end of sum Function

void diff(int x,int y)//Function with out return

a=x;

b=y;

d=a-b;

System.out.println("Difference = "+d);

}//end of Diff Function

public static void main(int x1,int y1)

Fun3 obj=new Fun3();//create 1st object

obj.sum(x1,y1);//Function called in main

obj.diff(x1,y1);

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

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;

void sum()//Function with out return and no argument

Scanner obj=new Scanner(System.in);

System.out.print("Enter 1st value:- ");

a=obj.nextInt();

System.out.print("Enter 2nd value:- ");

b=obj.nextInt();

s=a+b;

System.out.println("Sum = "+s);

}//end of Function

public static void main(String args[])

Fun4 obj=new Fun4();//create 1st object

obj.sum();//Function called in main

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

Output

Enter 1st value:- 10

Enter 2nd value:- 20

Sum = 30

Program 5

/*Sum of two numbers with help of function and value scanning in main return type*/

import java.io.*;

public class Fun5

{ int a,b,s=0;//Actual Argument

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

public static void main(int x1,int y1)

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

int k=0;

Fun5 obj=new Fun5();//create 1st object

k=obj.sum(x1,y1);//Function called in main

System.out.print("Sum = "+k);

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

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.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

Scanner obj=new Scanner(System.in);

System.out.print("Enter 1st Value:- ");

x=obj.nextInt();

if(x%2==0)

System.out.print("Number is even");

else

System.out.print("Number is odd");

public static void main(String args[])

Function_if obj1=new Function_if();

obj1.odd_even();

Output

Enter 1st Value:- 10

Number is even

Progam 7

/*Enter ending value and display 1 to ending value with help of function*/

import java.util.*;

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

class Looping

int n,i;

void s()

Scanner obj=new Scanner(System.in);

System.out.print("Enter ending value:- ");

n=obj.nextInt();

for(i=1;i<=n;i++)

System.out.println(i);

public static void main(String args[])

Looping obj=new Looping();

obj.s();

Output

Enter ending value:- 10

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

10

Progam 8

import java.io.*;

class nested_function

void A()

System.out.println("I am in Function A");

void B()

A();

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

System.out.println("I am in Function B");

public static void main(String args[])

nested_function obj=new nested_function();

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()

Scanner obj=new Scanner(System.in);

System.out.print("Enter 1st value:- ");

x=obj.nextInt();

System.out.print("Enter 2nd value:- ");

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

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

Scanner obj1=new Scanner(System.in);

System.out.print("Enter 1st value:- ");

x=obj1.nextInt();

System.out.print("Enter 2nd value:- ");

y=obj1.nextInt();

sum=x-y;

System.out.println("difference = "+sum);

public static void main(String args[])

nested_function2 obj2=new nested_function2();

obj2.diff();

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633
MAHADEVI EDUCATON ACADEMY

(GOVT.REGD.)

Computer VII-XII (all boards)

MCA, BCA, B.TECH and Others (Programming Only and Practical Based)

Output

Enter 1st value:- 10

Enter 2nd value:- 20

Sum = 30

Enter 1st value:- 30

Enter 2nd value:- 40

difference = -10

Centre at-Baguiati, Jyangra, Teghoria, Manicktalla, Khanna

79802 96633

You might also like