answersLogoWhite

0

Place the digits in an array (or vector) and use the loop to sum each digit to a running total initialised to zero. The following function demonstrates the loop:

int sum_vector(std::vector<int>& v)

{

int total=0;

int i=0;

while( i<v.size() )

total+=v[i];

return total;

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Using while loop write a program which calculates the product of digits from 1 to 5 and also show these nos vertically?

Using while loop, write a program which calculates the product of digits from 1 to 5 and also show these no's vertically.


How do you write a c program to print n no's required using while loop?

int i=0; while (i++&lt;n) { /*...*/ }


How can we print a book using google print?

"While it is possible to use this program to print a book, it is highly recommended to print it with an actual publisher. You will get a much better deal and your book will look a lot more professional."


Write a program using while loop?

//program to find the factorial value f any number using while loop #include&lt;stdio.h&gt; void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&amp;n); i=n; while (i&gt;=1) { fact=fact*i; i--; } printf("The factorial value=%d",fact); } the above is a program for calculating tha factorial value of any number which is entered by the user


I ned a program to print sum of digits without using while loop?

In python 3 I would write the following (assuming the digits are input by a user): #!/usr/bin/python a = input("Type a number: ") sum = 0 for i in a: #the for next loop performs the function of a while loop with less lines of code. #(My formating keeps getting deleted when I publish this answer. There should always be a tab- strictly speaking four spaces before this line) sum = sum + int(i) sum = str(sum) print("Your number was", a + ". The sum of its digits is" , sum + ".") As you can see, the for next loop iterates through each value in the string "a", and sets its value to "i". For each iteration, the current value of i is added to the variable "sum" until the end of the number is reached. The program then prints the original number, and the sum of its digits.


Is it possible to make a program using for and not with while?

The following cases are all possible:- program without any for and while- program without for- program without while- program with both for and while


Can an ear or elbow print be used to identify someone aren't only digits palms and soles unique?

While most believe you can only get a fingerprint or tongue print really any part of your body will leave a skin print.


How do you write a C program to reverse the 3 digits of a number using while loop?

unsigned reverse (unsigned num) { unsigned rev = 0; while (num) { rev *= 10; rev += num % 10; num /= 10; } return rev; }


What is the greatest 5 digit number using 3 different digits?

The greatest 5-digit number using 3 different digits is 99987. This number is formed by using the digit '9' three times, along with '8' and '7' to maximize the overall value while adhering to the condition of using only three different digits. Other combinations, like 99876 or 99875, do not yield a higher number while still using only three distinct digits.


How can I frame a print without using glass?

To frame a print without using glass, you can consider using alternative materials such as acrylic, plexiglass, or UV-protective coatings. These options can provide protection for the print while avoiding the use of traditional glass.


Write a program using method plainto check whether a string is a palindrome or not A palindrome is a string that reads the same from left to right and vice-versa?

Palindrome number is a number like 121 which remains the same when its digits are reversed. To find this number in a simple java program, just follow the below way. sum = 0; while(n&gt;0) { r=n % 10; sum=concat(r); n=n / 10; } print r;


Write a c program to print the 100 to 1 nos?

Write a c program to print the 100 to 1 nos