Skip to main content

Posts

Write a program to allow two players to play a game of dice. Each player has 5 consecutive Chances of rolling the dice. The player who has higher to total score is declared the winner. In case of a tie, the player who threw a lower number on the first roll is declared the winner

Write a program to allow two players to blay a game of dice. Each player has 5 consecutive Chances of rolling the dice. The player who has higher to tal score is declared the winner. In case of a tie, the player who threw a lower number on the first roll is declared the winner Code : import java.util.Scanner; public class Main {  public static void main(String[] args) {   Scanner sc = new Scanner(System.in);   int scoreA[] = new int[5];   int scoreB[] = new int[5];   int tscoreA = 0, tscoreB = 0;   System.out.println("Player 1 Plays:");   for (int i = 0; i < 5; i++) {    System.out.println("Press 1 to roll the dice");    sc.next();    int r = (int)(Math.random() * 6) + 1;    scoreA[i] = r;    tscoreA += r;   }   System.out.println("Player 2 Plays:");   for (int i = 0; i < 5; i++) {    System.out.println("Press 1 to roll the dice");    sc.next();    int ...

Write a program to accept a word. Check if the word is a happy word or not.

/** Suppose : VAT ASCII code of V : 86 -64 => 22 ASCII code of A : 65 - 64 => 1 ASCII code of T : 84 - 64 => 20 Position of V: 22 Position of A: 1 Position of T: 20 Subtract the ascii value with 64,so we get the position values To join the position values together, VAT => 22120 We need to perform this calculate 2*2+2*2+1*1+2*2+0*0 => 13 1*1+3*3 => 10 1*1+0*0 => 1 If we get 1 at the very end, then this is Happy Word */ import java.util.Scanner; class HappyWord {  public static void main(String args[])  {   Scanner sc = new Scanner(System.in);   System.out.println("Enter a word: ");   String str = sc.next().toUpperCase();   String num="";   for(int i=0;i<str.length();i++)   {    char ch = str.charAt(i); //we get each every character    int ascii = (int)ch;    int pos=ascii-64;    num=num+Integer.toString(pos);   }   System.out.println("The combined position is "+num);   i...

Write a program to accept a sentence. Display the word with maximum number of vowel.

Write a program to accept a sentence. Display the word with maximum number of vowel. import java.util.Scanner; class maxvowel {     public static void main(String args[])     {         Scanner sc=new Scanner(System.in);         System.out.println("Enter a sentence:");         String str=sc.nextLine()+" ";         String w="",mvw=""; //w word and mvw maximum vowel word          int mv=0; //maximum vowels         for(int i=0;i<str.length();i++)         {             char ch=str.charAt(i);             if(ch==' ')             {                 int nv=0; //number of vowels                  for(int j=0;j<w.length();j++)         ...

Write a program to accept a sentence. Find the shortest word present in the sentence.

Write a program to accept a sentence. Find the shortest word present in the sentence. import java.util.Scanner; class shortestWord {     public static void main(String args[])     {         Scanner sc=new Scanner(System.in);         System.out.println("Enter a sentence:");         String str=sc.nextLine()+" ";         int min=99999;         String w="",sw=""; //w for word          for(int i=0;i<str.length();i++)         {             char ch=str.charAt(i);             if(ch==' ')             {                 if(w.length()<min)                 {                     min=w.length();       ...

Write a program to accept 5 student names and their marks. Display the names in the descending order of the marks using bubble sort algorithm.

Write a program to accept 5 student names and their marks. Display the names in the descending order of the marks using bubble sort algorithm. Input: Ravi 25 Sheetal 30 Rajeev 27 Karan 28 Utsav 24 Output: Sheetal 30 Karan 28 Rajeev 27 Ravi 25 Utsav 24 import java.util.Scanner; class meritlist { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String names[] = new String[5]; int marks[] = new int[5]; System.out.println("Enter 5 names and their marks:"); for(int i=0;i<5;i++) { System.out.println("Enter name:"); names[i]=sc.next(); System.out.println("Enter marks:"); marks[i]=sc.nextInt(); } for(int i=0;i<5-1;i++) { for(int j=0;j<5-i-1;j++) { if(marks[j]<marks[j+1]) //for ascending order marks[j]>marks[j+1] { //marks sorting descending order  int t = marks[j]; marks[j=marks[j+1]; marks[j+1]=t; //name sortin...

Write a program to accept 5 names and their age. Arrange the names in ascending order along with their age.

Write a program to accept 5 names and their age. Arrange the names in ascending order along with their age. Input: Sheetal 25 Ishita 24 Punam 28 Avishek 20 Raima   22 Output: Avishek 20 Ishita  24 Punam 28 Raima 22 Sheetal 25 import java.util.Scanner; class sortnames { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter 5 names and their age:"); String names[] = new String[5]; int age[] = new int[5]; for(int i=0;i<5;i++) { System.out.println("Enter names:"); names[i]=sc.next(); System.out.println("Enter age:"); age[i]=sc.nextInt(); } for(int i=0;i<5-1;i++) { for(int j=0;j<5-i-1;j++) { if(names[j].compareTo(names[j+1])>0) //>0 means ascending and <0 means descending { //name sort  String t=names[j]; names[j]=names[j+1]; names[j+1]=t; //age sort int temp = age[j]; age[j]=age...

Write a program to accept a 3x3 matrix. Display the square of the product of the left and right diagonal sum of the matrix.

Write a program to accept a 3x3 matrix. Display the square of the product of the left and right diagonal sum of the matrix. Input Matrix: 1   2    3 4   5    6 7   8    9 Output :   Product of the left diagonal = 1*5*9=45 Product of the right diagoal = 3*5*7 = 105 Sum of the products 45+105 = 150 Square of the sum = 12.2 import java.util.Scanner; class matrixsquare { public static void main(String args[]) { int A[][] = new int[3][3]; // 3 rows and 3 columns Scanner sc=new Scanner(System.in); System.out.println("Enter numbers into the matrix:"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { A[i][j]=sc.nextInt(); } } int ld=1,rd=1; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ld=ld*A[i][i]; //left diagonal product rd=rd*A[i][3-i-1]; //right diagoal product  } } int sp = ld+rd; //sum of the product  double sq = Math.sq...