当前位置:网站首页>Use the while loop to calculate the odd and even sums in 1-100 [method 1]

Use the while loop to calculate the odd and even sums in 1-100 [method 1]

2022-06-21 22:17:00 Programming expert

【 Title Description 】
utilize while loop , Separate calculation 1-100 Sum of odd numbers in 、 An even sum .

The code is as follows :

notes : Code for reference only , There is not only one solution .

//while loop 【 Method 1 】
#include<iostream>
using namespace std;
int main(){
    
 int i=1,sum1=0,sum2=0;
 while(i<=100) {
    if(i%2==0) sum1=sum1+i;else sum2=sum2+i;i++;} 
 cout<<"sum1="<<sum1<<" sum2="<<sum2;
 return 0;
}
原网站

版权声明
本文为[Programming expert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212027390969.html