vendredi 1 mai 2020

C++ program to find sum of N numbers using while loop

Im a newbie in C++, so when im trying to run this code the compiler throws an error

while_st_2.cpp:18:1: error: expected class-name at end of input ~ ^

Can u please tell me how i can fix this, what does the expected class name at the end of input mean?

`#include <iostream>
// program to sum the numbers from 1 to N, taking N as input
int main ()
{
        int  val=1, sum=0, N=0;
        std::cout << "Enter N " << std::endl;
        std::cin >> N ;
        //while loop to compute the sum of N natural numbers
        while(val <= N) {
                sum += val; // assigns sum + val to sum
                ++val; // add 1 to val
        }
        std::cout << "The sum of " <<  N << " natural numbers inclusive is " << sum << std::endl;
        return 0;
}  
`

Aucun commentaire:

Enregistrer un commentaire