473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Express a number as the sum of three or more consecutive numbers in c

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
Apr 1 '20 #1
6 2561
Rabbit
12,516 Expert Mod 8TB
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.
Apr 1 '20 #2
AjayGohil
83 64KB
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void printNumber(int l,int f)
  3. {
  4.     int x;
  5.       for ( x = f; x<= l; x++) 
  6.       {
  7.       printf(" %d +",x);
  8.  
  9.       }
  10.         f++;
  11. }
  12.  
  13. void getNumber(int n)
  14. {
  15.     int last,first;
  16.     for ( last=1; last<n; last++) 
  17.     { 
  18.         for ( first=0; first<last; first++) 
  19.         { 
  20.             if (2*n == (last-first)*(last+first+1)) 
  21.             { 
  22.                 printf("%d =",n);
  23.                 printNumber(last, first+1); 
  24.                 return; 
  25.             } 
  26.         } 
  27.     } 
  28.  
  29.    printf("Not possible");  
  30. }
  31.  
  32. int main()
  33. {
  34.     int n;
  35.     printf("Enter input:");
  36.     scanf("%d",&n);
  37.     getNumber(n);
  38.     return 0;
  39. }
Apr 1 '20 #3
Rabbit
12,516 Expert Mod 8TB
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.
Apr 1 '20 #4
SioSio
272 256MB
The answer is not necessarily one set.
Expand|Select|Wrap|Line Numbers
  1. void getNumber(int n)
  2. {
  3.     int A;
  4.     int B;
  5.     int C;
  6.     int D;
  7.     int max = n / 3 + 1;
  8.     int Y[1000];
  9.     int Z[1000];
  10.     for (A = 0; A <= max; A++) {
  11.         B = A * (A + 1) / 2;
  12.         Z[A] = B;
  13.         Y[A] = B - n;
  14.     }
  15.     for (C = 0; C <= max; C++) {
  16.         for (D = 0; D <= max; D++) {
  17.             if (Z[C] == Y[D]) {
  18.                 printf("from %d to %d\n", C + 1, D);
  19.                 break;
  20.             }
  21.         }
  22.     }
  23. }
Or
Expand|Select|Wrap|Line Numbers
  1. void getNumber(int n)
  2. {
  3.     int A;
  4.     int B[1000];
  5.     int C[1000];
  6.     int D;
  7.     int max = n / 3 + 1;
  8.     for (A = 0; A <= max; A++)
  9.     {
  10.         B[A] = A * (A + 1) / 2;
  11.         C[A] = B[A] - n;
  12.         for (D = 0; D <= max; D++)
  13.         {
  14.             if (C[A] == B[D]) {
  15.                 printf("from %d to %d\n", D+1,A);
  16.                 break;
  17.             }
  18.         }
  19.     }
  20. }
Apr 2 '20 #5
Rabbit
12,516 Expert Mod 8TB
This will find all solutions in the fewest iterations possible.
Expand|Select|Wrap|Line Numbers
  1. void findSolution(int target) {
  2.     int i, solution;
  3.     for(i=3; (i * (i+1))/2 <= target; i++) {
  4.         solution = target - (i * (i-1))/2;
  5.         if(solution % i == 0 && solution > 0){
  6.             solution = solution/i;
  7.             printf("\n%d consecutive numbers starting at %d", i, solution);
  8.         }
  9.     }
  10. }
Apr 2 '20 #6
Thanks, i understand it now :)
May 5 '20 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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,...
10
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...
0
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...
9
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,...
4
hsn
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...
3
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....
2
bwesenberg
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. ...
2
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? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.