Write a program that calculates the sum S = 1 + 2 + 3 + ... + 10. Using the do-while Loop.
******** In Algorithm ********
Algorithm Variables i,S: integers Begin S ← 0 i ← 1 Do S ← S + i i ← i+1 while ( i <= 10 ) write("1+2+...+10 =", S ) End Result ==> 1+2+...+10 = 55
******** In C ********
#include <stdio.h> int main(){ int i=1 , S=0; do{ S = S+i; i++ ; // i = i+1 } while ( i <= 10 ) ; printf("1+2+3+....+10 = %d",S); return 0; }
******** In C++ ********
#include <iostream>
using namespace std;
int main(){
int i=1,S=0;
do{
S = S+i;
i++ ;
}while(i <= 10);
cout<<" 1+2+3+..+10 = "<<S;
return 0;
}
******** in JAVA ********
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int i=1 , S=0;
do {
S = S+i;
i++ ;
}while ( i <= 10 );
System.out.print("1+2+...+10= "+S);
}
}
******** in C# ********
using System;
public class Ex22 {
public static void Main(string[] args)
{ int i=1 , S=0;
do {
S = S+i;
i++ ;
}while ( i <= 10 );
Console.Write("1+2+....+10= "+S);
}
}
class Main {
public static void main(String[] args) {
int i=1 , S=0;
do {
S = S+i;
i++ ;
}while ( i <= 10 );
System.out.print("1+2+...+10= "+S);
}
}
using System;
public class Ex22 {
public static void Main(string[] args)
{ int i=1 , S=0;
do {
S = S+i;
i++ ;
}while ( i <= 10 );
Console.Write("1+2+....+10= "+S);
}
}