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