+ 1

number and square in c

Write a program to display the following output using for loop, 1 1 2 4 3 9 4 16

..10 100.

21st Apr 2018, 7:45 AM
Jordan
Jordan  - avatar
4 Answers
0
for(i=1;i<limit; i++){ int result = i*i; printf(" %d ", result); }
21st Apr 2018, 9:05 AM
Akib
Akib - avatar
+ 6
Just for those who want it in one line : for(int i = 1; i < 11; printf("%d %d ", i, i*i), ++i);
22nd Apr 2018, 6:48 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
#include <stdio.h> int print_square(int n) { int s=1; if(n < s) return -1; else for(;s <= n; s++) printf("%d %d ", s, s * s); return 0; } int main(int argc, int** argv) { int n; printf("Input : "), scanf("%d", &n); return print_square(n); } ------- execute ------ Input : 10 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100
21st Apr 2018, 8:22 AM
Code Koo
Code Koo - avatar
0
1) In case you have specified number of digits needed and all you need is output, not storing values. for ( i=1; i=<n; i++) printf("%d", i*i); 2) In case you need this values stored, i would recommend using an array and later manipulating with Array data (you can sort it, delete elements, add new, modify etc) int array[] ; for ( i=1; i=<n; i++) array[i-1] = i*i; //printing array for (i=0; i<n; i++) printf("%d", array[i]);
21st Apr 2018, 8:26 AM
Luka Vasiljevic
Luka Vasiljevic - avatar