Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

Write a c++ program using for loop that accepts any 2 positive numbers then display the nos. from the lower no. to the bigger no. and compute and print the sum. Sample run: Enter two no.: 5 2 the nos. from 2 to 5 are: 2 3 4 5 the sum is: 14

OpenStudy (anonymous):

have you tried it ?

OpenStudy (anonymous):

find min number first , then using loop print min_number , min_number++ , ..., max_number

OpenStudy (anonymous):

Here is a code to solve this problem : ********************************************************************** #include<iostream> //I include this so I can use : cout and cin #include<stdlib.h> // I include this so I can use : system("pause") using namespace std; int main(){ int a, b, sum=0; //The two numbers a and b, and sum is for the sum. cout<< "Enter two numbers : "; //a small message to the user. cin >> a >> b; //Read a and b. cout<< "The numbers from " << a <<" To " << b <<" are : "; for(int i=a; i<=b; i++){ sum = sum + i; //I calculate the addition every time i increment. cout<<" " <<i; // I print out the value of i. } cout<< "\nThe sum is : " <<sum <<endl; // At the end I print the value of sum. system("pause"); } ********************************************************************** Good luck.

OpenStudy (anonymous):

@ktobah congratulations, you enable him to copy and paste your code without understanting anything

OpenStudy (anonymous):

apart from that, your code is wrong and i won't tell you where the bug is :P

OpenStudy (anonymous):

oh you both are welcome. For the bug you can send him a private message to tell him about it, that will be good i think.

OpenStudy (anonymous):

@maheen_khizar yes. i tried. but their's something wrong with my program. but i made some changes. here it is. #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int a, b, sum=0; cout<<"Enter first number: "; cin>> a; cout<<"Enter second number: "; cin>>b; if (a<=0 || b<=0) { cout<<"\nYou entered " <<a<< " and " <<b<<". Please Enter a positive integer.\n"; } else if (a==b) { cout<<"\nInvalid input!\n"; } else if (b>a) { cout<< "The numbers from " << a <<" To " << b <<" are : "; for(int x=a; x<=b; x++) { sum = sum + x; cout<<" " <<x; } cout<< "\nThe sum is : " <<sum <<endl; } else { cout<< "The numbers from " << a <<" To " << b <<" are : "; for(int x=b; x<=a; x++) { sum = sum + x; cout<<" " <<x; } cout<< "\nThe sum is : " <<sum <<endl; } system("pause"); return 0; }

OpenStudy (anonymous):

@infinity_ no. i don't copy paste other work. that would be plagiarism. actually @ktobah and i have the same code.

OpenStudy (anonymous):

@ktobah thanks for replying.

OpenStudy (anonymous):

@kris100 it's nothing personal, i just say that by posting code you can enable others to paste it without further thought.Ktobah's code takes for granted that a<=b which is not necessarily correct

OpenStudy (anonymous):

@kris100 You're welcome, @infinity_ yeah true, that's what I supposed.

OpenStudy (anonymous):

@kris100 i didnt compile your code but implemented logic seems correct , is there still any issue in your code?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!