Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:21:41+00:00 2026-06-01T04:21:41+00:00

I need to limit the input from a user to only positive values, and

  • 0

I need to limit the input from a user to only positive values, and count the number of digits in that number. The user will only type in a (+/-) whole number up to 9 characters long.

I’m only allowed to use the scanf function and for, while, or do-while loops.(I saw in similar questions how to do this using getchar, but I can only use scanf). I’m not allowed to use arrays, or any other library besides stdio.h and math.h

I know that if I write:

n=scanf(“%c%c%c%c%c”,&a,&b,&c,&e,&f);

n will count the number of successful scanf conversions.

The problem i’m having is that when I define the input with char, it does everything I want except that the user MUST enter 5 characters. So if the user wants to input “55” he has to press “5” “5” “enter” “enter” “enter”.

I need the program to move on after the first “enter” but also be flexible to receive a number up to 9 digits long.

again, I can’t use getchar or anything fancy. Just the really basic stuff in C that you learn in the first 2 weeks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:21:42+00:00Added an answer on June 1, 2026 at 4:21 am

    Use scanf to read the number into a long int , then use a for loop with a /10 to count the number of digits

    What do you want the program to do in case of a -ve number being entered?

    #include<stdio.h>
    int main()
    {
    
        long int a;
        int b;
        do
        {
        scanf ("%ld",&a);
        if(a<0)
            printf ("invalid input");
        }while(a<0);
    
        for(b=0;a!=0;b++,a=a/10);
        printf("%d",b);
    }
    

    (does not handle -ve numbers specially)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help forming regex to limit user input to only numerics and only
Possible Duplicate: Limit file format when using <input type=file>? I need to use the
I need to limit the number of characters that can be appended to an
I have a PHP page I need to limit execution access of to only
i need a timer that works like this : imagine somebody have a limit
i need to know if exist a jquery's plugin to limit the number of
I've an upcoming project where I will need to handle very large uploads from
I have a textbox that the user can input into. Right now the user
I need a little utility function that generates a checksum from an array of
I need to take an input from a WYSIWYG and break it down to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.