473,387 Members | 1,465 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.

plz help me to write this code

write a prog to input an integer x? wheer x>0?for
integer x,the program as to convert it into sum of
consecutive positive integters/
4 example the sum of interger 10=1+2+3+4.
the total number of consecutive positive integers
should be maximum example 9=2+3+4 and 9=4+5 but 9=2+3+4 is valid
some integer such as 4 cannot be represented
as a sum of consecutive positive integers.
in that case output is no answer
Dec 5 '06 #1
1 1241
write a prog to input an integer x? wheer x>0?for
integer x,the program as to convert it into sum of
consecutive positive integters/
4 example the sum of interger 10=1+2+3+4.
the total number of consecutive positive integers
should be maximum example 9=2+3+4 and 9=4+5 but 9=2+3+4 is valid
some integer such as 4 cannot be represented
as a sum of consecutive positive integers.
in that case output is no answer
----------------------------------------------------------------------------------------------------------------------

# include <iostream.h>

int main()
{
int num; // to get the number from user.
int sum = 0; //To sum of consecutive numbers.
int flag = 0; // To verify whether reqd sum is get or not
int loop; // for loop
int start; // To remember the starting of consecutive number.
char numbers[100];
cout << "Enter a number greater than 0: ";
cin >> num;
int loopstart = 1; // for starting of loop.
while (loopstart < num) //Loop to change the starting of consecutive numbers.
{
sum = 0; //Sum of consecutive numbers.
for (loop = loopstart; loop < num; loop++) //Loop to get the sum of consecutive numbers until the sum is get
{
sum = sum + loop;
if (sum == num) //if sum of consecutive number matches with the entered number.
{
flag = 1;
start = loopstart; // to remember the starting of number.
loopstart = num; // to come out while loop
break; //TO come out for loop
}
}
loopstart =loopstart + 1;
}
if (flag == 1)
{
cout << num << "=";
int j;
int arr = 0;
for (j = start; j<= loop;j++)
{
if (j < (loop)) // Print numbers followed by plus sign
{
cout << j << "+";
}
else // It will print last number without plus sign
{
cout << j << endl ;
}
}

}

else
{
cout << "Not Possible";
}
system ("PAUSE");
return 0;
}
Dec 5 '06 #2

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

Similar topics

10
by: Greg Hurlman | last post by:
I've got what I'm sure is a very simple problem. In an ASP page, I am trying to write out 4 fields from a recordset in succession: Response.Write rs("LastName") Response.Write rs("Suffix")...
1
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>");...
3
by: Ike | last post by:
Can anyone discern why the following code writes the document.write() lines literally? That is, a line like document.write('<CENTER>') should write <CENTER> but instead writes the entire ...
2
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
0
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
14
by: Eli | last post by:
I've got a script that I'm trying to debug which uses document.write() to place HTML within a page. In both IE6 and Firefox when I view source, I see only the script itself and not any HTML as...
4
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400,...
5
by: matthew | last post by:
Hi all, I am now writing a aspx that get a session variable (string) and then write it out using Response.Write. The string length is: 494710. But Response.Write only write the string...
4
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.