Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,144 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,065 people online right now. Registration is fast and FREE... Join Now!




Sum of the squares of integers (using for/while loops)

 
Reply to this topicStart new topic

Sum of the squares of integers (using for/while loops)

crovaxpsodc
10 Apr, 2007 - 09:32 PM
Post #1

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 9


My Contributions
I have an assignment to create a program that receives an input from the user (an integer) and then prints that integer and the next 10 integers, the squares of those integers, and the sum of those squares. I have the first two parts, but I'm having some trouble with the last part involving the sum. Here is what I have:

CODE
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i,j,k=0,count=0;
    
    printf("Enter an integer: ");
    scanf("%d",&i);
    j=i;
    
    for (i;count<=10;i++&&count++) printf("%d ",i);
    printf("\n");
    
    count=0;
    for (j;count<=10;j++&&count++) printf("%d ", j*j);
    printf("\n");
    
    for(j;j<=10;j++) {printf("%d ",j*j); k+=j*j;} printf("%d ",k);
    
    
    system("pause");
    return 0;
}


When I run the program and input a value for the integer, it prints out something like this (use 3 as the input, for example):
3 4 5 ... 13
9 16 25 ... 169
0

I'm not sure why the last loop is printing 0. If anyone could help me with this, I'd really appreciate it.
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Sum Of The Squares Of Integers (using For/while Loops)
10 Apr, 2007 - 09:45 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
CODE
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i,j,k=0,count=0;
    
    printf("Enter an integer: ");
    scanf("%d",&i);
    j=i;
    
    for (i;count<=10;i++&&count++) printf("%d ",i);
    printf("\n");
    
    count=0;
    for (j;count<=10;j++&&count++) printf("%d ", j*j);
    printf("\n");
    
    for(j;j<=10;j++) {printf("%d ",j*j); k+=j*j;} printf("%d ",k);
    
    
    system("pause");
    return 0;
}


look at second for loop. you are incrementing j there and then
look at third for loop, you are not setting j to 0.
so j is 11 here and third for loop does not executes.
and I am not getting that && thing in for loop. I think a simple comma will do there.

here are the modified code

CODE

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i,j,k=0,count=0;
    
    printf("Enter an integer: ");
    scanf("%d",&i);
    j=i;
    
    for (i;count<=10;i++,count++)
                    printf("%d ",i);

    printf("\n");
    i=j;    

    for (count=0;count<=10;j++,count++)
                    printf("%d ", j*j);

    printf("\n");
    for(count=0;count<=10;count++,i++)
               {
                   k+=(i*i);
               }
               printf("%d ",k);    
    
    system("pause");
    return 0;
}


I hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month