Sunday, November 12, 2017

Lucky Four Codechef Problem Solution In (C++,C#)

https://www.codechef.com/problems/LUCKFOUR

In C++......

#include <bits/stdc++.h>

using namespace std;

int FourContaining(int a)
{
    int count=0;
    while(a!=0)
    {
        int n=a%10;
        a=a/10;
        if(n==4)
        {
            count++;
        }
    }
    return count;
}

int main()
{

    int t,a;
    cin>>t;
    while(t--)
    {
        cin>>a;
        cout<<FourContaining(a)<<endl;
    }

    return 0;
}


In C#............

using System;



    public class Program
    {
       public static long Counting(long a)
        {
            long count = 0;

            while (a != 0)
            {

                long n = a % 10;

                a = a / 10;

                if (n == 4)
                {

                    count++;

                }

            }

            return count;

        }
        public static void Main()

        {
            int T = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < T;i++ )
            {
                long n = Convert.ToInt64(Console.ReadLine());

                Console.WriteLine(Counting(n));
               }
            Console.ReadKey();
               
        }

    }
   

 

1 comment: