Some Java program part 6

  1. Write a Java program that would input three numbers from the user and print sum, then the first number, then the 2nd number followed by 3rd number. If user enters 10, 20, 30. then output should be 60, 10, 20, 30.
import java.util.Scanner;
public class Lab8q4{
  public static void main ( String [] args ){
    int [] number = new int [3];
    int c = 0, sum=0;
    Scanner input = new Scanner ( System.in );
    System.out.println("Enter 3 numbers");
    while(c<3){
      number  = input.nextInt();
      sum = sum + number ;
      ++c;
    }
    System.out.println("SUM : "+sum);
    c=0;
    while(c<3){
      System.out.println(number);
      ++c;
    }
  }
}
  1. Write a Java program that would input ten numbers from the user and print the ten numbers in reverse order. If user enters 20, 10, 30, 15. Then output should be 15, 30, 10, 20.
import java.util.Scanner;
public class Lab8q5{
  public static void main ( String [] args ){
    int [] number = new int [3];
    int c = 0, sum=0;
    Scanner input = new Scanner ( System.in );
    System.out.println("Enter 3 numbers");
    while(c<3){
      number  = input.nextInt();
      sum = sum + number ;
      ++c;
    }
    c=2;
    while(c>=0){
      System.out.println(number);
      --c;
    }
  }
}

  1. Without using String, write a Java program that will ask the user to enter 10 numbers and will verify the users input after each entry. For example if the user enters 2, the program will print “You have entered 2”, then if the user enters 4 the program will print “You have entered 2,  4”, then if the user enters 23 the program will print “You have entered 2,  4 , 23”. This will go on till the user enters the 10 numbers.
/* Time: 12.11 am
 * Author: Anik Das*/
import java.util.Scanner;
public class Lab8Q6{
  public static void main ( String [] args ){
    Scanner input = new Scanner ( System.in );
      int [] num = new int [10];
      System.out.println("Enter 10 Numbers");
      for(int c=0; c<10; ++c){
        num  = input.nextInt();
        System.out.print("You have entered ");
        for(int i=0; i<=c;++i){
          System.out.print(num[i]+",");
        }
        System.out.println(" ");
      }
  }
}

    1. Write a Java program that would input ten numbers from the user and print the ten numbers sorted in descending order.
/*Author: Anik Das
 * Time: 7:23*/
import java.util.Scanner;
public class Lab8Q7{
  public static void main (String [] args){
    int[] num = new int [10];
    int temp=0;
    Scanner input = new Scanner (System.in);
    for(int c=0; c<10; ++c){
      System.out.println("Enter Number #"+(c+1));
      num = input.nextInt();
    }
    for(int i=0;i<10;++i){
      for(int j=i+1;j<10;++j){
        if(num[j]>num[i]){
          temp=num[i];
          num[i]=num[j];
          num[j]=temp;
        }
      }
    }
    for(int k=0;k<10;++k){
      System.out.println(num[k]);
    }
  }
}
  1. Write a Java program that would ask the user to enter ten numbers. As the user enters the ten numbers the program will make sure that all the numbers entered by the user are unique. For example if the user enters 2, 3, 4, 6, and then tries to enter 3 again the program will display that 3 is already in among the entered numbers and would ask the user to enter a new number.
import java.util.Scanner;
public class ULab8Q8{
  public static void main (String [] args){
    int [] a = new int [10];
    int c=0, n=0;
    Scanner input = new Scanner (System.in);
    System.out.println("Enter 10 numbers");
    while(c      a  = input.nextInt();
      n=0;
      while(n        while(a==a[n]){
          System.out.println("You have repeated "+a+" .Enter A new number");
          a = input.nextInt();
          n=0;
        }
        ++n;
      }
      ++c;
    }
    c=0;
    while(c      System.out.println(a);
      ++c;3
    }
  }
}
  1. User will enter 10 numbers. All numbers are between 2 and 6. Write a Java program to count these numbers. Output should look like:

2 was found 4 times

3 was found 1 times

4 was found 0 times

5 was found 2 times

6 was found 3 times

/*Author: Anik Das
 * Time : 7.41pm*/
import java.util.Scanner;
public class Lab8Q9{
  public static void main (String [] args){
    Scanner input = new Scanner(System.in);
    int c2=0, c3=0, c4=0, c5=0, c6=0;
    int [] num = new int [10];
    for(int c=0; c<10; ++c){
      System.out.println("Enter Number #"+(c+1));
      num  = input.nextInt();
      if(num  == 2){
        ++c2;
      }
      if(num  == 3){
        ++c3;
      }

      if(num  == 4){
        ++c4;
      }
      if(num  == 5){
        ++c5;
      }
      if(num  == 6){
        ++c6;
      }
    }
    System.out.printf("2 was found %d timesn",c2);
    System.out.printf("3 was found %d timesn",c3);
    System.out.printf("4 was found %d timesn",c4);
    System.out.printf("5 was found %d timesn",c5);
    System.out.printf("6 was found %d timesn",c6);
  }
}
  1. Write a program that takes Matrix A and B as input and gives Matrix C as output.

Matrix A

7 7 0 1
2 2 7 7
0 8 6 9

Matrix B

3 4 7 0
3 7 6 1
6 5 1 9

Matrix C ( it is summation of Matrix A and Matrix B, e.g. C = A + B)

10 11 7 1
5 9 13 8
6 13 7 18
import java.util.Scanner;
public class Lab8Q10{
  public static void main ( String [] args ){
    int [][] a = new int [3] [4];
    int [][] b = new int [3] [4];
    int [][] c = new int [3] [4];
    Scanner input = new Scanner(System.in);
    for(int f=0; f<3;++f){
      for(int g=0; g<4; ++g){
        System.out.println("Enter Value for row "+(f+1)+" column "+(g+1)+" For matrix a");
        a[f][g] = input.nextInt();
      }
    }
    for(int h=0; h<3;++h){
      for(int i=0; i<4; ++i){
        System.out.println("Enter Value for row "+(h+1)+" column "+(i+1)+" For matrix b");
        b[h][i] = input.nextInt();
      }
    }
    for(int i = 0; i<3; i++){
      for(int j = 0; j<4; j++){
        c[i][j]=a[i][j]+b[i][j];
      }
    }
    System.out.print(c[0][0]+","+c[0][1]+","+c[0][2]+","+c[0][3]+"n");
    System.out.print(c[1][0]+","+c[1][1]+","+c[1][2]+","+c[1][3]+"n");
    System.out.print(c[2][0]+","+c[2][1]+","+c[2][2]+","+c[2][3]+"n");
  }
}

  1. Write java code for matrix multiplication
import java.util.Scanner;
public class Lab8Q11{
  public static void main ( String [] args ){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter Number of rows for Matrix A");
    int m = input.nextInt();
    System.out.println("Enter Number of columns for Matrix A");
    int n = input.nextInt();
    System.out.println("Enter Number of rows for Matrix B");
    int o = input.nextInt();
    System.out.println("Enter Number of columns for Matrix B");
    int p = input.nextInt();
    int [][] a = new int [m] [n];
    int [][] b = new int [o] [p];
    int [][] c = new int [m] [p];
    for(int f=0; f<m;++f){
      for(int g=0; g<n; ++g)
        System.out.println("Enter Value for row "+(f+1)+" column "+(g+1)+" For matrix a");
        a[f][g] = input.nextInt();
      }
    }
    for(int h=0; h<o;++h){
      for(int i=0; i<p; ++i){
        System.out.println("Enter Value for row "+(h+1)+" column "+(i+1)+" For matrix b");
        b[h][i] = input.nextInt();
      }
    }
    for(int j =0; j<m ;++j){
      for(int k=0; k<p ;++k){
        for(int l=0; l<o ;++l){
          c[j][k]+=a[j][l]*b[l][k];
        }
      }
    }
    for(int x=0; x<m; ++x){
      for(int y=0; y<p; ++y){
        System.out.print(c[x][y]+",");
      }
      System.out.println(" ");
    }
  }
}<