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:
Friday 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:
Jun 28, 2026
Ad icon
QA Engineer Intern
pramukag
Updated:
Jun 28, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Hey guys guys... i developed my first c program. it is simple.. please check it !!
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="Ranhiru" data-source="post: 3391414" data-attributes="member: 17748"><p>Got this from a website...i didnt write the code though ill explain it <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /> This is for validation if the entered numbers are actually digits and not any char values <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /></p><p>This is pretty much tough i guess for a beginner...ill try to explain line by line</p><p></p><p><span style="color: Blue">//First take input using a string</span></p><p><span style="color: Blue"></span><span style="color: black">printf("Enter a number :- ");</span></p><p><span style="color: black"></span><span style="color: black"> gets(str);</span></p><p><span style="color: Blue"> </span></p><p><span style="color: Blue">//Create variable's named flag and len</span></p><p>int flag,len;</p><p></p><p><span style="color: Blue">//Assign the value 0 to flag</span></p><p>flag=0;</p><p><span style="color: Blue"></span></p><p><span style="color: Blue">//Let len take the value of how much characters are there in the string</span></p><p><span style="color: Blue">//strlen is a function defined in string.h so dont forget to include it too <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /></span></p><p></p><p>len = strlen(str); /* str is the number in string format*/</p><p></p><p><span style="color: Blue">// Now loop len times in the string (char array actually <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /> )</span></p><p></p><p>for(int i=0;i<len;i++)</p><p> {</p><p> if(!isdigit(str<em>))</em></p><p><em> flag=1;</em></p><p><em> <span style="color: Blue">//isdigit is a function in ctype.h which will return whether the character is a digit or not...so you do it for all the letters in thearray </span> <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /></em></p><p> <em>}</em></p><p><em></em></p><p><em>if(flag==0)</em></p><p><em> <span style="color: Blue">/* the string contains only digits*/</span></em></p><p><em>else</em></p><p><em> <span style="color: Blue">/* it contains other characters*/</span></em></p><p><em>..</em></p><p><em>..</em></p><p><em>..</em></p><p><em><span style="color: Blue"></span></em></p><p><em><span style="color: Blue">isdigit is defined in "ctype.h"..</span></em></p><p><em><span style="color: Blue"></span></em></p><p><em><span style="color: Blue">in case of any problem .. u can write it on your own as ..</span></em></p><p><em><span style="color: Blue"></span></em></p><p><em><span style="color: Blue"><span style="color: Black">int isdigit(char c)</span></span></em></p><p><em><span style="color: Blue"><span style="color: Black">{</span></span></em></p><p><em><span style="color: Blue"><span style="color: Black"> if(c < '0' or c > '9') </span></span></em></p><p><em><span style="color: Blue"><span style="color: Black">return 0;</span></span></em></p><p><em><span style="color: Blue"><span style="color: Black"> else</span></span></em></p><p><em><span style="color: Blue"><span style="color: Black">return 1;</span></span></em></p><p><em><span style="color: Blue"><span style="color: Black">}</span></span> <span style="color: Black"></span></em></p><p><em><span style="color: Black"></span> </em></p><p><em></em></p><p><em>This is the full code that i've come up with</em></p><p><em></em></p><p><em>[code]</em></p><p><em>#include <stdio.h></em></p><p><em>#include <string.h></em></p><p><em>#include <ctype.h></em></p><p><em></em></p><p><em></em></p><p><em>void main()</em></p><p><em>{</em></p><p><em>char str[10];</em></p><p><em>int len=0,flag=0;</em></p><p><em></em></p><p><em>printf("Enter a number :- ");</em></p><p><em>gets(str);</em></p><p><em></em></p><p><em>len = strlen(str);</em></p><p><em></em></p><p><em>for(int i=0;i<len;i++)</em></p><p> <em>{</em></p><p><em> if(!isdigit(str[i]))</em></p><p><em> flag=1;</em></p><p> <em>}</em></p><p><em></em></p><p> <em>if (flag==0)</em></p><p> <em>printf("Only contains digits!");</em></p><p> <em>else</em></p><p> <em>printf("Contains non-digits");</em></p><p><em></em></p><p><em></em></p><p><em>}</em></p><p><em></em></p><p><em>[/code] </em></p><p><em>Please ask if anything seems weird ... and if you dont understand any of this...dont worry even a bit! its your first day!!! bookmark this page and after about 2 or 3 weeks..after you have done loops and arrays just come and take a look! you will understand everything! <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /> hope this helps and did not confuse you</em></p><p><em></em></p><p><em>gud luck! <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /></em></p></blockquote><p></p>
[QUOTE="Ranhiru, post: 3391414, member: 17748"] Got this from a website...i didnt write the code though ill explain it :) This is for validation if the entered numbers are actually digits and not any char values :) This is pretty much tough i guess for a beginner...ill try to explain line by line [COLOR=Blue]//First take input using a string [/COLOR][COLOR=black]printf("Enter a number :- "); [/COLOR][COLOR=black] gets(str);[/COLOR] [COLOR=Blue] //Create variable's named flag and len[/COLOR] int flag,len; [COLOR=Blue]//Assign the value 0 to flag[/COLOR] flag=0; [COLOR=Blue] //Let len take the value of how much characters are there in the string //strlen is a function defined in string.h so dont forget to include it too :)[/COLOR] len = strlen(str); /* str is the number in string format*/ [COLOR=Blue]// Now loop len times in the string (char array actually :) )[/COLOR] for(int i=0;i<len;i++) { if(!isdigit(str[i])) flag=1; [COLOR=Blue]//isdigit is a function in ctype.h which will return whether the character is a digit or not...so you do it for all the letters in thearray [/COLOR] :) } if(flag==0) [COLOR=Blue]/* the string contains only digits*/[/COLOR] else [COLOR=Blue]/* it contains other characters*/[/COLOR] .. .. .. [COLOR=Blue] isdigit is defined in "ctype.h".. in case of any problem .. u can write it on your own as .. [COLOR=Black]int isdigit(char c) { if(c < '0' or c > '9') return 0; else return 1; }[/COLOR][/COLOR] [COLOR=Black] [/COLOR] This is the full code that i've come up with [code] #include <stdio.h> #include <string.h> #include <ctype.h> void main() { char str[10]; int len=0,flag=0; printf("Enter a number :- "); gets(str); len = strlen(str); for(int i=0;i<len;i++) { if(!isdigit(str[i])) flag=1; } if (flag==0) printf("Only contains digits!"); else printf("Contains non-digits"); } [/code] Please ask if anything seems weird ... and if you dont understand any of this...dont worry even a bit! its your first day!!! bookmark this page and after about 2 or 3 weeks..after you have done loops and arrays just come and take a look! you will understand everything! :) hope this helps and did not confuse you gud luck! :)[/i] [/QUOTE]
Insert quotes…
Verification
Winadiyakata thappara keeyak tibeda?
Post reply
Top
Bottom