Computer Science, asked by meetalibba19scmt, 1 month ago

Write a program to print the square using for loop for any entered number. (3)

Eg. 3=9

12=144

30=900​

Answers

Answered by anindyaadhikari13
1

Answer:

Assuming that the number entered is an integer, here comes the c∅de for the question.

1. In Java.

import java.util.*;

public class Java {

public static void main(String[] args) {

int n,i, s=0;

Scanner sc=new Scanner(System.in);

System.out.print("Enter a number: ");

n=sc.nextInt();

for(i=1;i<=Math.abs(n);i++) {

s+=Math.abs(n);

}

System.out.println("Square of "+n+" is: "+s);

sc.close();

}

}

2. In Python.

n,s=int(input("Enter a number: ")),0

for i in range(1,abs(n)+1):

s+=abs(n)

print(f"Square of {n} is {s}")

3. In C.

#include <stdio.h>

int main () {

int s=0,i, n;

printf("Enter a number: ");

scanf("%d", &n);

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

s+=abs(n);

printf("Square of %d is %d",n,s);

return 0;

}

4. In C++.

#include <iostream>

using namespace std;

int main() {

int s=0,i=1,n;

cout << "Enter a number: ";

cin >> n;

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

s+=abs(n);

cout << "Square of "<<n<<" is: "<<s;

return 0;

}

Algorithm:

  1. START.
  2. Accept the number, say n.
  3. Declare s=0
  4. Iterate a loop n times.
  5. Add the absolute value of n to s.
  6. Display the value of s.
  7. STOP.

See the attachment for output ☑.

Attachments:
Similar questions