Description
Get the first and last value as input from the user, then calculate the sum within the given range.
Input
3
6
Output
18
C Program
#include <stdio.h>
int main()
{
int fvalue,lvalue,sum=0;
printf("Enter the first and last value: ");
scanf("%d %d",&fvalue,&lvalue);
for(int i=fvalue;i<=lvalue;i++)
sum=sum+i;
printf("%d",sum);
return 0;
}
C++ Program
#include <iostream>
using namespace std;
int main()
{
int first,last,sum=0;
cout<<"Enter the first and last values: ";
cin>>first>>last;
for(int i=first;i<=last;i++)
sum=sum+i;
cout<<sum;
return 0;
}
Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int first = sc.nextInt();
int last = sc.nextInt();
int sum= 0;
for(int i=first;i<=last;i++)
sum = sum + i;
System.out.print(sum);
}
}
Python
first=int(input("Enter first value: "))
last=int(input("Enter last value: "))
Sum=0
for i in range(first,last+1):
Sum=Sum+i
print(Sum)