Softlogic Systems - Placement and Training Institute in Chennai

Easy way to IT Job

Share on your Social Media

C Sharp Coding Challenges and Solutions for Beginners

Published On: November 2, 2024

Introduction

Although C# syntax is very expressive, it is also straightforward to master. Developers who are familiar with any of these languages may usually start working effectively in C# fairly quickly. Here are frequently used C# coding challenges and solutions for beginners. Unveil our C# coding course syllabus to get started. 

C Sharp Coding Challenges and Solutions

You can practice with these four C# coding tasks. They are an excellent method to improve your coding abilities. Every challenge begins with a description of the task and the anticipated outcomes. These coding challenges are designed for beginners. 

Sum the Odd Numbers

Challenge: To add up all of the odd integers in a collection, use an algorithm. The following tests should be passed by your solution:

new App().SumOdds(new int[]{1, 2, 3, 4, 5}) -> 9

new App().SumOdds(new int[]{1, 2, 3}) -> 4

new App().SumOdds(new int[]{2, 2}) -> 0

new App().SumOdds(new int[]{1, 1}) -> 2

That one ought to be quite simple. All you have to do is determine if each number in the collection is odd or even. Add its value to the total if it is odd.

Solution: Here is the code: 

using System;

namespace DotNetTutorials

{

    class SumOfEvenNumbers

    {

        static void Main(string[] args)

        {

            int sum = 0;

            Console.Write(“Enter value a Number:”);

            int Number = Convert.ToInt32(Console.ReadLine());

            //start with 1

            //1+2 = 3

            //3+2 = 5

            for (int i = 1; i <= Number; i += 2)

            {

                sum += i;

            }

            Console.Write($”Sum of Odd numbers from 1 to {Number} is : {sum}”);

            Console.ReadLine();

        }

    }

}

Output:

Learn C# comprehensively through our C# tutorial for beginners

Finding Factorial Number

Challenge: A mathematical operation applied to a non-negative integer is called a factorial. The product of every positive integer between 1 and “n,” represented as “n!,” is the factorial of a non-negative integer “n.” 

Method: n! = n × (n – 1) × (n – 2) × … × 2 × 1

Example:

5! = 5 × 4 × 3 × 2 × 1 = 120

0! is defined to be 1

Check out the Factorial() function’s tests. The expected outcomes: 

new App().Factorial(0) -> 1

new App().Factorial(1) -> 1

new App().Factorial(2) -> 2

new App().Factorial(3) -> 6

Solution: Here is the original code as well. All you have to do is complete the Factorial() function’s implementation: 

using System;  

  public class FactorialExample  

   {  

     public static void Main(string[] args)  

      {  

       int i,fact=1,number;      

       Console.Write(“Enter any Number: “);      

       number= int.Parse(Console.ReadLine());     

       for(i=1;i<=number;i++){      

        fact=fact*i;      

       }      

       Console.Write(“Factorial of ” +number+” is: “+fact);    

     }  

  }  

Output

Enter any Number: 6

Factorial of 6 is: 720

Learn practically with our C# project ideas.

Convert Days to Hours

Challenge: Converting days into hours is your current task. Hours = d * 24 is the basic formula. 

The following tests should be passed by your function: 

new App().ToDays(24) -> 1

new App().ToDays(6) -> 0,25

new App().ToDays(12) -> 0,5

new App().ToDays(48) -> 2

Solution: Code for Converting Days to Hours

using System;

class MainClass {

  public static void Main (string[] args) {

    Console.WriteLine (“Please enter days:”);

    int days = Convert.ToInt32(Console.ReadLine());

    int hours = days * 24;

    Console.WriteLine(hours + ” Hours”);

  }

}

Output

Please enter days: 3

72 Hours

Review your skills with our C# interview questions and answers

Word Count in a Text String

Challenge: The final one involves counting the words in a specified text string. First, let’s talk about tests: 

new App().CountWords(null) -> 0

new App().CountWords(“Hello”) -> 1

new App().CountWords(“Hello SLA”) -> 2

new App().CountWords(“Hello SLA !!!”) -> 3

Solution Code: 

        public static void numberofwordsinstring()

        {

            Console.Write(“Enter the string : “);

            string inputString = Console.ReadLine();

            int result = 0;

            inputString = inputString.Trim();

            if (inputString == “”)

                Console.WriteLine(0);

            while (inputString.Contains(”  “))

               inputString = inputString.Replace(”  “, ” “);

            foreach (string y in inputString.Split(‘ ‘))

                result++;

            Console.WriteLine(“Number of words is : ” +result);

            Console.ReadLine();

        }

You can check out our C# online course if you want to learn remotely.

Conclusion

We hope this article helps you practice some of the common C# coding challenges and solutions. Hone your skills with our C-Sharp training in Chennai.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.