FUNCTION
Def: A program module (a part or
segment of the program) used at
different instances in a program to
perform a specific task is known as
Method or Function.
Different type of Function:
1. Pure Function
it return some values and used
keyword “ return”.
2. Impure Function
it does not return any values and
used keyword “ void ”.
How to create object?
Class name: Rana
Object name : ob
Note: Object can be created using
Class name.
Rana ob=new Rana();
Definition of new keyword?
Ans: new is a keyword which is used
to create Dynamic memory
location.
Access Specifier:
1. public
2. private
3. protected
1. Input two different numbers
and add them using impure
function.
Ans:
import [Link].*;
public class Tech
{
void Addition(int a,int b)
{
int c;
c=a+b;
[Link](c);
}
public static void main(String
args[])
{
Scanner in=new
Scanner([Link]);
int a,b;
[Link]("Enter 1st
Number=");
a=[Link]();
[Link]("Enter 2nd
Number=");
b=[Link]();
Tech ob=new Tech();
[Link](a,b);
}
}
2. Input a number and check
number is even or odd using
impure function.
Ans:
3. Input a number and check
number is Palindrome
number or not using impure
function.
Ans:
4. Input a number and check
number is Buzz Number or
not using Impure function.
Ans:
5. Input a number and check
number is prime number or
not using Impure function.
Ans:
6. Input a number and find the
sum of even digits and odd
digits sum separately using
Impure function.
7. Input a two digit number and
check number is Special Two
digit number or not using
Impure function.
Example:
Input: 59
Digit sum=5+9
Digit product=5*9
If sum+product == Input
number then Special two
digit number.
8. Input a sentence and display
all the word separately using
Impure function.
9. Input a sentence and display
all the palindrome word
separately using impure
function.
Ans:
Pure Function:
1. Input two different
numbers and add them
using Pure function.
Ans:
import [Link].*;
public class Tech
{
int Addition(int a,int b)
{
int c;
c=a+b;
// return(c);
}
public static void
main(String args[])
{
Scanner in =new
Scanner([Link]);
int a,b,p;
[Link]("Enter
1st Number=");
a=[Link]();
[Link]("Enter
2nd Number=");
b=[Link]();
Tech ob=new Tech();
p=[Link](a,b);
[Link](p);
}
}
[Link] a number and check
number is Positive or
negative number using pure
function.
Ans:
2. Input a number and find
sum of each digit using
Pure function.
3. Input a word and find the
sum of each characters
ASCII value using Pure
function.
4. Input a number and make
a word by converting
each digit of the number
into corresponding
character.
Ans:
import [Link].*;
public class Tech
{
String make(int n)
{
String s="";
int a;
while(n!=0)
{
a=n%10;
n=n/10;
char c=(char)(a+65);
s=c+s;
}
return(s);
}
public static void
main(String args[])
{
Scanner in=new
Scanner([Link]);
int n;
String p="";
[Link]("Enter
Number=");
n=[Link]();
Tech ob=new Tech();
p=[Link](n);
[Link](p);
}
}
5. Input 10 different
numbers to make an
array. Now find the sum of
the array elements using
pure function.
Ans:
import [Link].*;
public class Tech
{
int Calculation(int a[])
{
int i,sum=0;
for(i=0;i<=9;i++)
{
sum=sum+a[i];
}
return(sum);
}
public static void
main(String args[])
{
Scanner in=new
Scanner([Link]);
int a[]=new int[10];
int i,p;
for(i=0;i<=9;i++)
{
[Link]("Enter
Number=");
a[i]=[Link]();
}
Tech ob=new Tech();
p=[Link](a);
[Link](p);
}
}
6. Input 10 different
numbers to make a list .
Now also input another
numbers to search that
number is present or not if
present then replace it by
10 otherwise display all
the element of the list
using impure function.
Ans:
import [Link].*;
public class Tech
{
void Search(int a[],int b)
{
int i;
for(i=0;i<=9;i++)
{
if(a[i]==b)
{
a[i]=10;
}
}
for(i=0;i<=9;i++)
{
[Link](a[i]+" ");
}
}
public static void
main(String args[])
{
Scanner in=new
Scanner([Link]);
int a[]=new int[10];
int i,b;
for(i=0;i<=9;i++)
{
[Link]("Enter
Number=");
a[i]=[Link]();
}
[Link]("Enter
number to be search=");
b=[Link]();
Tech ob=new Tech();
[Link](a,b);
}
}
7. Input 10 different
numbers into two
different arrays to make
two different array now
make a new array by
adding odd numbers from
two different arrays and
finally display the new
array.
Example:
A[ ]={1,2,3,5]
B[ ]={7,18,9,11,14}
New array create by odd
numbers:
C[ ]={1,3,5,7,9,11}
Ans:
import [Link].*;
public class Tech
{
void make(int a[],int b[])
{
int c[]=new int[20];
int i,k=0;
for(i=0;i<=9;i++)
{
if(a[i]%2!=0)
{
c[k]=a[i];
k++;
}
}
for(i=0;i<=9;i++)
{
if(b[i]%2!=0)
{
c[k]=b[i];
k++;
}
}
for(i=0;i<=k-1;i++)
{
[Link]("
"+c[i]);
}
}
public static void
main(String args[])
{
Scanner in=new
Scanner([Link]);
int a[]=new int[10];
int b[]=new int[10];
int i;
for(i=0;i<=9;i++)
{
[Link]("Enter
Number=");
a[i]=[Link]();
}
for(i=0;i<=9;i++)
{
[Link]("Enter
Number=");
b[i]=[Link]();
}
Tech ob=new Tech();
[Link](a,b);
}
}
Q4.(page-361)
Ans:
import [Link].*;
public class Tech
{
int fact(int n)
{
int i,pro=1;
for(i=1;i<=n;i++)
{
pro=pro*i;
}
return(pro);
}
public static void
main(String args[])
{
Scanner in=new
Scanner([Link]);
int n,m,p,q,r,s;
[Link]("Enter
the value of n=");
n=[Link]();
[Link]("Enter
the value of m=");
m=[Link]();
Tech ob=new Tech();
p=[Link](n);
q=[Link](m);
r=[Link](n-m);
s=p/(q*r);
[Link](s);
}
}
Q4.
import [Link].*;
public class Tech
{
void area()
{
Scanner in=new
Scanner([Link]);
int ch;
double r,s,l,b,area;
[Link]("Press 1
for area of Circle");
[Link]("Press 2
for area of Square");
[Link]("Press 3
for area of Rectangle");
[Link]("Enter your
choice=");
ch=[Link]();
switch(ch)
{
case 1:
[Link]("Enter
radius=");
r=[Link]();
area=(22*r*r)/7;
[Link]("Area of
Circle="+area);
break;
case 2:
[Link]("Enter
Side of square=");
s=[Link]();
area=s*s;
[Link]("Area of
Square="+area);
break;
case 3:
[Link]("Enter
Length=");
l=[Link]();
[Link]("Enter
Breadth=");
b=[Link]();
area=l*b;
[Link]("Area of
Rectangle="+area);
break;
default:
[Link]("Wrong
Choice");
}
}
public static void main(String
args[])
{
Tech ob=new Tech();
[Link]();
}
}
Q6.
import [Link].*;
public class Tech
{
void Glcm(int a,int b)
{
int m,n,x=1,hcf=0,lcm=0,c;
m=a;
n=b;
while(x!=0)
{
c=a%b;
if(c==0)
{
hcf=b;
break;
}
else
{
a=b;
b=c;
}
}
lcm=(m*n)/hcf;
[Link]("HCF="+hcf);
[Link]("LCM="+lcm);
}
public static void main(String
args[])
{
Scanner in=new
Scanner([Link]);
int a,b;
[Link]("Enter 1st
Number=");
a=[Link]();
[Link]("Enter 2nd
Number=");
b=[Link]();
Tech ob=new Tech();
[Link](a,b);
}
}
Q.7:
import [Link].*;
public class Tech
{
void Magic(String str)
{
int i,len,k=0,a,b;
str=[Link]();
len=[Link]();
for(i=0;i<=len-2;i++)
{
char c=[Link](i);
char c1=[Link](i+1);
a=(int)c;
b=(int)c1;
if(b-a==1)
{
k=1;
break;
}
}
if(k==1)
{
[Link]("Magic
String");
}
else
{
[Link]("Not
Magic String");
}
}
public static void main(String
args[])
{
Scanner in=new
Scanner([Link]);
String str;
[Link]("Enter
Word=");
str=[Link]();
Tech ob=new Tech();
[Link](str);
}
}
Function Overloading:
Function overloading mean
where more than one function
with the same name but
parameter of the function must
be different and return type may
be same or different.
Example:
int Addition(int a,int b)
{
…
…
}
void Addition(int x,int y,int z)
{
……….
……….
}
Write a function overloading
program for addition.
Ans:
import [Link].*;
public class Tech
{
void Addition(int a,int b)
{
int c;
c=a+b;
[Link](c);
}
void Addition(int a,int b,int c)
{
int d;
d=a+b+c;
[Link](d);
}
public static void main(String
args[])
{
Scanner in=new
Scanner([Link]);
int a,b,c;
[Link]("Enter 1st
Number=");
a=[Link]();
[Link]("Enter 2nd
Number=");
b=[Link]();
[Link]("Enter 3rd
Number=");
c=[Link]();
Tech ob=new Tech();
[Link](a,b);
[Link](a,b,c);
}
}