Thursday, October 27, 2022

Java lab programs for ECE

//    Lab Task -1 NON RECURSIVE   program
class Fibonacci
{
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;
}
}
}

2 a) write a java program that prompts the user for an integer and then prints out all the prime numbers up to that integer.

import java.util.Scanner;
public class primeExample {
 
    public static void main(String[] args) {
        
        int min = 2;
        int max ;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the maximum number upto which prime numbers are needed");
        max=sc.nextInt();
        for(int n=min;n<=max;n++) {
            if(isPrime(n)) {
                System.out.println(n);
            }
        }
    }
     
    public static boolean isPrime(int num) {    
        for(int i = 2; i <= num/i; ++i) {
            if(num % i == 0) {
                return false;
            }
        }
        return true;
    }
}

2. b)Write a JAVA program for the following

          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.

import java.util.Scanner;
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);
      }
}

4.b)Java program to check weather a number is Palindrome or not and Prime or not by using Constructor Overloading concept.

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

Machine Learning Course Assignments 2024(Dec)

 Machine Learning Course Assignments 2024 1)   Explain the concept of "generalization" in machine learning. Why is it a central go...