Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Thursday, 25 May 2023

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string

import java.util.*;

public class CountChars

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        String s; int i;

        System.out.println("Enter a String");

        s=in.nextLine();

        for(i=0;i<s.length();i++)

           if(s.charAt(i)=='\0')

              break;

        System.out.println("The String "+s+" has "+i+" characters");

    }

}


Output:

img

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string

import java.util.*;

public class CountChars

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        String s; int i;

        System.out.println("Enter a String");

        s=in.nextLine();

        for(i=0;i<s.length();i++)

           if(s.charAt(i)=='\0')

              break;

        System.out.println("The String "+s+" has "+i+" characters");

    }

}

Wednesday, 14 April 2021

Loops in Java

 In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in Java.






Java For Loop vs While Loop vs Do While Loop

Comparisonfor loopwhile loopdo while loop
IntroductionThe Java for loop is a control flow statement that iterates a part of the programs multiple times.The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.
When to useIf the number of iteration is fixed, it is recommended to use for loop.If the number of iteration is not fixed, it is recommended to use while loop.If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop.
Syntax
for(init;condition;incr/decr){  
// code to be executed 
}
while(condition){  
//code to be executed 
}
do{  
//code to be executed  
}while(condition); 
Example
//for loop  
for(int i=1;i<=10;i++){  
System.out.println(i);  
}  
//while loop  
int i=1;  
while(i<=10){  
System.out.println(i);  
i++;  
}  
//do-while loop  
int i=1;  
do{  
System.out.println(i);  
i++;  
}while(i<=10); 
Syntax for infinitive loop
for(;;){  
//code to be executed  
}
while(true){  
//code to be executed  
}
do{  
//code to be executed  
}while(true);  

Thursday, 21 January 2021

PROGRAM IN JAVA TO ADD 2 MATRICES [ DOUBLE DIMENSIONAL ARRAYS ]

 import java.util.*;

class DDA

{

  public static void main(String args[])

  {

  Scanner in=new Scanner(System.in); 

  int i,j,n;

  System.out.println("Enter the Size of the Matrix");

  n=in.nextInt();

  int a[][]=new int[n][n];

  System.out.println("Enter the elements of the Matrix");

  for(i=0;i<n;i++)

  { 

  for(j=0;j<n;j++)

  a[i][j]=in.nextInt();

  }

  System.out.println("The Martix:");

  for(i=0;i<n;i++)

  {

  for(j=0;j<n;j++)

  System.out.print(a[i][j]+" ");

  System.out.println();

  }

  }

}

WAP in Java to read and display the sum of elements in a Single Dimensional array.

 import java.util.*;

class Sum_Array

{

  public static void main(String args[])

  {

  Scanner in=new Scanner(System.in);      

  int N,i,sum=0;

  System.out.println("Enter the size of the array");

  N=in.nextInt();

  int arr[]=new int[N];

  System.out.println("Enter "+N+" Numbers");

  for(i=0;i<N;i++)

  {

  arr[i]=in.nextInt();

  sum=sum+arr[i];

  }

  System.out.println("The Sum of the elements of the array is: "+sum);

  }

}

Friday, 15 January 2021

WAP in Java to read and display a Single Dimensional array.

import java.util.*;

public class Main

{

  public static void main(String args[])

  {

  Scanner in=new Scanner(System.in); 

  int N,i;

  System.out.println("Enter the size of the array");

  N=in.nextInt();

  int arr[]=new int[N];

  System.out.println("Enter "+N+" Numbers");

  for(i=0;i<N;i++)

  arr[i]=in.nextInt();

  System.out.println("The elements of the array are:");

  for(i=0;i<N;i++)

  System.out.println(arr[i]);

  }

}

Sunday, 10 January 2021

Java Program to accept a letter and check Whether a character is Vowel or Consonant.

 import java.util.*;

public class Main

{

    public static void main(String args[])

    {

    Scanner in=new Scanner(System.in);    

    char ch;

    System.out.println("Enter a Character to check whether it is a vowel or a Constant");

    ch=in.next().charAt(0);

    if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

    System.out.println(ch+" is a Vowel.");

    else 

    System.out.println(ch+" is a Constant.");

    }

}

Write a Program in Java to find Sum of 2 Numbers using assignment statement.

 public class Sum

{

    public static void main(String args[])

    {

        int a=10,b=20,sum;

        sum=a+b;

        System.out.println("Sum= "+sum);

    }

}

Saturday, 26 December 2020

Write a Program in Java to input 3 Numbers and display the smallest number. Hint: Use Math.min()

 import java.util.*;

class Min

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int a,b,c,min;

        System.out.println("Enter 3 Numbers");

        a=in.nextInt();

        b=in.nextInt();

        c=in.nextInt();

        min=Math.min(a,b);

        min=Math.min(min,c);

        System.out.println("Smallest Number= "+min);

    }

}

Write a Program in Java to find out the Diagonal of a Square taking side of a Square as Input.

import java.util.*;

class Diagonal

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        double a,d;

        System.out.println("Enter the side of the Square");

        a=in.nextInt();

        d=Math.sqrt(2)*a;

        System.out.println("Side of the Square= "+a);

        System.out.println("Diagonal of the Square= "+d);

    }

}

 /* Question: The final velocity of a vehicle can be calculated by using the formula:

V2=u2+2as;

where u= initial velocity, a=acceleration, s=distance covered

Write a Program in Java to calculate and Display the final velocity by taking initial velocity, acceleration and the distance covered as inputs. */

import java.util.*;

public class Velocity

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int u,a,s;

        double v;

        System.out.println("Enter the initial Velocity, acceleration and distance covered");

        u=in.nextInt();

        a=in.nextInt();

        s=in.nextInt();

        v=Math.sqrt((u*u)+(2*a*s));

        System.out.println("Final Velocity = "+v);

    }

}

Friday, 25 December 2020

Write a program to find the sum and average of “N” numbers.

 import java.util.*;

class SUM_AVG

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int N,Num,Sum=0,avg;

        System.out.println("Enter the Number of Numbers");

        N=in.nextInt();

       for(int i=1;i<=N;i++)

      {

         System.out.println("Enter a Number");

         Num=in.nextInt();

         Sum=Sum+Num;

  }

   avg=Sum/N;

   System.out.println("Sum= "+Sum+"\n"+"Average= "+avg);

}

}

Wednesday, 16 December 2020

WAP in Java to print Hello World for infinite Times.

 public class Infinite

{

public static void main()

{

    for(;;)

    System.out.println("Hello World");

}

}

Friday, 11 December 2020

Program in Java using Input Stream Reader Class.

 /* Question :-

A class contains 'b' number of boys and 'g' number of girls. On a rainy day, 'ba' number of boys and 'ga' number of girls are absent. Write a program to input the values of 'b', 'g', 'ba' and 'ga'. Calculate and display the following:

(i)  Percentage of girls present in the class.

(ii) Percentage of boys present in the class.

[Percentage must be calculated on the total number of Students]

*/

import java.io.*;

class Class

{

    public static void main(String args[])throws IOException

    {

    InputStreamReader read=new InputStreamReader(System.in);

    BufferedReader in=new BufferedReader(read);

    int b,g,ba,ga;

    float per_boys,per_girls;

    System.out.println("Enter the number of boys and girls in the class");

    b=Integer.parseInt(in.readLine());

    g=Integer.parseInt(in.readLine());

    System.out.println("Enter the number of boys and girls in the class who were absent");

    ba=Integer.parseInt(in.readLine());

    ga=Integer.parseInt(in.readLine());

    per_girls=(float)(g-ga)/(g+b)*100;

    per_boys=(float)(b-ba)/(g+b)*100;

    System.out.println("The Percentage of girls Present in the class : "+per_girls+"%");

    System.out.println("The Percentage of boys Present in the class : "+per_boys+"%");

    }

}

Thursday, 3 December 2020

Write a program in Java to Print Floyd's Triangle

 /*

 Floyd's Triangle:

 1

 2 3

 4 5 6

 7 8 9 10

 11 12 13 14 15

 */ 

public class Main

{

   public static void main (String[] args)

   {

   int i,j,p;

   for(i=1,p=1;i<=5;i++)

   {

       for(j=1;j<=i;j++)

       {

       System.out.print(p+" ");

       p++;

       }

       System.out.println();

   }

   }

    

}

Tuesday, 1 December 2020

Write a Program in Java to Calculate Simple Interest by taking input from the User.

import java.util.*;

class Simple_Interest

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        double SI,P,R,T;

        System.out.println("Enter The Principal, Rate and Time");

        P=in.nextDouble();

        R=in.nextDouble();

        T=in.nextDouble();

        SI=(P*R*T)/100.0;

        System.out.println("The Simple Interest= "+SI);

    }

}

Write a Program in Java to Accept a Word or a Sentence and Display it vertically.

import java.util.*;

class Vertical

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        String s;

        System.out.println("Enter a String");

        s=in.nextLine();

        for(int i=0;i<s.length();i++)

        System.out.println(s.charAt(i));

    }

}

Monday, 30 November 2020

PROGRAM TO ILLUSTRATE INFINITE for LOOP IN JAVA

 // //ILLUSTRATION 1

class NULL_LOOP

{

   public static void main(String args[])

   {

       int i;

       for(i=1;i<=10;i--)

       System.out.println("Hello");

   }

}


/*

//ILLUSTRATION 2

class NULL_LOOP

{

   public static void main(String args[])

   {

       int i;

       for(i=1;;i--)

       System.out.println("Hello");

   }

}

*/

/*

//ILLUSTRATION 3

class NULL_LOOP

{

   public static void main(String args[])

   {

       int i;

       for(;;)

       System.out.println("Hello");

   }

}

*/

Write a Program in Java to accept a Word Or Sentence and print the Second letter.

 import java.util.*;

 class Number

 {

     public static void main(String args[])

     {

         Scanner in=new Scanner(System.in);

         String s;

         System.out.println("Enter a Word or Sentence");

         s=in.nextLine();

         System.out.println("The Second Digit: "+s.charAt(1));

     }

 }

Saturday, 21 November 2020

Write a program in Java to Create a Single Dimensional Array of n integers. Print only the Armstrong numbers from the Array.

 /* Armstrong Number: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Let's try to understand why 153 is an Armstrong number.

153 = (1*1*1)+(5*5*5)+(3*3*3)  
where:  
(1*1*1)=1  
(5*5*5)=125  
(3*3*3)=27  
So:  
1+125+27=153  

*/

import java.util.*;

class ArmStrong

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int n,n1,d,cube,sum,i;

        System.out.println("Enter the size of the array");

        n=in.nextInt();

        int a[]=new int[n];

        System.out.println("Enter "+n+" Numbers");

        for(i=0;i<n;i++)

        a[i]=in.nextInt();

        for(i=0;i<n;i++)

        {

         n1=a[i];

         sum=0;

         while(a[i]>0)

        {

          d=a[i]%10;

          cube=d*d*d;

          sum=sum+cube;

          a[i]=a[i]/10;

        }

        if(sum==n1)

        System.out.println(n1+" is an Armstrong Number");

        else

        System.out.println(n1+" is not an Armstrong Number");

        }

    }

}


Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...