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 6019467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:25:33+00:00 2026-05-23T03:25:33+00:00

int n; int[] ar = new int[50]; Console.Write(Enter the size of array= ); n

  • 0
int n;
        int[] ar = new int[50];
        Console.Write("Enter the size of array= ");
        n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            ar[i] = int.Parse(Console.ReadLine());
        }
        for (int i = 0; i < n; i++)
        {
            Console.WriteLine("AR["+i+"]="+ar[i]);
        }
        Console.Read();

enter image description here

As here you can see that when m entering the 09 or 08 its going to remove it and print the 9 and 8. And when same run on the c++ compiler then its print the 0 and 9 on different indices, why the compilers of both language doing behave like this? Why they do not reading it one digit?

  • 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-05-23T03:25:34+00:00Added an answer on May 23, 2026 at 3:25 am

    The behavior of int.Parse(..) and other string to numeric parsing functions is to strip out any leading characters (zeros in this case) which are irrelevant to the numeric value.

    If you wish to keep the leading zero then change the data type of your array to string.

    Now that you’ve posted some code, here’s a few suggestions (comments inline)

        int n;
        bool validNumber;
        do {
          validNumber = true;
          Console.Write("Enter the size of array:");
          if(!int.TryParse(Console.ReadLine(), out n)) // use TryParse instead of Parse to avoid formatting exceptions
          {
              Console.WriteLine("Not a number, please try again");
              validNumber = false;
          }
        } while(!validNumber);  // re-prompt if an invalid number has been entered
    
        int[] ar = new int[n];    // declare the array after the size is entered - your code will break if the user enters a number greater than 50
    
        // change to "string[] ar = new string[n];" if you want to keep the full value entered
    
        for (int i = 0; i < n; i++)
        {
            bool valid;
            do {
              valid = true;
              int num;
              string value = Console.ReadLine();
              if(int.TryParse(value, out num))  // again - use tryparse.
                ar[i] = num;  // change to "ar[i] = value;" if you're using a string array. 
              else {
                Console.WriteLine("Please enter that again - not a number");
                valid = false;
              }
           } while(!valid);
    
        }
        for (int i = 0; i < n; i++)
        {
            Console.WriteLine("AR["+i+"]="+ar[i]);
        }
        Console.Read();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Console.WriteLine(Please enter your number of the Names : ?); int x = Convert.ToInt32(Console.ReadLine()); string[]
Question is: Write a program that reads data into an array of type int.
I have an array of integers: int[] number = new int[] { 2,3,6,7 };
suppose I declare a dynamic array like int *dynArray = new int [1]; which
Consider the following example: int size = 10, *kk = new int[size]; for (int
Something like char[] a = new char[] { 'a', 'b', 'c', 'd' };<br> Console.WriteLine(a);
why this lock test doesn't work ? it's throwing an exception bellow Console.Write that
I'm trying to write a program that scans in values from the console and
int[] arrc = new int[] {1, 2, 3}; System.out.println(new ArrayList(Arrays.asList(arrc))); prints address, but i
Having code: int** a = new int*[2]; a[0] = new int(1); a[1] = new

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.