answersLogoWhite

0

the following is C code, should work well enough. If not, you should get the general idea.

int counter;

for (int i=1;i<10;i++)

{

println("Enter a number: ");

cin << counter;

}

println("Total, %i", counter);

User Avatar

Wiki User

17y ago

Still curious? Ask our experts.

Chat with our AI personalities

BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
BeauBeau
You're doing better than you think!
Chat with Beau
More answers

#include<stdio.h> void main() { int i,n,f1=0,f2=1;f,sum=0; printf("enmter the n value"); scanf("%d",&n); for(i=1;i<=n;i++) { f1=f2; f2=f; f=f1+f2; sum=sum+f; }//loop end }main end

User Avatar

Wiki User

16y ago
User Avatar

Place the numbers in an array, then pass the array to a function that sums the squares. Note that you must check for overflow to ensure the returned value is valid.

long long sum_squares (const int a[], const int size, bool* overflow) { int i;

long long sum, product;

*overflow=false;

sum = 0;

for (i=0; i<size; ++i) {

if (*overflow = product_overflow (a[i], a[i])) break;

product = (long long) a[i] * a[i];

if (*overflow = sum_overflow (sum, product)) break;

sum += product;

}

return sum;

}

Example usage:

int main (void) {

bool overflow;

int a[] = {42, 39, 56, INT_MAX }; // a likely overflow

long long sum = sum_squares (a, 4, &overflow)

if (overflow) {

// sum is invalid

} else {

// sum is valid

}

return 0;

}

The overflow test functions have the following definitions:

#include<limits.h> // defines INT_MAX and LLONG_MAX

bool sum_overflow (const long long a, const long long b) {

return a > 0 && b > LLONG_MAX - a;

}

bool product_overflow (const long long a, const long long b) {

return a > 0 && b > LLONG_MAX / a;

}

User Avatar

Wiki User

8y ago
User Avatar

for(int i = 0; i <= 10; ++i) {

}

User Avatar

Wiki User

15y ago
User Avatar

int main()

{

for(int i = 1; i <= 10; i++)

std::cout << i << endl;

return 0;

}

User Avatar

Wiki User

16y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Display numbers from 1 to 10 using for loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp