Untitled.png 4.28K
14 downloads
How to know if there is a number which start with number stored in variable N?
Started by carbon, Jan 05 2012 04:33 AM
17 replies to this topic
#1
Posted 05 January 2012 - 04:33 AM
How to detect if a number in this case, "i" starts with any number in if statement.
Untitled.png 4.28K
14 downloads
Untitled.png 4.28K
14 downloads
|
|
|
#2
Posted 05 January 2012 - 04:45 AM
Im not sure about what you mean with this question, You want to find out if the variable i that you create in the for loop is equal to any of the numbers in the if statement?
Make a line of code to print the value of i to the console.. Thats a way of knowing if its equal to the number you want.. The if statement could use some code to execute and if it executes you will know if i went to the number you wanted.
Does this help?
Make a line of code to print the value of i to the console.. Thats a way of knowing if its equal to the number you want.. The if statement could use some code to execute and if it executes you will know if i went to the number you wanted.
Does this help?
Think my post we're usefull? Please take your time and press the Like button at my post, Big Thanks!
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
#3
Posted 05 January 2012 - 04:49 AM
I want to know if variable (n) I create before starts with any number in if statement.
#4
Posted 05 January 2012 - 05:06 AM
If you want to check the value of a variable outside the loop make an if statement that check for n.. I realy don't understand this..
Think my post we're usefull? Please take your time and press the Like button at my post, Big Thanks!
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
#5
Posted 05 January 2012 - 05:15 AM
I have this:
int n;
printf(".....");
scanf("%d", &n);
n=985742;
how to check if n starts with 9?
#6
Posted 05 January 2012 - 06:39 AM
The easiest way would be to scan it as a sequence of characters [string] instead of an int and look to see if the first character == '9'.
You can still verify that it is a valid int by either rescanning it as an int using sscanf() or by checking each character with isdigit() in ctype.h.
You can still verify that it is a valid int by either rescanning it as an int using sscanf() or by checking each character with isdigit() in ctype.h.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#7
Posted 05 January 2012 - 06:47 AM
Okay I got it! Thanks
#8
Posted 05 January 2012 - 10:25 AM
You could also divide n by 10 and print mod 10.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#9
Posted 05 January 2012 - 11:07 AM
Flying Dutchman said:
You could also divide n by 10 and print mod 10.
This would give least significant digit. I think the OP was needing most significant digit. I assumed it was for an integer with an arbitrary number of digits, hence why I suggested scanning as a string.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#10
Posted 05 January 2012 - 11:10 AM
Flying Dutchman said:
You could also divide n by 10 and print mod 10.
I need a first number, but with mod I cant make this.
#11
Posted 05 January 2012 - 12:15 PM
I meant within a loop of course. :P
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#12
Posted 05 January 2012 - 12:35 PM
Flying Dutchman said:
I meant within a loop of course. :P
I guessed that's what you meant, though, I think scanning characters from the front would be more computationally efficient, since it's the first character he's interested in.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









