Computer Science, asked by Darvince, 11 months ago


Write a program to add numbers up to 'n' using \textbf{do while} loop.

Topic : Programming in C Language. ​

Answers

Answered by Anonymous
33

Answer:

How while loop works :-

The while loop evaluates the test expression inside the parenthesis ().

If the test expression is true, statements inside the body of while loop are executed. Then, the test expression is evaluated again.

The process goes on until the test expression is evaluated to false.

If the test expression is false, the loop terminates (ends).

To learn more about test expression (when the test expression is evaluated to true and false), check out relational and logical operators.

Example:-

// Print numbers from 1 to 5

// Print numbers from 1 to 5#include <stdio.h>

// Print numbers from 1 to 5#include <stdio.h>int main()

// Print numbers from 1 to 5#include <stdio.h>int main(){

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1;

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1;

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1; while (i <= 5)

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1; while (i <= 5) {

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1; while (i <= 5) { printf("%d\n", i);

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1; while (i <= 5) { printf("%d\n", i); ++i;

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1; while (i <= 5) { printf("%d\n", i); ++i; }

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1; while (i <= 5) { printf("%d\n", i); ++i; } return 0;

// Print numbers from 1 to 5#include <stdio.h>int main(){ int i = 1; while (i <= 5) { printf("%d\n", i); ++i; } return 0;}

Answered by Sauron
109

\mathfrak{\large{\underline{\underline{Answer : }}}}

#include<stdio.h>

#include<conio.h>

int main ()

{

 int sum = 0, num;

 printf ("Enter a number\n");

 scanf ("%d", &num);

 do

   {

     sum = sum + num;

     num --;

   }

while (num != 0);

 printf ("\nAddition is %d", sum);

}

\rule{300}{1.5}

In the above program, the user asked to enter a number. Suppose the user enters the input  as 7. The code inside the body executes and the sum becomes 7 (As sum = 0 + 7).

Then, n is decremented to 6 and the condition of while loop is checked, which is true. Again, the code inside  the body is executed and sum becomes 13.

This process continues until number becomes 0. When  num = 0 the loop condition becomes false, num becomes 28 and the loop gets terminated.

The first attachment is the code whereas the second one includes the output. (When the value is entered as 7)

Attachments:
Similar questions