Sum of digits of a number

Here given code implementation process.

//C Program
//Sum of digits of a number
#include <stdio.h>

// Method which are sum of given number 
// This method are add all digits and not take negative sign
void sum_digits(int number)
{
  int result = 0;
  if(number<0)
  {
    // Convert negative number to positive number
    number = - number;
  }
  while(number!=0)
  {
  
    // Adding last digit of a given number
    result+=number%10;
    
    // Remove last digit
    number/=10;
    
  }

  printf("%d\n",result );
}


int main()
{
  //Test Case
  sum_digits(123);
  sum_digits(562);

  //when passes negative number
  sum_digits(-562);

  return 0;
}

Output

6
13
13
/*
 C++ Program
 Sum of digits of a number
*/

#include<iostream>

using namespace std;

class MyNumber {
  public:

    // Method which are sum of given number 
    // This method are add all digits and not take negative sign
    void sum_digits(int number) {
      int result = 0;
      if (number < 0) {
        // Convert negative number to positive number
        number = -number;
      }
      while (number != 0) {
        // Adding last digit of a given number
        result += number % 10;
        // Remove last digit
        number /= 10;
      }
      cout << result << "\n";
    }
};
int main() {
  MyNumber obj ;
  //Test Case
  obj.sum_digits(123);
  obj.sum_digits(562);
  //When passes negative number
  obj.sum_digits(-562);
  return 0;
}

Output

6
13
13
/*
  Java Program
  Sum of digits of a number
*/

public class MyNumber {

  // Method which are sum of given number 
  // This method are add all digits and not take negative sign
  public void sum_digits(int number)
  {
    int result = 0;
   
    if(number<0)
    {
      // Convert negative number to positive number
      number = - number;
    }
    while(number!=0)
    {
    
      // Adding last digit of a given number
      result+=number%10;
      
      // Remove last digit
      number=number/10;
      
    }

    System.out.print(result+"\n" );
  }
  public static void main(String[] args) {
    
    MyNumber obj = new MyNumber();
    //Test Case
    obj.sum_digits(123);
    obj.sum_digits(562);

    //When passes negative number
    obj.sum_digits(-562);

  }


}

Output

6
13
13
/*
  C# Program
  Sum of digits of a number
*/
using System;
public class MyNumber {

	// Method which are sum of given number 
	// This method are add all digits and not take negative sign
	public void sum_digits(int number) {
		int result = 0;

		if (number < 0) {
			// Convert negative number to positive number
			number = -number;
		}
		while (number != 0) {

			// Adding last digit of a given number
			result += number % 10;

			// Remove last digit
			number = number / 10;

		}

		Console.Write(result + "\n");
	}
	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test Case
		obj.sum_digits(123);
		obj.sum_digits(562);

		//When passes negative number
		obj.sum_digits(-562);

	}


}

Output

6
13
13
# Python 3 Program
# Sum of digits of a number
class MyNumber :
  # This method are add all digits and not take negative sign

  # Method which are sum of given number 
  def sum_digits(self, number) :
    result = 0
    if (number < 0) :
      # Convert negative number to positive number
      number = -number
    
    while (number != 0) :
      # Adding last digit of a given number
      result += number % 10
      # Remove last digit
      number = int(number / 10)
    
    print(result)
  

def main() :
  obj = MyNumber()
  #Test Case
  obj.sum_digits(123)
  obj.sum_digits(562)
  #When passes negative number
  obj.sum_digits(-562)


if __name__ == "__main__":
  main()

Output

6
13
13
# Ruby Program 

# Sum of digits of a number
class MyNumber 
	# This method are add all digits and not take negative sign

	# Method which are sum of given number 
	def sum_digits(number) 
		result = 0
		if (number < 0) 
			# Convert negative number to positive number
			number = -number
		end
		while (number != 0) 
			# Adding last digit of a given number
			result += number % 10
			# Remove last digit
			number = number / 10
		end
		print(result ,"\n")
	end
end
def main() 
	obj = MyNumber.new()
	#Test Case
	obj.sum_digits(123)
	obj.sum_digits(562)
	#When passes negative number
	obj.sum_digits(-562)
end
main()

Output

6
13
13
/*
 Scala Program
 Sum of digits of a number
*/
class MyNumber {
	// Method which are sum of given number 
	// This method are add all digits and not take negative sign
	def sum_digits(value: Int): Unit = {
		var result: Int = 0;
        var number: Int = value;
		if (number < 0) {
			// Convert negative number to positive number
			number = -number;
		}
		while (number != 0) {
			// Adding last digit of a given number
			result += number % 10;
			// Remove last digit
			number = number / 10;
		}
		print(result);
        print("\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.sum_digits(123);obj.sum_digits(562);
		//When passes negative number
		obj.sum_digits(-562);
	}
}

Output

6
13
13
/*
  Swift 4 Program
  Sum of digits of a number
*/
class MyNumber {
	// Method which are sum of given number 
	// This method are add all digits and not take negative sign
	func sum_digits(_ value: Int) {
		var result: Int = 0;
      	var number: Int = value;
		if (number < 0) {
			// Convert negative number to positive number
			number = -number;
		}
		while (number != 0) {
			// Adding last digit of a given number
			result += number % 10;
			// Remove last digit
			number = number / 10;
		}
		print(result);
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.sum_digits(123);
	obj.sum_digits(562);
	//When passes negative number
	obj.sum_digits(-562);
}
main();

Output

6
13
13
<?php
/*
  Php Program
  Sum of digits of a number
*/
class MyNumber {
	// Method which are sum of given number 
	// This method are add all digits and not take negative sign

	public 	function sum_digits($number) {
		$result = 0;
		if ($number < 0) {
			// Convert negative number to positive number
			$number = -$number;
		}
		while ($number != 0) {
			// Adding last digit of a given number
			$result += $number % 10;
			// Remove last digit
			$number = intval($number / 10);
		}
		echo ($result."\n");
	}
}
function main() {
	$obj = new MyNumber();
	//Test Case

	$obj->sum_digits(123);
	$obj->sum_digits(562);
	//When passes negative number

	$obj->sum_digits(-562);
}
main();
?>

Output

6
13
13
/*
 Node Js Program
 Sum of digits of a number
*/
class MyNumber {
	// Method which are sum of given number 
	// This method are add all digits and not take negative sign
	sum_digits(number) {
		var result = 0;
		if (number < 0) {
			// Convert negative number to positive number
			number = -number;
		}
		while (number != 0) {
			// Adding last digit of a given number
			result += number % 10;
			// Remove last digit
			number = parseInt(number / 10);
		}
		process.stdout.write(result + "\n");
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Case
	obj.sum_digits(123);
	obj.sum_digits(562);
	//When passes negative number
	obj.sum_digits(-562)
}
main();

Output

6
13
13


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved