C++小例子

#include <iostream>
#include <conio.h>
int main(){
    std::cout<<"Enter two numbers:"<<std::endl;
    int v1,v2;
    std::cin>>v1>>v2;
    int lower,upper;
    if(v1<=v2){
        lower = v1;
        upper = v2;
    }else{
        lower = v2;
        upper = v1;
    }
    int sum = 0;
    for(int val = lower;val<=upper;++val){
        sum +=val;
    }

    std::cout<<"Sum of "<<lower<<"to"<<upper
             <<" is "
             <<sum<<std::endl;
    getch();
    return 0;
}

输入两个数

计算出,他们之间的和

例如输入1、3

计算1+2+3

posted @ 2014-05-12 16:34  TBHacker  阅读(378)  评论(0编辑  收藏  举报