Search This Blog

Makingcodesimple.

Simple solutions with explanation for the problems on competitive sites like hackerearth and many many more things like how to make your code faster, shorter and simple only at makingcodesimple.blogspot.com

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

Problem 1: sum of two digits, solution: (in c++), ( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder).

This code is simple. There's no need for any explanation.  

Problem 2: Maximum Pairwise Product

Guys , if you have any queries related to a question or need more explanation, comment down below!  

Post a Comment

Popular posts from this blog, hackerearth's basic programming solutions( seating arrangement, zoos, anagrams ) :, hackerearth's basic programming solutions( minimize cost, magical word, best index ) :.

Codeforwin

C program to find sum of digits of a number

Write a C program to input a number and calculate sum of digits using for loop. How to find sum of digits of a number in C program. Logic to find sum of digits of a given number in C programming.

Required knowledge

Basic C programming , While loop

Logic to find sum of digits of a number

The main idea to find sum of digits can be divided in three steps.

  • Extract last digit of the given number .
  • Add the extracted last digit to sum.
  • Remove last digit from given number. As it is processed and not required any more.

If you repeat above three steps till the number becomes 0 . Finally you will be left with sum of digits.

Step by step descriptive logic to find sum of digits of a given number.

  • Input a number from user. Store it in some variable say num .
  • Find last digit of the number. To get last digit modulo division the number by 10 i.e. lastDigit = num % 10 .
  • Add last digit found above to sum i.e. sum = sum + lastDigit .
  • Remove last digit from number by dividing the number by 10 i.e. num = num / 10 .
  • Repeat step 2-4 till number becomes 0 . Finally you will be left with the sum of digits in sum .

Program to find sum of digits of a number

In the above program I have used shorthand assignment operator sum += num % 10; which is equivalent to sum = sum + (num % 10); .

Happy coding 😉

Recommended posts

  • Loop programming exercises index .
  • C program to count number of digits in any number .
  • C program to find first and last digit of any number .
  • C program to swap first and last digit of any number .
  • C program to find product of digits of a given number .
  • C program to find reverse of any number .

Python Examples

  • Online Python Compiler
  • Hello World
  • Console Operations
  • Conditional Statements
  • Loop Statements
  • Builtin Functions
  • Type Conversion

Collections

  • Classes and Objects
  • File Operations
  • Global Variables
  • Regular Expressions
  • Multi-threading
  • phonenumbers
  • Breadcrumbs
  • ► Python Examples
  • ► ► Python Programs
  • ► ► ► Python – Sum of Two Numbers
  • Python Programs
  • Python Basic Programs
  • Python Sorting Programs
  • Basic Programs
  • Python - Add two numbers
  • Python - Arrange given numbers to form a maximum number
  • Python - Arrange given numbers to form a minimum number
  • Python - Average of two numbers
  • Python - Calculator program
  • Python - Check if number is Armstrong
  • Python - Check if number is even
  • Python - Check if number is odd
  • Python - Check if number is prime number
  • Python - Check if number is positive or negative
  • Python - Check if two numbers are equal
  • Python - Fibonacci series using For-Loop
  • Python - Fibonacci series using recursion
  • Python - Find all factors of a number
  • Python - Find area of circle
  • Python - Find area of rectangle
  • Python - Find area of square
  • Python - Find factorial of a number
  • Python - Find factorial of a number using recursion
  • Python - Find if given year is Leap Year in Georgian calendar
  • Python - Find maximum of two numbers
  • Python - Find minimum of two numbers
  • Python - Find Nth Fibonacci number
  • Python - Find roots of a quadratic equation
  • Python - Get unique digits in a number
  • Python - Largest of three numbers
  • Python - Matrix Transpose
  • Python - Matrix Addition
  • Python - Palindrome program
  • Python - Pangram string
  • Python - Print from A to Z
  • Python - Print numbers 123..N
  • Python - Print prime numbers between two given numbers
  • Python - Reverse a number
  • Python - Round a floating number to specific decimal places
  • Python - Round a floating number to nearest integer
  • Python - Smallest of three numbers
  • Python - Star pattern programs
  • Python - Sum of two numbers
  • Python - Sum of first N natural numbers
  • Python - Sum of cubes of first N natural numbers
  • Python - Sum of squares of first N natural numbers
  • Python - Swap two numbers
  • Python - Tower of Hanoi
  • Sorting Programs
  • Python Bubble Sort Program
  • Python Selection Sort Program
  • Python Insertion Sort Program
  • Python Merge Sort Program
  • Python Quick Sort Program
  • Python Heap Sort Program
  • Character related programs
  • Python - Get ASCII value of a character
  • Python - Check if given character is vowel or consonant
  • Python - Check if a given character is alphabet or not
  • Other Programs
  • Get Python version

Python – Sum of Two Numbers

  • Sum of Two Integers
  • Sum of two Floating Point Numbers

Python Program to Find Sum of Two Numbers

To find sum of two numbers in Python, you can use Arithmetic Addition Operator + .

+ operator takes two operands and returns the sum of the two numbers.

1. Sum of Two Integers

In this example, we shall take two integers and find their sum. Following are the steps we shall implement.

  • Take two numbers in n1 , n2 .
  • Compute sum of the two numbers.

Python Program

2. Sum of two Floating Point Numbers

You can use the same steps to find the sum of two floating point numbers. In this example, we shall read two floating point numbers and find their sum.

In this tutorial, we learned how to find sum of two numbers in Python using Addition operator, with the help of example programs.

Related Tutorials

Learn C practically and Get Certified .

Popular Tutorials

Popular examples, reference materials, learn c interactively.

  • Swap Two Numbers
  • Find the Size of int, float, double and char
  • Compute Quotient and Remainder
  • Find ASCII Value of a Character
  • Multiply Two Floating-Point Numbers
  • Add Two Integers
  • Print an Integer (Entered by the User)
  • C "Hello, World!" Program
  • C if...else Statement
  • C Input Output (I/O)

Find Largest Element in an Array

Find GCD of two Numbers

Find Largest Number Using Dynamic Memory Allocation

Store Information of Students Using Structure

C Program to Add Two Integers

To understand this example, you should have the knowledge of the following C programming topics:

  • C Data Types
  • C Variables, Constants and Literals
  • C Programming Operators

Program to Add Two Integers

In this program, the user is asked to enter two integers. These two integers are stored in variables number1 and number2 respectively.

Then, these two numbers are added using the + operator, and the result is stored in the sum variable.

Adding two integers in C programming

Finally, the printf() function is used to display the sum of numbers.

Sorry about that.

Related Examples

Assignments  »  Variable, Operator and Expression  »  Set 1  »  Solution 2

Write a program that prompts the user to enter two integers and display their sum on the screen.

Source Code

Enter the first integer: 10 Enter the second integer: 5 The sum of 10 and 5 is 15

  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers

Related Articles

  • Solve Coding Problems
  • Program to convert a given number to words
  • Add two numbers represented by linked lists | Set 2
  • Print all possible words from phone digits
  • Find Excel column name from a given column number
  • Find a pair with maximum product in array of Integers
  • A product array puzzle | Set 2 (O(1) Space)
  • Find an element in array such that sum of left array is equal to sum of right array
  • Count distinct elements in every window of size k
  • Longest Palindromic Substring using Dynamic Programming
  • Add two numbers represented by two arrays
  • Find minimum number of merge operations to make an array palindrome
  • Find the subarray with least average
  • Find zeroes to be flipped so that number of consecutive 1's is maximized
  • Find an equal point in a string of brackets
  • Difference of two large numbers
  • Remove characters from the first string which are present in the second string
  • Extract maximum numeric value from a given string | Set 1 (General approach)
  • C Program For Adding Two Numbers Represented By Linked Lists- Set 2
  • C++ Program For Adding Two Numbers Represented By Linked Lists- Set 2

Sum of two large numbers

Given two numbers as strings. The numbers may be very large (may not fit in long long int), the task is to find sum of these two numbers.

Examples:  

Approach 1: Using BigInteger class

The simplest approach is use BigInteger class. BigInteger provides analogues to all of Java’s primitive integer operators, and all relevant methods from java.lang.Math. In this approach we will create 2 objects of BigInteger and pass string in it then add simply add those objects using add() method.

Time Complexity: O(1) Auxiliary Space: O(1)

Approach 2: Iterative approach

The idea is based on school mathematics. We traverse both strings from end, one by one add digits and keep track of carry. To simplify the process, we do following:  1) Reverse both strings.  2) Keep adding digits one by one from 0’th index (in reversed strings) to end of smaller string, append the sum % 10 to end of result and keep track of carry as sum/10.  3) Finally reverse the result. 

Output:  

Time Complexity: O(n1+n2) where n1 and n2 are lengths of two input strings representing numbers.

Auxiliary Space: O(max(n1, n2))

Approach 3 : Optimization

We can avoid the first two string reverse operations by traversing them from the end. Below is the optimized solution. 

Time Complexity: O(max(n1, n2)) where n1 and n2 are lengths of two input strings representing numbers.

Please Login to comment...

  • large-numbers
  • Mathematical
  • Mithun Kumar
  • subhammahato348
  • nareshsaharan1
  • shinjanpatra
  • rohitmishra051000
  • classroompxico
  • siddyamgond
  • esrinivasa7kps
  • chandanagarwv9m5
  • tarunsarawgi_gfg
  • 10 Best ChatGPT Prompts for Lawyers 2024
  • What is Meta’s new V-JEPA model? [Explained]
  • What is Chaiverse & How it Works?
  • Top 10 Mailchimp Alternatives (Free) - 2024
  • Dev Scripter 2024 - Biggest Technical Writing Event By GeeksforGeeks

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

queslers.com

Buy this domain.

The domain queslers.com may be for sale by its owner!

This webpage was generated by the domain owner using Sedo Domain Parking . Disclaimer: Sedo maintains no relationship with third party advertisers. Reference to any specific service or trade mark is not controlled by Sedo nor does it constitute or imply its association, endorsement or recommendation.

IMAGES

  1. Sum of Two Numbers in C

    programming assignment 1 sum of two digits

  2. C Program to Calculate Sum between Two Numbers

    programming assignment 1 sum of two digits

  3. Sum of two integers in c

    programming assignment 1 sum of two digits

  4. Python coding for sum of digits

    programming assignment 1 sum of two digits

  5. Sum of Two Numbers Python Program

    programming assignment 1 sum of two digits

  6. Solved Write a program to print the sum of two digits.

    programming assignment 1 sum of two digits

VIDEO

  1. C Program to read a number find the sum of digits reverse the number and check it palindrome or not

  2. NPTEL » Programming In Java Week 6 Programming Assignment 1 to 5 2023

  3. P5

  4. Coding Interview Tutorial 3: Three Sum [LeetCode]

  5. SEEE1022 INTRODUCTION TO SCIENTIFIC PROGRAMMING ASSIGNMENT 1

  6. Sum of two numbers without using arithmetic operators

COMMENTS

  1. anoubhav/Coursera-Algorithmic-Toolbox

    Coursera: Algorithmic Toolbox. This repository contains all solutions for the course Algorithmic Toolbox offered on Coursera. The assignment solutions are in Python3. Disclaimer: The below solutions are for reference only.Please design and implement your own algorithms to pass the course.

  2. GitHub: Let's build from here · GitHub

    {"payload":{"allShortcutsEnabled":false,"fileTree":{"week1_programming_challenges/1_sum_of_two_digits":{"items":[{"name":"APlusB.cpp","path":"week1_programming ...

  3. Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits

    Problem 1: Sum of Two Digits Solution: (in c++) ( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

  4. PDF ffi cient and reliable code.

    8 Chapter 1. Programming Challenges 1.1Sum of Two Digits Sum of Two Digits Problem Compute the sum of two single digit numbers. Input: Two single digit numbers. Output: The sum of these num-bers. 2+3 = 5 We start from this ridiculously simple problem to show you the pipeline of reading the problem statement, designing an algorithm, implementing

  5. GitHub: Let's build from here · GitHub

    {"payload":{"allShortcutsEnabled":false,"fileTree":{"assignment solutions":{"items":[{"name":"1.1 sum of digits.py","path":"assignment solutions/1.1 sum of digits.py ...

  6. C program to find sum of digits of a number

    To get last digit modulo division the number by 10 i.e. lastDigit = num % 10. Add last digit found above to sum i.e. sum = sum + lastDigit. Remove last digit from number by dividing the number by 10 i.e. num = num / 10. Repeat step 2-4 till number becomes 0. Finally you will be left with the sum of digits in sum.

  7. Program for Sum of the digits of a given number

    Follow the below steps to solve the problem: Get the number. Declare a variable to store the sum and set it to 0. Repeat the next two steps till the number is not 0. Get the rightmost digit of the number with help of the remainder '%' operator by dividing it by 10 and adding it to the sum. Divide the number by 10 with help of ...

  8. Python

    Python Program to Find Sum of Two Numbers. To find sum of two numbers in Python, you can use Arithmetic Addition Operator +. + operator takes two operands and returns the sum of the two numbers. 1. Sum of Two Integers. In this example, we shall take two integers and find their sum. Following are the steps we shall implement. Start. Take two ...

  9. Python's sum (): The Pythonic Way to Sum Values

    Python's built-in function sum() is an efficient and Pythonic way to sum a list of numeric values. Adding several numbers together is a common intermediate step in many computations, so sum() is a pretty handy tool for a Python programmer.. As an additional and interesting use case, you can concatenate lists and tuples using sum(), which can be convenient when you need to flatten a list of ...

  10. python

    a × 10^ p + b × 10^ p-1 .. z × 10^ 0. so the sum of a number's digits is the sum of the coefficients of the terms. Based on this information, the sum of the digits can be computed like this: import math. def add_digits(n): # Assume n >= 0, else we should take abs(n) if 0 <= n < 10: return n.

  11. Solved Programming assignment 1: write a program that asks

    See Answer. Question: Programming assignment 1: write a program that asks the user for 2 numbers, and then do the following calculation and output the results. 1. the sum of those two numbers 2. num1 - num2 + 2 3. num1 x num2 - 5 4. integer results of num1/num2 5. num1 % num2 (remainder of num1/num2) Sample output:. Programming assignment 1:

  12. Algorithmic-Toolbox/APlusB.py at master

    Cannot retrieve contributors at this time. 9 lines (6 sloc) 158 Bytes. Raw Blame. # python3. def sum_of_two_digits (a, b): return a + b.

  13. C Program to Add Two Integers

    In this program, the user is asked to enter two integers. These two integers are stored in variables number1 and number2 ... these two numbers are added using the + operator, and the result is stored in the sum variable. sum = number1 + number2; Add Two Numbers. Finally, the printf() function is used to display the sum of numbers. printf("%d ...

  14. Sum of Two Numbers

    Assignments » Variable, Operator and Expression » Set 1 » Solution 2 Write a program that prompts the user to enter two integers and display their sum on the screen. Source Code

  15. Solved COP1334C

    Final answer. COP1334C - Assignment #1 (Sum the digits in an integer) Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14. Hint: Use the % operator to extract digits, and use the operator to remove the extracted digit.

  16. JavaScript Program for Sum of Digits of a Number

    This process is repeated until a single-digit sum, also known as the digital root. There are several methods that can be used to find the Sum Of Digits by using JavaScript, which are listed below: Using Array Reduce () Method. Using For…of Loop. Using Math.floor and Division. Using forEach () Loop.

  17. Sum of two large numbers

    We traverse both strings from end, one by one add digits and keep track of carry. To simplify the process, we do following: 1) Reverse both strings. 2) Keep adding digits one by one from 0'th index (in reversed strings) to end of smaller string, append the sum % 10 to end of result and keep track of carry as sum/10. 3) Finally reverse the result.

  18. Queslers

    Queslers

  19. Solved Assignments of Coursera's DSA Specialization

    Saved searches Use saved searches to filter your results more quickly

  20. Programming Assignment Programming Assignment 1 Sum of Two Digits.png

    My files - OneDrive Other bookmarks Reading list coursera v Search in course Search Gabriel Tomagos Algorithmic Toolbox > Week 1 > Programming Assignment 1: Sum of Two Digits < Previous Next > Welcome Programming Assignment 1: Programming Challenges Programming Assignment: Programming Video: Solving the Sum of Two Digits Programming Challenge ...

  21. DSA/Programming Assignment Programming Assignment 1 Sum of Two Digits

    Contribute to m0hit-kumar/DSA development by creating an account on GitHub.