answersLogoWhite

0

#include<stdio.h>

void main()

{

int n,sum=0;

clrscr();

printf("enter the value"):

scanf("%d",&n);

do

{

sum=sum+n;

n=n-1;

printf("sum is %d"<sum);

}

while(n>0)

getch();

}

User Avatar

Wiki User

14y ago

What else can I help you with?

Continue Learning about Engineering

Individual digits sum with flowchart and algorithm?

Well, it's very hard to write a flowchart in text, so I'll give you some pseudo code instead. int number = the given number int sum = 0 loop while number is not 0 sum = sum + (number mod 10) number = number / 10


What are the types of loops?

There are three forms of loop commonly used in C/C++, the for loop, the while loop and the do-while loop. The for loop is most commonly used whenever an action is going to be performed a set amount of times. For example, to sum every element in an array: for(i = 0; i &lt; arraySize; i++) { sum = sum + array[i]; } The while loop and do-while loop are commonly used to loop until a condition is met. The difference between the two is that the do-while loop goes through one iteration before checking its condition, while the while loop checks its condition before any execution of the loop. Example do-while loop: do { randomNumber = rand() % 10; }while(randomNumber != 6); Example while loop: cout &gt; number; while(number &lt; 0) { cout &gt; number; }


How do you write while andfor loop to get sum of1to100 integer?

int i, sum; /* example using while */ sum = 0; i = 1; while (i &lt;= 100) { sum += i; ++i; } /* example using for */ sum = 0; for (i = 1; i &lt;= 100; ++i) sum += i;


Write a java script program to find sum of the digits of a given integer?

import java.util.*;public class Digit{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.print("Enter Number:");String r = sc.nextLine();int[] b = new int[r.length()];String [] a = new String[r.length()];int k = 0;System.out.println("\n");System.out.print("The Digits of the number are: ");for (int i = 0; i < r.length(); i++){a[i] = r.substring(i,i+1);System.out.print(a[i]);System.out.print(" ");k = k + Integer.parseInt(a[i]);}System.out.println("\n");System.out.print("The sum of the digits is: ");System.out.println(k);}}Output:C:\>java DigitEnter Number:123456789The Digits of the number are: 1 2 3 4 5 6 7 8 9The sum of the digits is: 45Modified while loopAnswer// start off with an integer int n = some_integer;// this will keep track of the sum of the digits of n...// we set it to n initially to simplify our loop logicint sum_digits = n;// this loop is to handle cases where the sum of the digits is initially greater than 10// and we need to sum the digits againwhile(sum_digits >=10) {// reset sum_digitssum_digits = 0;// use log function to get the number of digits in nint num_digits = (int) Math.log10(n) + 1;// here is the actual work of the algorithm...do {// (n % 10) will return the last (right-most) digit of n,// which we will add to our sumsum_digits += n % 10;// now divide n by 10 to get rid of the digit we just addedn /= 10;}while( n > 0 ); // loop until we're done with this number// set n to the sum of the digits in case we need to loop againn = sum_digits}


Write a program in Java to print the numbers 1 to 100?

class sum { void main () { int sum = 0; int n = 1; while ( n &lt;= 100 ) { sum = sum + n; n++ ; } System.out.println("Sum is = " + sum ); }}

Related Questions

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.


What is a C programming to calculate the sum of 1-2-3-4-5 and so on using while loop a for loop and a do while loop?

#include using std::cout;using std::endl;int main(){int number(1);cout number;//using loop forint sum(0);for(int i(1); i


Individual digits sum with flowchart and algorithm?

Well, it's very hard to write a flowchart in text, so I'll give you some pseudo code instead. int number = the given number int sum = 0 loop while number is not 0 sum = sum + (number mod 10) number = number / 10


How do you sum a number's all digit using for loop?

Using for loop we can find sum of digits of a number. Inside the loop just perform Logic Expression 1) rem=num%10. {To find the unit place no. using remainder functon} 2) sum = sum+rem {to find the addition ie output} 3) num=num/10 {to eliminate the added digit} Just repeat these steps in the loop.


How do you make a c plus plus program using while loop to print a sum of digits?

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&lt;int&gt;&amp; v) { int total=0; int i=0; while( i&lt;v.size() ) total+=v[i]; return total; }


The sum of the digits of the number is 6?

the sum of my digits is 6? answer=60


How do you find the sum of digits of each number?

Add the digits together. The sum of the digits of 23 is 5.


There is a number that is 5 times the sum of its digits What is this number?

The number is 45. The sum of its digits i.e. 4+5=9 Five times the sum of its digits i.e. 5 times 9 which is 45


Write a java program to find out the sum of a number of n digits?

class Sum_Of_Digits { public static void printSumandnoofdigits(int n) { int temp = n; int count = 0; int sum = 0; while ( n &gt; 0 ) { sum = sum + n % 10; n = n / 10; count ++; } System.out.println("The number is..." + temp ); System.out.println("The sum of digits is..." + sum); System.out.println("The number of digits is..." + count); } }


What are the types of loops?

There are three forms of loop commonly used in C/C++, the for loop, the while loop and the do-while loop. The for loop is most commonly used whenever an action is going to be performed a set amount of times. For example, to sum every element in an array: for(i = 0; i &lt; arraySize; i++) { sum = sum + array[i]; } The while loop and do-while loop are commonly used to loop until a condition is met. The difference between the two is that the do-while loop goes through one iteration before checking its condition, while the while loop checks its condition before any execution of the loop. Example do-while loop: do { randomNumber = rand() % 10; }while(randomNumber != 6); Example while loop: cout &gt; number; while(number &lt; 0) { cout &gt; number; }


A number that is equal to the sum of the digits of its cube?

8 is the same as the sum of the digits of its cube, 512.


What is the sum of the digits of the number 11?

1 + 1 = 2 The sum of the digits is therefore 2.