Given two integers num1
and num2
, return the sum of the two integers.
Example 1:
Input: num1 = 12, num2 = 5 Output: 17 Explanation: num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.
Example 2:
Input: num1 = -10, num2 = 4 Output: -6 Explanation: num1 + num2 = -6, so -6 is returned.
Constraints:
-100 <= num1, num2 <= 100
program main
implicit none
integer :: num1, num2, result
! Example 1
num1 = 12
num2 = 5
result = sum(num1, num2)
print '(A, I0)', 'Example 1: ', result
! Example 2
num1 = -10
num2 = 4
result = sum(num1, num2)
print '(A, I0)', 'Example 2: ', result
contains
function sum(num1, num2) result(result)
implicit none
integer, intent(in) :: num1, num2
integer :: result
result = num1 + num2
end function sum
end program main
Example 1: 17 Example 2: -6
```
program sum_two_numbers
implicit none
integer :: num1, num2, sum
! Read the input
read *, num1, num2
! Calculate the sum
sum = num1 + num2
! Print the output
write (*,*) sum
end program sum_two_numbers
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:16:2: 16 | ``` | 1 Error: Invalid character in name at (1)
def capitalize_title(title):
return ' '.join([word.capitalize() for word in title.split(' ')])
The algorithm loops through each character of the string `title`. If it encounters a new word edge (either beginning of the title or previous character was a space), the current character is capitalized. In all other cases, the character is converted to lower case. In Python, we use the built-in `.capitalize()` method to capitalize each word in the title. We split the string into words, apply the capitalize method on each word, and join the words by spaces.
#include <string>
using namespace std;
string capitalizeTitle(string title) {
bool newWord = true;
for (int i = 0; i < title.length(); ++i) {
if (newWord && isalpha(title[i])) {
title[i] = toupper(title[i]);
newWord = false;
} else if (title[i] == ' ') {
newWord = true;
} else {
title[i] = tolower(title[i]);
}
}
return title;
}
The algorithm loops through each character of the string `title`. If it encounters a new word edge (either beginning of the title or previous character was a space), the current character is capitalized. In all other cases, the character is converted to lower case. In Python, we use the built-in `.capitalize()` method to capitalize each word in the title. We split the string into words, apply the capitalize method on each word, and join the words by spaces.