Nikhil Sharma
Nikhil Sharma

Reputation: 2301

Why doesn't the output from the variables show on the Console using c#

/**
Write a program in C# language to perform the following operation:

b. Finding greatest of n numbers

**/
using System;
using System.Linq;


class Greatest
{
    public static void Main(String[] args)
    {   
        //get the number of elements
        Console.WriteLine("Enter the number of elements");
        int n;
        n=Convert.ToInt32(Console.ReadLine());
        int[] array1 = new int[n];         
        //accept the elements
        for(int i=0; i<n; i++)
        {
            Console.WriteLine("Enter element no:",i+1);
            array1[i]=Convert.ToInt32(Console.ReadLine());
        }
        //Greatest
        int max=array1.Max();
        Console.WriteLine("The max is ", max);

        Console.Read();
    }
}

The program doesn't output the variable's value, I can't figure out why ??

The sample output is

 Enter the number of elements
3
Enter element no:
2
Enter element no:
3
Enter element no:
2
The max is 

Note no output from variables.

Thanks

Upvotes: 2

Views: 151

Answers (3)

Cemafor
Cemafor

Reputation: 1653

You may also want this:

Console.WriteLine("Enter element no. {0}:", i+1);

Upvotes: 1

George Johnston
George Johnston

Reputation: 32258

You're missing your format item, e.g.

Change...

Console.WriteLine("The max is ", max);

to...

Console.WriteLine("The max is {0}", max);

Upvotes: 13

oz adari
oz adari

Reputation: 148

Console.WriteLine("The max is " + max);

will work.

Upvotes: 4

Related Questions