Sunday, November 12, 2017

Sum of Digits Codechef Problem Solution In (C++,C#)

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

In c++...................

#include <bits/stdc++.h>

using namespace std;

int SumofDigits(int a)
{
    int sum=0;
    while(a!=0)
    {
        sum=sum+(a%10);
        a=a/10;
    }
    return sum;
}

int main()
{

    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        cout<<SumofDigits(n)<<endl;
    }

    return 0;

 }


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

using System;

    public class Program
    {
        public static long SumofDigits(long a)
        {
             long sum=0;
        while(a!=0)
        {
        sum=sum+(a%10);
        a=a/10;
        }
             return sum;
        }
        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(SumofDigits(n));

            }
               
        }

    }
   








1 comment: