
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program for Find sum of odd factors of a number
Given with a positive integer and the task is to generate the odd factors of a number and finding out the sum of given odd factors.
Example
Input-: number = 20 Output-: sum of odd factors is: 6 Input-: number = 18 Output-: sum of odd factors is: 13
So, result = 1 + 5 = 6
Approach used in the below program is as follows −
- Input the number for calculating the sum of odd factors of that number
- Ignore the digit 0 and 2 because both are even digits and store the digit 1 because it is an odd digit
- Start the loop from 3 till square root of a number
- Traverse till number % i is returning 0 and keep dividing the number with the value of i
- In the loop keep setting the value of temporary variable to temp = temp * i
- Set the total to total + temp
- Return the value of final res variable and print the result
Algorithm
START Step 1-> Declare function to calculate sum of odd factors int sum(int num) declare int res = 1 Loop While(num % 2 == 0) set num = num / 2 End Loop For int i = 3 and i <= sqrt(num) and i++ declare int count = 0 and total = 1 declare int temp = 1 Loop while (num % i == 0) count++ set num = num / i set temp *= i set total += temp End set res = res*total End IF (num >= 2) set res *= (1 + num) End return res Step 2-> In main() Declare int num = 20 call sum(num) STOP
Example
#include <bits/stdc++.h> using namespace std; //calculate sum of odd factors int sum(int num) { int res = 1; while (num % 2 == 0) num = num / 2; for (int i = 3; i <= sqrt(num); i++) { int count = 0, total = 1 ; int temp = 1; while (num % i == 0) { count++; num = num / i; temp *= i; total += temp; } res = res*total; } if (num >= 2) res *= (1 + num); return res; } int main() { int num = 20; cout<<"sum of odd factors is : "; cout <<sum(num); return 0; }
Output
sum of odd factors is : 6
- Related Articles
- Python Program for Find sum of odd factors of a number
- C Program for Find sum of odd factors of a number?
- Find sum of odd factors of a number using C++.
- Python Program for Find sum of even factors of a number
- Python Program for Find minimum sum of factors of number
- C++ Program to find sum of even factors of a number?
- Find sum of even factors of a number in Python Program
- Java Program to find minimum sum of factors of a number
- Java Program to Find sum of even factors of a number
- To find sum of even factors of a number in C++ Program?
- C Program to Find the minimum sum of factors of a number?
- Python Program for Number of elements with odd factors in the given range
- Queries on sum of odd number digit sums of all the factors of a number in C++
- Find sum of even factors of a number using C++.
- Find minimum sum of factors of number using C++.

Advertisements