You are on page 1of 4

QB-301 WAP to find nPr by using class and object

import java.util.*;
class Main_301
{
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
System.out.print("Enter r: ");
int r = sc.nextInt();
Recursion11 r11 = new Recursion11();
int Fact_N = r11.factorial(n);
System.out.println("Factorial of n = "+Fact_N);
int Fact_N_R = r11.factorial(n-r);
System.out.println("Factorial of n-r = "+Fact_N_R);

int nPr = (Fact_N)/(Fact_N_R);


System.out.println("Result of nCr = "+nPr);
}
}
class Recursion11
{
int factorial(int n)
{
int fact = 1;
for(int i=1 ; i<=n ; i++)
{
fact *= i;
}
return fact;
}
}

QB-309 Create a class name Temperature in which create methods of given name
ferenhit(), celcius() to perform basic conversion. Call all this methods using class named
Main.
class Temperature{

void ferenhit(double celsius){


double result = celsius * (9/5.0) + 32;
System.out.println("Temperature in fahrenheit is: "+result);
}

void celsius(double fahrenheit){


double result = (fahrenheit - 32) * (5/9.0);
System.out.println("Temperature in Celsius is: "+result);
}
}

class Main{
public static void main(String[] args) {
Temperature T1 = new Temperature();
T1.ferenhit(100);
T1.celsius(212);
}
}
QB-377 Write a Java program to Swap two values, which is given from user by using call
by value

import java.util.Scanner;
class SwapNumber
{
public static void main(String args[])
{
int x, y;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
x = sc.nextInt();
System.out.print("Enter the second number: ");
y = sc.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
swap(x, y);
}
public static void swap(int a, int b)
{
a = a + b;
b = a - b;
a = a - b;
System.out.println("After Swapping\nx = "+a+"\ny = "+b);
}
}

QB-378 Write a Java program to Swap two values, which is given from user by using call
by reference.

class CallByReference {

int a, b;

void set(int x, int y) {


a = x;
b = y;
}

void swapvalue(CallByReference obj) {


int temp;
temp = obj.a;
obj.a = obj.b;
obj.b = temp;
}
}

public class Main {

public static void main(String[] args) {

CallByReference object
= new CallByReference();
object.set(10, 20);
System.out.println("Value of a: " + object.a
+ " & b: " + object.b);

object.swapvalue(object);

System.out.println("Value of a: " + object.a


+ " & b: " + object.b);
}
}

QB- 367 Write a Java program to find the power of number using passing an object to the
method(i.e num=5 and power=3 then ans is5^3 that is 125)
import java.util.*;
class Ex_8_367{

int num,pow,result=1;

Ex_8_367 findPow(Ex_8_367 obj){


int i;
Ex_8_367 obj1=new Ex_8_367();
for(i=1;i<=obj.pow;i++){
obj1.result=obj1.result*obj.num;
}

return obj1;

}
}
class run{
public static void main(String args[])
{
Ex_8_367 obj2=new Ex_8_367();
Ex_8_367 obj3=new Ex_8_367();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number ");
obj2.num=sc.nextInt();
System.out.println("Enter the value of Power");
obj2.pow=sc.nextInt();

obj3=obj2.findPow(obj2);
System.out.println("power of given number is
"+obj3.result);
}
}

QB- 368 Write a Java program to find GCD of two numbers using passing an object to the
method(i.e a=4 and b=6 then GCD is 2)
import java.util.*;
class EX_8_368
{
int a,b;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER a and b=");
a=sc.nextInt();
b=sc.nextInt();
}
void gcd(EX_8_368 ob1)
{
while(ob1.a!=ob1.b)
{
if(ob1.a>=ob1.b)
ob1.a=ob1.a-ob1.b;
else
ob1.b=ob1.b-ob1.a;
}
System.out.println("GCD="+ob1.a);
}
}
class Run
{
public static void main(String args[])
{
EX_8_368 ob1 = new EX_8_368();
EX_8_368 ob2 = new EX_8_368();
ob1.input();
ob2.gcd(ob1);
}
}
6

You might also like