Natural Number Summation
Problem Statement: Make a program function that take two number start and end, and print all the number between the start and end number. Also calculate and print the total sum of numbers.Solution Program
Source code:
JavaScript Source Code
<html>
<body>
<form action="#">
<label for="start">Enter your start number</label>
<input type="number" id="start">
<label for="end" class="ending">Enter your end number</label>
<input type="number" id="end">
<p id="enter"></p>
<button onclick="G_sum()" class="btn">Generate Sum</button>
<label for="t_no">Total Number Read</label>
<p id="t_no" class="box"> 0 </p>
<label for="sum">Sum of Total Number</label>
<p id="sum" class="box"> 0 </p>
</form>
<script>
function G_sum()
{
let start_num = Number(document.getElementById("start").value);
let end_num = Number(document.getElementById("end").value);
if (start_num == "" || end_num == "")
{
document.getElementById("enter").innerHTML = "Please provide numbers";
}
else
{
document.getElementById("enter").innerHTML = "";
document.getElementById("t_no").innerHTML = "";
let i, t_sum = 0;
for (i = start_num; i <= end_num; i++)
{
document.getElementById("t_no").innerHTML += i + " , ";
t_sum = t_sum + i;
}
document.getElementById("sum").innerHTML = t_sum;
}
}
</script>
</body>
</html>
C Source Code
include<stdio.h>
void main ()
{
int a, b;
printf("enter the starting number");
scanf("%d",& a);
printf("enter the end number");
scanf("%d",& b);
G_sum (a,b);
}
void G_sum (int a, int b)
{
int i, t_sum = 0;
for (i = a; i <=b; i++)
{
printf("%d , ", + = i);
t_sum = t_sum + i;
}
printf(t_sum);
}