1. Write a program in c language to find the sum of all numbers between two given numbers using a while loop.
Introduction
Loops are a fundamental concept in programming that allow us to repeat a block of code until a certain condition is met. In C programming, the while loop is a popular choice for iterating through a set of instructions. This article aims to introduce beginners to the concept of loops and demonstrate how a while loop can be used to find the sum of numbers between two given values.
Purpose
The purpose of this article is to provide a clear explanation of loops in C programming, specifically focusing on the while loop. By showcasing a practical example of finding the sum of numbers, readers will gain a better understanding of how loops can be utilized in solving computational problems.
Agenda
Step-by-step guide on writing a program to calculate the sum of numbers using a while loop.
Objective
By the end of this article, readers will be able to:
Understand the concept of loops and their role in programming.
Implement a while loop in C to find the sum of numbers between two given values.
Recognize the practical applications of loops in problem-solving.
Real Life Applications
The ability to find the sum of numbers using a while loop has practical applications in various scenarios, such as:
Calculating total expenses within a budget range.
Summing up scores in a game.
Analyzing data points within a specific range for statistical analysis.
/* a program in c programming language to find the sum of all
numbers between two given numbers using a while loop. */
#include <stdio.h>
int main()
{
int start, end, sum = 0;
// Prompt the user to enter the starting and ending numbers
printf("\nEnter the starting number: ");
scanf("%d", &start);
printf("\nEnter the ending number: ");
scanf("%d", &end);
// Initialize a variable 'current' to the starting number
int current = start;
// Use a while loop to iterate through numbers from 'start' to 'end'
while (current <= end) {
sum += current; // Add the current number to the sum
current++; // Move to the next number
}
// Display the sum of numbers between the given range
printf("\nThe sum of numbers between %d and %d is: %d\n", start, end, sum);
return 0;
}
Output:
Enter startinng number: 5
Enter the ending number: 10
The sum of numbers between 5 and 10 is:45
Explanation
Include the #include<stdio.h> Standard Input-Output Library header file to ensures that the functions for input and output processes are accessible.
int main() function definition starts the execution of the program.
Define start, end and sum variables in main() function to store beginning number, ending number and total numbers.
Implement printf and scanf functions to prompt the user to enter start and end numbers.
Assign the user supplied initial value to a variable named current.
Write a while loop to iterate between integers from beginning to end. Use the += operator to add the current integer to the sum. Increment the current variable to move to the next number.
Use the printf function to display the sum of integers in a specified range after the loop completes.
At the end, return the number 0 to show the successful execution of the program.
Previous :-->>Conditionl statements Programs
-->> NEXT:
2.Write a program in to compute the factorial of any given number using a do while loop.
-->>ALL
Loops Assignments in c