0

print 1 to end square of numbers in c

using function in c write a program to 1 to end(entered by user) square of numbers. #include<stdio.h> int square(int x); int main() { int n,i,s; printf("Enter the value of n"); scanf("%d",&n); printf("%d",square(i)); } int square(int i){ int n,s; for(i=1;i<=n;i++){ s=i*i; printf("%d %d",i,s); } }

13th May 2018, 6:09 PM
Jordan
Jordan  - avatar
1 Answer
+ 1
I've added comments to point out issues. #include<stdio.h> int square(int x); int main() { int n,i,s; // s is never used printf("Enter the value of n"); scanf("%d",&n); printf("%d",square(i)); // i hasn't been assigned } int square(int i){ int n,s; for(i=1;i<=n;i++){ // n hasn't been assigned s=i*i; printf("%d %d",i,s); } } // Failed to return a value so garbage will be printed
14th May 2018, 12:21 AM
John Wells
John Wells - avatar