Jump to content

can somebody help me translate this to c#?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
harddisk

harddisk

    Newbie

  • Members
  • PipPip
  • 12 posts
hi, i practice c# but my teacher give me this i cannot understand. can someone help me?:confused:

int main()
{
    int a,b,c,d=1,num;
    cout<<"Enter the no:"<<endd;
    cin>>num;

    for(c=num;c>=1;c--)
    {
        for(a=c;a>=1;a--)
            cout<<" ";
        if(d<num)
        {
            for(b=1;b<=d;b++)
            cout<<"*"<<" ";
        }
        d++;
        if(d!=num)
            cout<<endd;

    }

    d=1;
    for(c=num; c>=1; c--)
    {
        for(b=1; b<=d; b++)
        {
            cout<<" ";
        }
        if( d <= num)
        {
        for(a=c; a>=1; a--)
        cout << "*" << " ";
        }
        d++;
        cout<<endd;
    }
getch();
return 0;
}

Edited by Alexander, 20 November 2010 - 08:54 PM.


#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
What exactly don't you understand? There is nothing complicated in this code, but I don't have Visual Studio installed on my PC.

#3
swann_

swann_

    Newbie

  • Members
  • Pip
  • 7 posts
Here is C# version:


using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c, num;
            int d = 1; 

            Console.WriteLine("Enter the number: ");

            /*
               There are couple ways to convert input from the console to integer,
               you can use them all but in this example I think the best will be second method
            */

            /* 1. Parse --------------------------------------------------------------------------
               this will throw exception if user writes to console value of different type than integer
               if you can, try to avoid exceptions 
               -------------------------------------------------------------------------- */
            /*
            try
            {
                num = Int32.Parse(Console.ReadLine()); 
            }
            catch (FormatException)
            {
                Console.WriteLine("Error method 1, entered value must be of integer type !");
            }
            */

            /* 2. TryParse --------------------------------------------------------------------------
               This tests if writed value is in correct format, if not it will return false
               its cheaper and faster than using exceptions 
               -------------------------------------------------------------------------- */

            bool test = Int32.TryParse(Console.ReadLine(), out num); 
            if (!test)
                Console.WriteLine("Error method 2, entered value must be of integer type !");

            /* 3. Convert --------------------------------------------------------------------------
               in this example, like with Parse, if input value is not integer it will thto exception 
               -------------------------------------------------------------------------- */
            /*
            try
            {
                num = Convert.ToInt32(Console.ReadLine());
            }
            catch (FormatException)
            {
                Console.WriteLine("Error method 3, entered value must be of integer type !");
            }
            */
 

            for (c = num; c >= 1; c--)
            {
                for (a = c; a >= 1; a--)
                    Console.Write(" "); //cout<<" ";
                if (d < num)
                {
                    for (b = 1; b <= d; b++)
                        Console.Write("*" + " "); //cout<<"*"<<" ";
                }
                d++;
                if (d != num)
                    Console.WriteLine(); //cout << endl;
            }
            
            d = 1;
            for (c = num; c >= 1; c--)
            {
                for (b = 1; b <= d; b++)
                {
                    Console.Write(" "); //cout << " ";
                }
                if (d <= num)
                {
                    for (a = c; a >= 1; a--)
                        Console.Write("*" + " "); //cout << "*" << " ";
                }
                d++;
                Console.WriteLine(); //cout << endl;
            }

            Console.ReadLine();
        }
    }
}


#4
harddisk

harddisk

    Newbie

  • Members
  • PipPip
  • 12 posts

swann_ said:

Here is C# version:


using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c, num;
            int d = 1; 

            Console.WriteLine("Enter the number: ");

            /*
               There are couple ways to convert input from the console to integer,
               you can use them all but in this example I think the best will be second method
            */

            /* 1. Parse --------------------------------------------------------------------------
               this will throw exception if user writes to console value of different type than integer
               if you can, try to avoid exceptions 
               -------------------------------------------------------------------------- */
            /*
            try
            {
                num = Int32.Parse(Console.ReadLine()); 
            }
            catch (FormatException)
            {
                Console.WriteLine("Error method 1, entered value must be of integer type !");
            }
            */

            /* 2. TryParse --------------------------------------------------------------------------
               This tests if writed value is in correct format, if not it will return false
               its cheaper and faster than using exceptions 
               -------------------------------------------------------------------------- */

            bool test = Int32.TryParse(Console.ReadLine(), out num); 
            if (!test)
                Console.WriteLine("Error method 2, entered value must be of integer type !");

            /* 3. Convert --------------------------------------------------------------------------
               in this example, like with Parse, if input value is not integer it will thto exception 
               -------------------------------------------------------------------------- */
            /*
            try
            {
                num = Convert.ToInt32(Console.ReadLine());
            }
            catch (FormatException)
            {
                Console.WriteLine("Error method 3, entered value must be of integer type !");
            }
            */
 

            for (c = num; c >= 1; c--)
            {
                for (a = c; a >= 1; a--)
                    Console.Write(" "); //cout<<" ";
                if (d < num)
                {
                    for (b = 1; b <= d; b++)
                        Console.Write("*" + " "); //cout<<"*"<<" ";
                }
                d++;
                if (d != num)
                    Console.WriteLine(); //cout << endl;
            }
            
            d = 1;
            for (c = num; c >= 1; c--)
            {
                for (b = 1; b <= d; b++)
                {
                    Console.Write(" "); //cout << " ";
                }
                if (d <= num)
                {
                    for (a = c; a >= 1; a--)
                        Console.Write("*" + " "); //cout << "*" << " ";
                }
                d++;
                Console.WriteLine(); //cout << endl;
            }

            Console.ReadLine();
        }
    }
}

thanks, the 3rd method worked well for me. :thumbup:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users