Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Colombo
Red Hat Certified System Administrator (RHCSA) - RHEL 10
Sanjeewani95
Updated:
Yesterday at 7:43 PM
NURSING , CAREGIVER , HOTEL & BEAUTY COURSES
IVA Para Medical Campus
Updated:
Thursday at 9:24 AM
Handmade Character Soft Toys Peppa Pig Family
anil1961
Updated:
Wednesday at 9:58 PM
Ad icon
Video Content Creator
pramukag
Updated:
Sunday at 6:10 AM
Ad icon
QA Engineer Intern
pramukag
Updated:
Sunday at 6:07 AM
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Help
programme logic eka kiyanna menna me examlpe ekata??
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="viraj_slk" data-source="post: 7402455" data-attributes="member: 51932"><p><strong>One possible solution to think of</strong></p><p></p><p></p><p></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">Good to see that u have asked for the logic rather than a code.</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">Here's one way I thought of to do it:</span></p><p><span style="font-size: 12px">first assign ur number to a variable as integer :</span></p><p><span style="font-size: 12px">x = 9087236781;</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">then convert the same number to a string value. This is so that I can loop through each character.</span></p><p><span style="font-size: 12px">str = (string_cast) x;</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">now str = '9087236781';</span></p><p><span style="font-size: 12px">so..for example: str[0] is equal to 9;</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">now use a loop like this to <strong>keep adding the numbers together UNTIL the total is less than or equal to 9.</strong></span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">[CODE]</span></p><p><span style="font-size: 12px">while (x > 9)</span></p><p><span style="font-size: 12px">{</span></p><p><span style="font-size: 12px"> str = (string_cast) x;</span></p><p><span style="font-size: 12px"> len = length(x); // the length of the number </span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> total = 0;</span></p><p><span style="font-size: 12px"> for (i = 0; i < len; i++) </span></p><p><span style="font-size: 12px"> {</span></p><p><span style="font-size: 12px"> total += (integer_cast)str[i];</span></p><p><span style="font-size: 12px"> }</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px"> x = total;</span></p><p><span style="font-size: 12px">}</span></p><p><span style="font-size: 12px">[/CODE]</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">finally.. when the loop gets finished, 'x' will have a single digit value that is less than or equal to 9, and is also the summation of numbers in '9087236781'</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">Let me know if u need a bit more help to clarify this one.</span></p><p> <span style="font-size: 12px"></span></p><p><span style="font-size: 12px"></span></p></blockquote><p></p>
[QUOTE="viraj_slk, post: 7402455, member: 51932"] [b]One possible solution to think of[/b] [SIZE="3"] Good to see that u have asked for the logic rather than a code. Here's one way I thought of to do it: first assign ur number to a variable as integer : x = 9087236781; then convert the same number to a string value. This is so that I can loop through each character. str = (string_cast) x; now str = '9087236781'; so..for example: str[0] is equal to 9; now use a loop like this to [B]keep adding the numbers together UNTIL the total is less than or equal to 9.[/B] [CODE] while (x > 9) { str = (string_cast) x; len = length(x); // the length of the number total = 0; for (i = 0; i < len; i++) { total += (integer_cast)str[i]; } x = total; } [/CODE] finally.. when the loop gets finished, 'x' will have a single digit value that is less than or equal to 9, and is also the summation of numbers in '9087236781' Let me know if u need a bit more help to clarify this one. [/SIZE] [/QUOTE]
Insert quotes…
Verification
Hath warak paha keeyada? (hatha wadikireema paha)
Post reply
Top
Bottom