Q:

Write a C# Sharp program to check a positive integer and return true if it contains a number 2

0

Write a C# Sharp program to check a positive integer and return true if it contains a number 2

Sample Output:

True
False
True

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {               
             Console.WriteLine(test(123)); 
             Console.WriteLine(test(13)); 
             Console.WriteLine(test(222)); 
        }                  
        public static bool test(int n)
           {
             while (n > 0)
               {
                if (n % 10 == 2) return true;
                n /= 10;
               }
            return false;
        }    
    } 
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now