The following C++ program calculates the the number of digits in a given integer and the sum of all digits of an integer.
#include <iostream>
#include <cmath>
using namespace std;
/*@author Bishal Acharya
The idea of summing the numbers in an integer is to take
a division of the number from ones to final units of the number
and then take a modulo against 10*/
class SumOfInteger {
int number;
public :
int getSummation(int);
int numberOfDigits(int);
};
/*Gets the number of digits in an integer. Have to make
sure that we dont calculate log value of 0 */
int SumOfInteger::numberOfDigits(int number){
int digits = 0;
digits = floor(log10(abs (number != 0 ? number :1))) +1;
return digits;
}
/*Returns the sum of numbers in an integer*/
int SumOfInteger::getSummation(int number){
int noOfDigits;
noOfDigits = numberOfDigits(number);
int sum =0;
int power =0;
for(int i=0; i< noOfDigits; i++){
power = pow(10,i);
sum = sum + (number / power) % 10;
}
return sum;
}
int main(){
int number = 0;
cout << "Enter the number :";
cin >> number;
int sum =0;
SumOfInteger sumObj;
sum = sumObj.getSummation(number);
cout << "The sum of the integer numbers :" << sum << endl;
}
Output of the given program :-
Enter the number : 123
The sum of the integer numbers : 6
Alternative program to calculate number of digits in an number :-
#include<iostream>
using namespace std;
int main(){
int number = 0;
cout << "Enter the number" << endl;
cin >> number;
int count;
while(number !=0){
number = number/10;
count++;
}
cout << "The number of digits in the number is :" << count;
}
Program to reverse the digits in the given number. For eg :- if given number is 123 then the reversed value should be 321
#include
using namespace std;
int main(){
int number = 0;
cout <<"enter the number :"<< endl;
cin >> number;
int reverse = 0;
while(number !=0){
reverse = reverse*10 + number%10;
number = number%10;
}
cout <<"The reversed number is :" << reverse;
}
Benchmarks in Java program : Time it took to calculate number of digits in a number with log approach and with while loop approach.
public static void main(String[] args) {
int number = 0;
int temp = 0;
Random rnd = new Random();
int range = 1;
while (range < 10) {
number = rnd.nextInt();
temp = number;
System.out.print("Number :" + number + "TimeWhile :");
// approach with while loop
int count = 0;
long now = System.nanoTime();
while (number != 0) {
number = number / 10;
count++;
}
System.out.print((System.nanoTime() - now) / 1000);
now = System.nanoTime();
double count1 = (Math.log10(Math.abs(temp) == 0 ? 1 : temp)) + 1;
System.out.print(": Timelogarithmic :" + (System.nanoTime() - now)
/ 1000);
System.out.println("");
range++;
}
}
Output benchmark for 10 random numbers:-
Number :279590619TimeWhile :2: Timelogarithmic :40
Number :878851661TimeWhile :1: Timelogarithmic :3
Number :1617859989TimeWhile :1: Timelogarithmic :3
Number :195513660TimeWhile :1: Timelogarithmic :7
Number :2024673675TimeWhile :1: Timelogarithmic :3
Number :478104225TimeWhile :1: Timelogarithmic :3
Number :417156389TimeWhile :1: Timelogarithmic :3
Number :-104604112TimeWhile :1: Timelogarithmic :3
Number :-516925116TimeWhile :3: Timelogarithmic :4
The benchmark with Java program suggests that while loop approach of dividing the number by 10 each time gives a better response than using the inbuilt logarithmic and absolute functions.
See More :-
Game Of Pigs in C++
swapping two numbers without using the third one
No comments:
Post a Comment