Program in c, to find the consecutive series of a number, example.
Input:24
Output: 7+8+9=24
Another example
Input:26
Output:5+6+7+8
6 2561
We aren't here to do all the work for you but if you post what you've tried so far, we can help you along.
Hiii,
Refer Below equation:
Sum of first n natural numbers = n * (n + 1)/2
Sum of first (n + k) numbers = (n + k) * (n + k + 1)/2
If N is sum of k consecutive numbers, then
following must be true.
N = [(n+k)(n+k+1) - n(n+1)] / 2
OR
2 * N = [(n+k)(n+k+1) - n(n+1)]
you can try below code: - #include <stdio.h>
-
void printNumber(int l,int f)
-
{
-
int x;
-
for ( x = f; x<= l; x++)
-
{
-
printf(" %d +",x);
-
-
}
-
f++;
-
}
-
-
void getNumber(int n)
-
{
-
int last,first;
-
for ( last=1; last<n; last++)
-
{
-
for ( first=0; first<last; first++)
-
{
-
if (2*n == (last-first)*(last+first+1))
-
{
-
printf("%d =",n);
-
printNumber(last, first+1);
-
return;
-
}
-
}
-
}
-
-
printf("Not possible");
-
}
-
-
int main()
-
{
-
int n;
-
printf("Enter input:");
-
scanf("%d",&n);
-
getNumber(n);
-
return 0;
-
}
There's a slightly more efficient method that uses fewer iterations. Using the formula target = n*x + (n*(n-1)/2) , given that x must be an integer, you can test that a solution exists for n consecutive numbers by using the mod operator.
The answer is not necessarily one set. - void getNumber(int n)
-
{
-
int A;
-
int B;
-
int C;
-
int D;
-
int max = n / 3 + 1;
-
int Y[1000];
-
int Z[1000];
-
for (A = 0; A <= max; A++) {
-
B = A * (A + 1) / 2;
-
Z[A] = B;
-
Y[A] = B - n;
-
}
-
for (C = 0; C <= max; C++) {
-
for (D = 0; D <= max; D++) {
-
if (Z[C] == Y[D]) {
-
printf("from %d to %d\n", C + 1, D);
-
break;
-
}
-
}
-
}
-
}
Or - void getNumber(int n)
-
{
-
int A;
-
int B[1000];
-
int C[1000];
-
int D;
-
int max = n / 3 + 1;
-
for (A = 0; A <= max; A++)
-
{
-
B[A] = A * (A + 1) / 2;
-
C[A] = B[A] - n;
-
for (D = 0; D <= max; D++)
-
{
-
if (C[A] == B[D]) {
-
printf("from %d to %d\n", D+1,A);
-
break;
-
}
-
}
-
}
-
}
This will find all solutions in the fewest iterations possible. - void findSolution(int target) {
-
int i, solution;
-
for(i=3; (i * (i+1))/2 <= target; i++) {
-
solution = target - (i * (i-1))/2;
-
if(solution % i == 0 && solution > 0){
-
solution = solution/i;
-
printf("\n%d consecutive numbers starting at %d", i, solution);
-
}
-
}
-
}
Thanks, i understand it now :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Adam |
last post by:
Hi,
I am trying to mark consective numbers in a data set and get a count as
to how many consecutive numbers exist in the a given line of data.
Here is an example line:
3, 5, 7, 9, 10, 13,...
|
by: ChrisD |
last post by:
I'm trying extract a count of consecutive numbers, or "unbroken" years in
this case, at any particular given time.
For example (simplified):
CREATE TABLE #Customers
(
CustNo INT,
YearNo...
|
by: Dennis Ruppert |
last post by:
Greetings
This should be easy, but I am stuck!
I have a table that I import from another program. There are 25
fields, but I only need to use 3 of them for what I need to do. After
I import...
|
by: boliches |
last post by:
I have a seperate table to generate consecutive numbers. Using "Dmax" to find the largest number to increment .
My problem is that I want the number to begin at 1000 at the start of each month,...
|
by: hsn |
last post by:
i am having a problem \
i am preparing for a programming contest
and some questions are coming to me with an input of numbers each number has more than 10 digits in it.
i know i can store them in...
|
by: emeped |
last post by:
I am using C#.NET 2003. Please I want to know if it is possible and how to insert the same data into 2 or more consecutive rows in a datatable column, at the same time, by a single click of a button....
|
by: bwesenberg |
last post by:
I am not sure how to explain this so I will try to the best of my ability.
I need to be able to produce 1000 labels from access that are based on consecutive numbers that Access will produce. ...
|
by: LuisAngel |
last post by:
How do I extract 9 consecutive numbers from URLs like the following:
http://itunes.apple.com/us/app/redlaser/id312720263?mt=8
The desired extraction is 312720263.
Will the following work?
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |