Java Loops I

Objective
In this challenge, we’re going to use loops to help us do some simple math.

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

Input Format

A single integer.

Output Format

Print 10  lines of output; each line i contains the  of in the form:
N x i = result.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

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

The another way one can do it is

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i=1;

while(i<=10)

{
System.out.println(n + ” x ” +i+” = “+(n*i));

i+=1;
}
}
}

So basically there are 4 loops in java

  1. for loop
  2. while loop
  3. do while loop
  4. for each

In this post you’ve seen both the for loop and while loop , next post will share the remaining loop structures

Feel free to comment.

Advertisements

2 thoughts on “Java Loops I

  1. Alex says:

    You actually make it seem so easy with your presentation but I find this matter to be actually something that I think I’d by no means understand. It sort of feels too complicated and extremely large for me. I’m having a look ahead on your next publish, I’ll attempt to get the hold of it!

    Like

  2. Gertrude Wheeler says:

    Thank you a bunch for sharing this with all folks you really understand what you are talking approximately! Bookmarked. Kindly also consult with my web site =). We could have a hyperlink alternate arrangement among us!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s