Wednesday, 5 July 2017

Java Code for loop example

Problem:

Given an integer, , print its first  multiples. Each multiple  (where ) should be printed on a new line in the form: N x i = result.

Solution:

package com.myprograms;
import java.util.Scanner;
public class LoopExample {

   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       System.out.println("enter n value");
       int n = in.nextInt();
       for(int i =1;i <= 10; i++){
        System.out.println(n + " * " + i + " = " + n*i);
       }
       in.close();
   }

}

Output:

enter n value
5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

No comments:

Post a Comment

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...