Jump to content

How to know if there is a number which start with number stored in variable N?

- - - - -

  • Please log in to reply
17 replies to this topic

#1
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
How to detect if a number in this case, "i" starts with any number in if statement.

Attached File  Untitled.png   4.28K   14 downloads

#2
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
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?
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/

#3
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
I want to know if variable (n) I create before starts with any number in if statement.

#4
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
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/

#5
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
I have this:


int n;

printf(".....");

scanf("%d", &n);


n=985742;


how to check if n starts with 9?





#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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.
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
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
Okay I got it! Thanks

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

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
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts

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
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I meant within a loop of course. :P
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#12
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

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