write a program using do- while loop to compute the sum of the first 50 positive odd integers
Answers
Answered by
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:
- START.
- Initialise s=0, i=1
- Iterate do-while loop till i(<=50) is true.
- Add the value of i in s.
- Increment the value of i.
- Display the sum.
- STOP.
See the attachment for output ☑.
Attachments:
Answered by
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