how to sum all numbers under a given number c++

int x, s = 0;
   cout << "Enter the number : ";
   cin >> x;
   while (x != 0) {
      s = s + x % 10;
      x = x / 10;
   }

3.78
9
Phoenix Logan 186120 points

                                    // Method 1: Mathematical -&gt; Sum up numbers from 1 to n 
int sum(int n){
	return (n * (n+1)) / 2;
}

// Method 2: Using a for-loop -&gt; Sum up numbers from 1 to n 
int sum(int n){
	int tempSum = 0; 
  	for (int i = n; i &gt; 0; i--){
     	tempSum += i;  
    }
  	return tempSum; 
}

// Method 3: Using recursion -&gt; Sum up numbers from 1 to n 
int sum(int n){
	return n &gt; 0 ? n + sum(n-1) : 0; 
}

3.78 (9 Votes)
0
5
1
Awgiedawgie 440220 points

                                    #include &lt;iostream&gt;
#include &lt;math.h&gt;
// &quot;#include &lt;math.h&gt; isn't needed.
using namespace std;
/* run this program using the console pauser or add your own getch, system(&quot;pause&quot;) or input loop */

int main(int argc, char** argv) {
	// Sum of numbers in a group under a given number.
	cout &lt;&lt; &quot;Insert four numbers:&quot; &lt;&lt; endl;
	// In this case the numbers I chose to use are four but you can use how many you want.
	float a, b, c, d;
	cin &gt;&gt; a &gt;&gt; b &gt;&gt; c &gt;&gt; d;
	float aa, bb, cc, dd;
	// The second set of variables must have the same number of variables of the numbers
	// you want to sum.
	if(a &lt; 8){
	// In this case &quot;n&quot; is replaced by 8, but, again, you can use any number you prefer.
		aa = a;
		if(b &lt; 8){
			bb = b;
			if(c &lt; 8){
				cc = c;
				if(d &lt; 8){
					dd = d;
				} else {
					dd = 0;
				}
			} else {
				cc = 0;
				if(d &lt; 8){
					dd = d;
				} else {
					dd = 0;
				}
			}
		} else {
			bb = 0;
			if(c &lt; 8){
				cc = c;
				if(d &lt; 8){
					dd = d;
				} else {
					dd = 0;
				}
			} else {
				cc = 0;
				if(d &lt; 8){
					dd = d;
				} else {
					dd = 0;
				}
			}
		}
	} else {
		aa = 0;
		if(b &lt; 8){
			bb = b;
			if(c &lt; 8){
				cc = c;
				if(d &lt; 8){
					dd = d;
				} else {
					dd = 0;
				}
			} else {
				cc = 0;
				if(d &lt; 8){
					dd = d;
				} else {
					dd = 0;
				}
			}
		} else {
			bb = 0;
			if(c &lt; 8){
				cc = c;
				if(d &lt; 8){
					dd = d;
				} else {
					dd = 0;
				}
			} else {
				cc = 0;
				if(d &lt; 8){
					dd = d;
				} else {
					dd = 0;
				}
			}
		}
	}
	cout &lt;&lt; endl &lt;&lt; &quot;Sum of the numbers lower than n (8): &quot; &lt;&lt; aa+bb+cc+dd &lt;&lt; endl;
	// Basically this associates each number to a variable of the second group and
	// then, through if, it sees which numbers are under n.
	// If they are under n the second variable is given the same numeric value as
	// its respective number, if not its value is equal to 0.
	// It then sums the variables together.
	// To have more/less numbers you add the respective variables, then make two
	// cases where the number is higher or lower than n. Then, you copy-paste
	// the two if chains underneath.
	return 0;
}

5 (1 Votes)
0
3.83
6
Krish 100200 points

                                    // Method 1: Mathematical -&gt; Sum up numbers from 1 to n 
int sum(int n){
	return (n * (n+1)) / 2;
}

// Method 2: Using a for-loop  -&gt; Sum up numbers from 1 to n 
int sum(int n){
	int tempSum = 0; 
  	for (int i = n; i &gt; 0; i--){
     	tempSum += i;  
    }
  	return tempSum; 
}

// Method 3: Using recursion -&gt; Sum up numbers from 1 to n 
int sum(int n){
	return n &gt; 0 ? n + sum(n-1) : 0; 
}

3.83 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
sum of a number in c++ sum of digits of a number in c++ using for loop add digits of a number c++ how to add digits to a number c++ n number sum to find a element using c++ n number sum using c++ sum of integers of a number c++ value of a element is sum of all the numbers before a number in the array cpp sum of digits to a single digit in c++ get sum of digits of a number c++ sum of the digits in cpp sum of 1 to n numbers in c++ f. digits summation in c++ sum of n natural numbers in cpp sum number from 1 to N IN C++ get the sum of digits of an int cpp get numbers that sums up to n c++ get number that sums up to n c++ how to sum all the elements of a numeric string in c++ summing the digits of a number cpp adding up numbers in an integer cpp sum of digits of numbers in c++ how to add the digits of a number in c++ print the sum of natural numbers c++ c++ program to print the sum of n natural numbers sum of digits of numbers ic c++ HOW TO SUM OF 3 NUMBER IN C++ c++ program to sum of digits c++ code for sum of digit of number sum of n natural numbers in c ++ c++ program to sum c++ code for sum of numbers find sum for numbers from 1 to 10 c++ calculate sum of digits of a number c++ program to sum of digits in c++ int sum{} meaning c++ sum of n numbers c++ how to add digits of a number in c++ c++ sum of n numbers sum of digits in a string c++ sum of n numbers using array in c++ algorithm C++ program to find sum of n natural numbers using array calculate sum of numbers in array c++ c++ sum of numbers by array c++ sum of digits in an integer sum of digits inputted c++ find sum of digits of a number in cpp find the sum of digits of a number in c++ summation of digits c++ find sum of digits c++ function to sum all number in c++ get sum of digit of int c++ get sum of int c++ find sum(int, int) c++ how to print sum of numbs in c++ sum number c++ sum of digits in string c++ Write a program of c++ function that finds the sum of the digits in any given integer. write a program in c++ to find the sum of digits of a given number. Write a c++ program a function that finds the sum of the digits in any given integer. funtion to find sum of digits in c++ hwo to find sum of digits in c++ sum of n number c++ sum of digits in c++ function create a function to calculate the sum of any n numbers c++ c++ sum of all numbers up to a number function sum of digits in c++ using for loop sum of digit in cpp find sum of digit c++ how to do sum of digit in cpp c++ sum digits of a number a function that finds the sum of digits in an int c++ sum of 1 to n c++ find the sum from 1 to n c++ finding the sum of digits in c++ sum of natural numbers cpp determine whether the sum is made using the numbers c++ c++ problems to get the sum of digits in a number sum of digits using class in c++ sum of natural numbers in c++ summ all numbers in array c++ add numbers c++ sum of two numbers c++ sum of number using function in c++ sum of digit c++ Calculate the sum of digits of a number given by user. c++ sum n number in c++ sum of integers function c++ how to make an addition sum in c++ how to sum of 4 digit numbers in c++ sum of 10 numbers in c++ easiest way to add the digits of a number in c++ digit sum in c++ how to find sum of digits in c++ addition of 5 digits in C++ sum digits of a number c++ return the sum of two numbers c++ sum of n natural numbers in c++ sum of numbers in array in c++ how to find the sum of digits of an integer in c++ get sum of digits c++ sum of digits of a number in c++ digit sum c++ sum of all digits in cpp sum int c++ C++ sum the digits of an integer sum of digits cpp how to sum all numbers under a given number c++ sum of natural numbers in cpp calculate the sum of digits of a number given by user c+ sum of digits c++ how to sum number c++ sum of 3 digit number in c++ how to get the sum og digits in c++ how sum c++ sum of two numbers in c++ sum of digitts of a number in cpp additioni of digit in cpp c++ add all digits of a number. total digits in a number c++ how to find the sum of digits in c++ how to get the sum of all digits in a number in c++ c++ program to find sum of natural numbers add all the elements in numbers cpp how to create the sum in c++ how to find the sum of the digits in a number in c++ find sum of digits of a number c++ c++ program to find sum of digits of a given number function to get sum of the digits cpp c++ sum of digits of a number c++ sum of digits sum of digits of a number in cpp program to find sum of all digits of a number in c++ sum of digits of a number c++ sum of n numbers in cpp sum the digit of number in cpp sum up all numbers in int array c++ sum of the digits number c ++ the sum of the digits of a number c++ sum of all natural numbers to N cpp sum of all natural numbers to 100000 c++ sum of all natural numbers c++ c++ code to find sum of digits of a number c++ program to find sum of digits of a number using for loop c++ program to find sum of n numbers using function overloading write a program to to add sum of n numbers entered by user using function overloading in c++ write a program to to add n sum of numbers using function overloading in c++ c++ programsum of n digit numbers C++ program calculation of natural numbers from 1 to n c++ program to find sum of digits of a number using function Write a program to create a function to calculate the sum of the individual digits of a given number. in C++ write a program that prompt user enter an integer of 6 digits then calculate the sum of odd digits in c++ whole numbers in c++ how to find sum of digits of a number c++ c++ sum of 3 digits in number natural numbers average c++ sum of natural numbers in c++ code print sum of every digit in array c++ how can you take input natural numbers in c++ print the sum of digits of x digit number in c++ c++ code for sum of n numbers how to add digits of a number c++ for loop adding numbers c++ sum of digits in c++ digit sum integer c++ sum of all digits in a number c++ run on digits in number c++ how to get a desired sum from a list of numbers in cpp sum of number in c++ sum numbers from 1to 100 c++ how to make a program in c++ which adds infinite numbers sum from 0 to 100 in c++ What is the sum of all numbers 1 to 1000? in c++ c++ function to find the sum odf each didgit of an umner each digit is sum of previous 2 digits c++ sum of digits of a number in C++ funciton how to get sum of all digits c++ digits sum in c++ how to get sum of diits of number c++ express a number as the sum of digits in c++ function to give me sum of digits of a number in c++ function to give me sum of numbers in c++ sum of 10 natural numbers in c++ program to find sum of digits of number c++ Write a C++ program to print sum of numbers from 2 to 8. short form of sum=sum +n in c++ c++ program to input a number and find the sum of its digits sum of digits in cpp c++ sum up all numbers C++ Write a program that calculates the sum for the following using a loop. c++ program to find sum of n numbers using for loop C++ the sum of a sequence of two numbers within a loop summation Write a program that calculates the summation for the following using a loop. The user will provide the values for i and k. C++ Write a program that calculates the sum for the following using a loop. The user will provide the values for i and k. C++ summation of two numbers in a loop C++ c++ for calculate all numbers c++ calculate numbers how natural number declare in c++ output sum in c++ program sum of numbers in c++ if (sum &lt; 2295) c++ Write a program to ask the user to enter 5 numbers. Then, display the sum of all integer numbers entered by the user. sum of numbers c++ c++ code different n numbers addition print all the sum number from 1 to 15 in c++ sum =+10 in c++ c++ sum of numbers loop c++ sum how to add 10 in c++ summation of c++ sum c++ Calculate 2^50 using c++ sum of natural numbers in c++ in O(1) C++ for loop to compute sum c++ sum numbers check for first N natural number in C++ genrate n natural number using generate in c++ how to compute ~1 in c++ Write a CPP program to calculate sum of first N natural numbers find the sum of no c++ is there a limit when caculate total in c++ sum of n natural numbers c++ c++ sum up numbers c++ sum of numbers c++ sum of all numbers up to a number
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source