Skip to main content

Posts

Write a program to accept the starting and ending range. Display the sum of even and odd numbers .

Write a program to accept the starting and ending range. Display the sum of even and odd numbers . import java.util.Scanner; public class Range {     public static void main(String args[])     {         Scanner sc=new Scanner(System.in);                                   System.out.println("Enter the starting and ending range:");         int m = sc.nextInt();         int n = sc.nextInt();         if(m<n){             int so=0,se=0;             for(int i=m;i<=n;i++)             {                 if(i%2==0)                 {                     se=se+i;        ...

Write a program to accept 50 numbers. Count and display the numbers which are positive and negative.

Write a program to accept 50 numbers. Count and display the numbers which are positive and negative. import java.util.Scanner; class posneg {     public static void main(String args[])     {         Scanner sc=new Scanner(System.in);         System.out.println("Enter 50 numbers:");         int nc=0,pc=0;         for(int i=1;i<=50;i++)         {             System.out.println("Enter number:");             int n=sc.nextInt();             if(n>=0)             {                 pc=pc+1;                 System.out.println(n+" is positive number");             }             else{          ...

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...