Evan
Evan

Reputation: 17

How to square with only a single starting integer?

I’m new to programming. I am to make a code that squares the integer. The rules is that:

  1. The input consist of only one integer
  2. It only loops up to 5 times
  3. The output should result with 3 integer places

Below is my desired result:

Input:

1

Output:

001
004
009
016
025

Below is my attempted code but it is not working.

int start=input.nextInt();

for(int i=5; i <=5; i++; start++){
    num[i]= start*start;
    }
    for (i=0; i<num.length; i++){
        System.out.println(“%03d\n”, num[i]);
    }
    }
    }

Upvotes: 0

Views: 48

Answers (2)

new_to_code
new_to_code

Reputation: 37

inputNum = sacnner.nextInt();
int loopTimes = 0;
While(loopTimes < 5 , loopTimes++){
       System.out.println("%30d\n", inputNum*inputNum);
       inputNum += 1
   }

Upvotes: 0

Lakshman
Lakshman

Reputation: 479

Maybe you are looking for something like this. Ideone link https://www.ideone.com/IzO18y

import java.util.*;
import java.lang.*;
import java.io.*;
 
 
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
 
        for(int i=1;i<=5;i++){
            System.out.printf("%03d\n",n*n);
            n+=1;
        }
    }
}

Upvotes: 2

Related Questions