diff --git a/cpu_scheduling_algorithms/fcfs_scheduling.cpp b/cpu_scheduling_algorithms/fcfs_scheduling.cpp index 139a33683..5f49b3516 100644 --- a/cpu_scheduling_algorithms/fcfs_scheduling.cpp +++ b/cpu_scheduling_algorithms/fcfs_scheduling.cpp @@ -8,15 +8,15 @@ * @author [Pratyush Vatsa](https://github.com/Pratyush219) */ -#include /// for IO operations -#include /// for std::priority_queue -#include /// for std::unordered_set -#include /// for std::vector #include /// for sorting #include /// for assert #include /// random number generation #include /// for time #include /// for formatting the output +#include /// for IO operations +#include /// for std::priority_queue +#include /// for std::unordered_set +#include /// for std::vector using std::cin; using std::cout; @@ -271,7 +271,8 @@ void test() { readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]), get<2>(input[i])); } - vector> res = get_final_status(input); + vector> + res = get_final_status(input); assert(res == readyQueue.scheduleForFcfs()); // readyQueue.printResult(); } diff --git a/dynamic_programming/armstrong_number.cpp b/dynamic_programming/armstrong_number.cpp index 270a705f7..53f1be9fe 100644 --- a/dynamic_programming/armstrong_number.cpp +++ b/dynamic_programming/armstrong_number.cpp @@ -1,21 +1,39 @@ // Program to check whether a number is an armstrong number or not +#include #include - using std::cin; using std::cout; int main() { - int n, k, d, s = 0; - cout << "Enter a number:"; + int n = 0, temp = 0, rem = 0, count = 0, sum = 0; + cout << "Enter a number: "; cin >> n; - k = n; - while (k != 0) { - d = k % 10; - s += d * d * d; - k /= 10; + + temp = n; + + /* First Count the number of digits + in the given number */ + while (temp != 0) { + temp /= 10; + count++; } - if (s == n) - cout << n << "is an armstrong number"; - else - cout << n << "is not an armstrong number"; + + /* Calaculation for checking of armstrongs number i.e. + in a n digit number sum of the digits raised to a power of n + is equal to the original number */ + + temp = n; + while (temp != 0) { + rem = temp % 10; + sum += static_cast(pow(rem, count)); + temp /= 10; + } + + if (sum == n) { + cout << n << " is an armstrong number"; + } else { + cout << n << " is not an armstrong number"; + } + + return 0; }