Write a program in C++ to find the sum of first 10 natural numbers

  • برمجة سي بلس بلس
  • برمجة

Write a program in C++ to find the sum of first 10 natural numbers

Sample Output:

 Find the first 10 natural numbers:                                    
---------------------------------------                                
 The natural numbers are:                                              
1 2 3 4 5 6 7 8 9 10                                                   
 The sum of first 10 natural numbers: 55

الأجوبة

#include <iostream>
using namespace std;
int main()
{
    int i,sum=0;
    cout << "\n\n Find the first 10 natural numbers:\n";
    cout << "---------------------------------------\n";
    cout << " The natural numbers are: \n";
    for (i = 1; i <= 10; i++) 
    {
        cout << i << " ";
		sum=sum+i;
    }
     cout << "\n The sum of first 10 natural numbers: "<<sum << endl;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...