Skip to main content

Posts

Showing posts from March, 2019

Java program to print multiplication table

Java program to print multiplication table of a number entered by a user using a  for  loop. You can modify it for  while  or  do while  loop for practice. Using nested loops (a loop inside another loop), we can print tables of numbers between a given range say a to b, for example, if the input numbers are '3' and '6' then tables of '3', '4', '5' and '6' will be printed. Java programming source code import   java.util.Scanner ;   class   MultiplicationTable {     public   static   void   main ( String   args [ ] )     {       int   n, c ;       System . out . println ( "Enter an integer to print it's multiplication table" ) ;     Scanner in   =   new   Scanner ( System . in ) ;     n   =   in. nextInt ( ) ;       System . out . println ( "Multiplication table of "   + ...

Java program to print alphabets

Java program to print alphabets Java program to print alphabets on screen, i.e., a, b, c, ..., z; in lower case. Java source code class  Alphabets {     public   static   void  main ( String  args [ ] )     {        char  ch ;          for   ( ch  =   'a' ;  ch  <=   'z' ;  ch ++ )           System . out . println ( ch ) ;     } } You can easily modify the above java program to print alphabets in upper case. Download  Alphabets  program class file. Output of program: Printing alphabets using a while loop (Only the body of the main method is shown): char  c  =   'a' ;   while   ( c  <=   'z' )   {    System . out . println ( c ) ;   c ++; } Using a do while loop: char  c  =...

Java while loop

Java while loop Java while loop is used to execute statement(s) until a condition is true. In this tutorial, we will learn to use while loop with examples. First of all, let's discuss while loop syntax: while (condition(s)) { // Body of loop } 1. If the condition(s) holds true then the body of the loop is executed, after execution of the loop body condition is tested again and if the condition is still true then the body of the loop is executed again, and the process repeats until the condition(s) becomes false. The condition evaluates to true or false and if it is a constant, for example, while (c) {…}, where c is a constant, then any non zero value of c is considered to be true, and zero is considered false. 2. You can test multiple conditions such as while   ( a  >  b  &&  c  !=   0 )   {   // Loop body } Loop body is executed till value of variable a is greater than value of...

Java for loop

Java for loop Java for loop is used to repeat execution of the statement(s) until a certain condition holds true.  for  is a keyword in Java programming language. Java for loop syntax for   ( /* Initialization of variables */   ;   /*Conditions to test*/   ;   /* Increment(s) or decrement(s) of variables */ ) {    // Statements to execute, i.e., Body of a for loop } You can initialize multiple variables, test many conditions and perform increments or decrements on many variables according to requirement. Please note that all three components of a for loop are optional. For example, following for loop prints "Java programming" indefinitely. // Infinite for loop for   (   ;   ;   )   {    System . out . println ( "Java programming" ) ; } You can terminate an infinite loop by pressing  Ctrl + C . Simple for loop example in Java Example program below uses fo...