******** In Algorithm ********
Algorithm
Variables i,S,N: integers
Begin
i ← 1 S ← 0
write("Enter an integer:")
read (N)
While ( i <= N )
S ← S + i
i ← i + 1
next
write("the sum 1+2+....+",N,"= ",S) End
End
Result : ==> Enter an integer : 6 the sum 1+2+....+6 = 21
******** In C ********
#include <stdio.h> int main(){ int i=1 , S=0 ,N; printf("Enter an integer :"); scanf ("%d",&N); while ( i <= N ) { S = S+i; i++ ; } printf("the sum 1+2+....+%d = %d\n",N,S); return 0; }
******** In C++ ********
#include <iostream>
using namespace std;
int main(){
int i=1 , S=0 ,N;
cout<<"Enter an integer :";
cin>>N;
while ( i <= N ){
S = S+i;
i++ ;
}
cout<<"the sum 1+2+....+<<N<<" = "<<S;
return 0;
}
******** in Python ********
******** in Python ********
i=1
S=0
N = int(input("Enter N:"))
while i <= N :
S = S + i
i = i + 1
print("the sum 1 +2+...+ N =:",S)
******** in JAVA ********import java.util.Scanner;
class Main {
public static void main(String[] args){
Scanner sc = new Scanner (System.in);
System.out.print("Enter N :");
int N = sc.nextInt();
int i=1 , S=0;
while ( i <= N ) {
S = S+i;
i++ ;
}
System.out.print("the sum 1 ->"+N+" =: "+S);
}
}
********* in C# *******
using System;
public class Ex15 {
public static void Main(string[] args)
{ int i=1 , S=0 ,N;
Console.Write("Enter N :");
N=int.Parse(System.Console.ReadLine());
while ( i <= N ) {
S = S+i;
i++ ;
}
Console.Write("the sum 1+2+...+"+N+" = "+S);
}
}
class Main {
public static void main(String[] args){
Scanner sc = new Scanner (System.in);
System.out.print("Enter N :");
int N = sc.nextInt();
int i=1 , S=0;
while ( i <= N ) {
S = S+i;
i++ ;
}
System.out.print("the sum 1 ->"+N+" =: "+S);
}
}
using System;
public class Ex15 {
public static void Main(string[] args)
{ int i=1 , S=0 ,N;
Console.Write("Enter N :");
N=int.Parse(System.Console.ReadLine());
while ( i <= N ) {
S = S+i;
i++ ;
}
Console.Write("the sum 1+2+...+"+N+" = "+S);
}
}