how can I convert C language to C#

lopez27 0 Reputation points
2023-03-11T02:52:37.2766667+00:00
#include <stdio.h>
int main() {

  int i, n;

  // initialize first and second terms
  int t1 = 0, t2 = 1;

  // initialize the next term (3rd term)
  int nextTerm = t1 + t2;

  // get no. of terms from user
  printf("Enter the number of terms: ");
  scanf("%d", &n);

  // print the first two terms t1 and t2
  printf("Fibonacci Series: %d, %d, ", t1, t2);

  // print 3rd to nth terms
  for (i = 3; i <= n; ++i) {
    printf("%d, ", nextTerm);
    t1 = t2;
    t2 = nextTerm;
    nextTerm = t1 + t2;
  }

  return 0;
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Mike Feng 26 Reputation points
    2023-03-11T07:20:07.7666667+00:00

    Take a look at this:

    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void main(string[] args)
            {
    
                int i, n;
    
                // initialize first and second terms
                int t1 = 0, t2 = 1;
    
                // initialize the next term (3rd term)
                int nextTerm = t1 + t2;
    
                // get no. of terms from user
                Console.WriteLine("Enter the number of terms: ");
                n = Convert.ToInt32(Console.ReadLine());
    
                // print the first two terms t1 and t2
                Console.Write($"Fibonacci Series: {t1}, {t2}, ");
    
                // print 3rd to nth terms
                for (i = 3; i <= n; ++i)
                {
                    Console.Write($"{nextTerm}, ");
                    t1 = t2;
                    t2 = nextTerm;
                    nextTerm = t1 + t2;
                }
            }
        }
    }
    
    0 comments No comments