Function-based Programs_Programs
Function-based Programs_Programs
Write the main method and call all the three versions of the above function.
What will happen if, in the third version of the function (byte size) is replaced by (int
size)?
ANS:
package FUNCTIONS;
import java.util.*;
float s=a+b;
float avg=s/2.0f;
return(avg);
return(a);
for(int i=0;i<size;i++)
a[i]=ob.nextInt();
s=s+a[i];
avg=s/size;
return(avg);
// TO DISPLAY:
void main()
I-TECH 2
float a= calcAvg(5,10);
System.out.println(a);
System.out.println(calcAvg(5));
System.out.println(calcAvg((byte)6));
void num_calc (int num, char ch) which computes the square of integer argument if
choice ch is ‘s’ otherwise finds its cube.
void num_calc (int a, int b, char ch) which computes the product of the integer arguments,
if choice ch is ‘p’ else adds the integers.
void num_calc (String s1, String s2) which prints whether the strings are equal or
not.
ANS:
package FUNCTIONS;
import java.util.*;
ch=Character.toLowerCase(ch);
I-TECH 3
switch(ch)
case 's':
System.out.println(Math.pow(num,2));
break;
default :
System.out.println(Math.pow(num,3));
switch(ch)
case 'p':
System.out.println(a*b);
break;
default :
System.out.println(a+b);
I-TECH 4
{
System.out.println(s1.equalsIgnoreCase(s2));
//MAIN TO CALL
void main()
num_calc(5,'s');
num_calc("FATEHPUR","SIKHRI");
num_calc(5,4,'c');
maximum ( ) with one integer argument should display the next integer.
maximum ( ) with two integer arguments and should display the larger value among
the two.
maximum ( ) with three integer arguments and should display the smallest value among
the three.
ANS:
package FUNCTIONS;
import java.util.*;
I-TECH 5
//TO DISPLAY THE NEXT INTEGER AFTER
THE ARGUMENT
void maximum(int a)
//TO CALL
void main()
maximum(5);
maximum(69,96,49);
I-TECH 6
maximum(18,21);
4. Prime palindrome is a number which is prime as well as palindrome. E.g. 101. Write
a program to display prime palindrome numbers within the given range. The limits of
the range should be entered by the user. Define two user defined functions to check
for the number. The function prototypes are given below.
ANS:
package FUNCTIONS;
import java.util.*;
boolean isPrime(int n)
int i,k=0;
for(i=1;i<=n;i++)
I-TECH 7
if(n%i==0)
k++;
if(k==2)
return(true);
return(false);
boolean isPalin(int n)
int t=n,r,p=0;
while(t>0)
r=t%10;
p=p*10+r;
t=t/10;
ifn==p)
I-TECH 8
return(true);
return(false);
void main()
start=ob.nextInt();
stop=ob.nextInt();
for(i=start;i<=stop;i++)
boolean r1=isPalin(i);
boolean r2=isPrime(i);
if(r1==true&&r2==true)
System.out.println(i+" , ");
I-TECH 9
1
5. Design a class Overload to overload a function calc( ) as
follows:
void calc (int num1, int num2, int num3) which computes the sum of 3 integer
arguments.
void calc (int a, int b) which calculates and prints the HCF of two integers a and
b.
void calc (char c1, char c2) which prints sum of ASCII code of both the
characters.
ANS:
package FUNCTIONS;
import java.util.*;
System.out.println(a+b+c);
int i,hcf=0;
for(i=1;i<=Math.min(a,b);i++)
I-TECH 10
{
if(a%i==0&&b%i==0)
hcf=i;
// MAIN TO CALL:
void main()
calc(8,5,9);
calc(4,5);
calc('F','S');
I-TECH 11
I-TECH 12