C# program to find the magnitude of an integer number

Given an integer number and we have to find its magnitude using C# program.
Submitted by Nidhi, on August 18, 2020

Here we will find the magnitude of an integer number, here magnitude of a number specify the length of number, for example:

Number= 543623; Then the magnitude of the number is 6.

Program:

The source code to find the magnitude of an integer number in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

// Write a program to find the 
// magnitude of an integer number in C#.

using System;

public class MagnitudeDemo
{
    public static int GetMagnitude(int num)
    { 
        int magnitude=0;

        while(num>0)
        {
            magnitude++;
            num = num/10;
        }
        return magnitude;
    }
    public static void Main()
    {
        int num = 34521;
        int mag = 0;

        mag = GetMagnitude(num);

        Console.WriteLine("Magnitude: " + mag);
    }
}

Output:

Magnitude: 5
Press any key to continue . . .

Explanation:

In the above program, we created a class MagnitudeDemo that contains GetMagnitude() and Main() methods. The GetMagnitude() method returns the magnitude of the specified number.

Here we divide the number by 10 till it becomes 0. In every iteration of while loop we increase the value of magnitude variable by 1 and reduce the number num by 1 digit in length.

In the Main() method, we created an integer variable num initialized with 34521 and then pass the variable in the static method GetMagnitude() and get the magnitude in local variable mag and then printed the value of mag on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.