How to write a C program to find and sum all numbers divisible by 9 between 100
Introduction
Welcome to our comprehensive guide on finding and summing all numbers divisible by 9 between 100 and a user input in a C program. In this article, we will provide you with a step-by-step approach to solve this problem efficiently and effectively. By following our instructions, you will be able to create a high-performing C program that not only solves the task at hand but also stands out from the competition. So, let’s dive right in!
Understanding the Problem
Before we start writing the code, it’s essential to have a clear understanding of the problem statement. We are tasked with finding and summing all numbers that are divisible by 9 within a specific range. The lower bound of the range is fixed at 100, while the upper bound will be determined by the user’s input.
Algorithm
To accomplish our goal, we will follow a systematic algorithm that ensures accuracy and efficiency in our program. Here are the steps we will take:
Step 1: Accept User Input
We begin by taking input from the user to determine the upper bound of the range. This value will be stored in a variable for later use.
Step 2: Initialize Variables
Next, we initialize a variable to hold the sum of the divisible numbers and another variable to iterate through the range starting from 100.
Step 3: Iterate Through the Range
Using a loop, we iterate through the range of numbers starting from 100 up to the user-defined upper bound. For each number, we check if it is divisible by 9 using the modulo operator (%). If the number is divisible, we add it to the sum variable.
Step 4: Display the Result
Once the loop has finished iterating through the range, we display the final sum of all the divisible numbers to the user.
How to write a C program to find and sum all numbers divisible by 9 between 100
Implementation
Example 1:
Now that we have a clear algorithm in mind, let’s implement it in C code. Here’s a sample implementation that you can use as a reference:
#include <stdio.h>
int main() {
int userInput, sum = 0;
printf("Enter the upper bound: ");
scanf("%d", &userInput);
for (int i = 100; i <= userInput; i++) {
if (i % 9 == 0) {
sum += i;
}
}
printf("The sum of numbers divisible by 9 between 100 and %d is: %d\n", userInput, sum);
return 0;
}
Example 2:
#include<stdio.h> void main () { int i,n,disvi=1,sum=0; scanf ("%d",&n); printf("Numbers between 100 and 200:"); for (i=100;i<=n;i++){ if(disvi=(i%9==0)){ printf(" %d ",i); sum=i+sum; } } printf("\nThe sum:%d",sum); }
Conclusion
Congratulations! You have successfully created a C program that finds and sums all the numbers divisible by 9 between 100 and the user input. By following the steps outlined in this article, you now have a competitive advantage in outranking other websites offering similar content.
Remember, providing valuable and comprehensive articles is just one aspect of improving search rankings. To further enhance your website’s visibility, we recommend delving into the world of search engine optimization (SEO). Understanding SEO best practices will help you optimize your content, increase organic traffic, and ultimately achieve higher rankings on search engine result pages (SERPs).