{
public static void main(String ar[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.println("fib series:");
System.out.print(n1+" "+n2);
for(i=2;i<=count;i++)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
a. Example for call by value.
b. Example for call by reference.
call by value.
public class CallByValue
{
public static void main(String[] args)
{
int a=10;
int b=20;
System.out.println("Values of a and b
before the call...");
System.out.println("a="+a+",b="+b);
swap(a,b);
System.out.println("Values of a and b
after the call...");
System.out.println("a="+a+",b="+b);
}
public static void swap(inta,int b)
{
System.out.println("Inside swap(), before
swapping...");
System.out.println("a="+a+",b="+b);
int temp=a;
a=b;
b=temp;
System.out.println("Inside swap(), after
swapping...");
System.out.println("a="+a+",b="+b);
}
}
call by reference.
public class Call By Reference
{
inta,b;
public static void change(LabTask14b obj)
{
obj.a=20;
obj.b=10;
}
public static void main(String args[])
{
LabTask14b obj=new LabTask14b();
obj.a=10;
obj.b=20;
System.out.println("Before
changing...");
System.out.println("a="+obj.a+",b="+obj.b);
change(obj);
System.out.println("After
changing...");
System.out.println("a="+obj.a+",b="+obj.b);
}
}
4.a)Java program to check weather a number is Palindrome or not and Prime or not by using function Overloading concept.
public class MyClass {
int n;
void find()
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
break;
}
if(n==i)
System.out.println("Number is prime");
else
System.out.println("Number is not prime");
}
void find(int x)
{
int r,s=0;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(s==x)
System.out.println("Number is pallindrome");
else
System.out.println("Number is not pallindrome");
}
public static void main(String args[]) {
MyClass m1=new MyClass();
Scanner s1=new Scanner(System.in);
System.out.println("Enter a number for prime and palindrome test");
m1.n=s1.nextInt();
m1.find();
m1.find(m1.n);
}
}
import java.util.Scanner;
public class MyClass {
int n;
MyClass()
{
Scanner s1=new Scanner(System.in);
n=s1.nextInt();
}
MyClass(int x)
{
n=x;
}
void
Prime()
{
inti;
for(i=2;i<n;i++)
{
if(n%i==0)
break;
}
if(n==i)
System.out.println("Number is prime");
else
System.out.println("Number is not prime");
}
void
Palindrome(int x)
{
intr,s=0;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(s==x)
System.out.println("Number is
pallindrome");
else
System.out.println("Number is not
pallindrome");
}
public
static void main(String args[]) {
MyClass m1=new MyClass();
MyClass m2=new MyClass(356);
m1.Prime();
m2.Palindrome(m1.n);
}}
No comments:
Post a Comment