Computer Science, asked by Parthan2006, 2 months ago

Java Program to Print the squares of numbers from 1 to n. (Class 9)

Answers

Answered by anindyaadhikari13
7

Solution:

The given co‎de is written in Java.

import java.util.*;

public class Squares    {

   public static void main(String args[])  {

       Scanner sc=new Scanner(System.in);

       int n,i;

       System.out.print("Enter n - ");

       n=sc.nextInt();

       System.out.println("Squares of numbers from 1 to "+n+" are as follows..");

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

           System.out.print(i*i+" ");

   }

}

Logic:

  • Enter N.
  • Loop for I = 1 to N, Increment = 1:
  •   Display the value of I × I.
  • End Loop.

See the attachment for output.

•••♪

Attachments:
Answered by BrainlyProgrammer
8

Answer:

import java.util.Scanner;

class SqrNum{

public static void main (String ar []){

Scanner sc= new Scanner (System.in);

System.out.println("Enter limit");

int n=sc.nextInt();

for(int I=1;I<=n;I++)

System.out.println("Square of "+I+" is "+(I*I));

}

}

Logic:-

  • Nothing so complicated, just run a loop from 1 to n taking n as input where n is a limit
  • Print the Square by using (I*I) or (Math.pow(I,2))
  • Math.pow() returns in double data type value.

Required Output Attached.

Attachments:
Similar questions