Computer Science, asked by kad56, 3 months ago

write a program using do- while loop to compute the sum of the first 50 positive odd integers​

Answers

Answered by anindyaadhikari13
16

Answer:

This is the required program for the question.

1. In Java.

public class Java {

public static void main(String[] args) {

int s=0, i=1;

do {

s+=i++;

} while(i<=50);

System.out.println("Sum: "+s);

}

}

2. In C.

#include <stdio.h>

int main () {

int s=0, i=1;

do {

s+=i;

i++;

} while(i<=50);

printf("Sum: %d", s);

return 0;

}

3. In C++.

#include <iostream>

using namespace std;

int main() {

int s=0,i=1;

do {

s+=i;

i+=1;

} while(i<=50);

cout << "Sum: "<< s;

return 0;

}

Algorithm:

  1. START.
  2. Initialise s=0, i=1
  3. Iterate do-while loop till i(<=50) is true.
  4. Add the value of i in s.
  5. Increment the value of i.
  6. Display the sum.
  7. STOP.

See the attachment for output ☑.

Attachments:
Answered by Anonymous
4

Answer:

public class Java {

public static void main(String[] args) {

int s=0, i=1;

do {

s+=i++;

} while(i<=50);

System.out.println("Sum: "+s);

}

}

Explanation:

Similar questions