Answer:
#include <stdio.h>
#include <string.h>
int main(){
char number[100];
printf("Number: ");
scanf("%s", number);
int sum = 0;
for(int i =0;i<strlen(number);i++){
sum+= number[i] - '0';
}
printf("Sum: %d",sum);
return 0;
}
Explanation:
This declares a c string of 100 characters
char number[100];
This prompts user for input
printf("Number: ");
This gets user input
scanf("%s", number);
This initializes sum to 0
int sum = 0;
This iterates through the input string
for(int i =0;i<strlen(number);i++){
This adds individual digits
sum+= number[i] - '0';
}
This prints the calculated sum
printf("Sum: %d",sum);
return 0;
Answer:
The program in Python is as follows:
myList = []
num = int(input())
count = 0
while num>=0:
myList.append(num)
count+=1
num = int(input())
myList.sort()
mid = int((count-1)/2)
print(myList[mid])
Explanation:
This initializes the list
myList = []
This gets the first input
num = int(input())
This initializes count to 0
count = 0
The following is repeated until the user inputs a negative number
while num>=0:
This appends the input number to the list
myList.append(num)
This increments count by 1
count+=1
This gets another input
num = int(input())
This sorts the list
myList.sort()
Assume the number of inputs is odd, the middle element is calculated as
mid = int((count-1)/2)
This prints the middle element
print(myList[mid])
<em>From the complete question. the condition that ends the loop is a negative integer input</em>
The answer is 243. Hope it helps
Answer:
scope
Explanation:
Destructor is a member function and it call automatically when the class object goes out of scope.
Out of scope means, the program exit, function end etc.
Destructor name must be same as class name and it has no return type.
syntax:
~class_name() { };
For example:
class xyz{
xyz(){
print(constructor);
}
~xyz(){
print(destructor);
}
}
int main(){
xyz num;
}//end program
when the object is create the constructor will called and when the program end destructor will call automatically.
Answer:
mystr = input("Enter a string ")
length = len(mystr)
while length<10:
mystr = input("Enter a string ")
length = len(mystr)
if(length>=10):
break
if len(mystr)%2==0:
print(mystr.lower())
else:
print(mystr.upper())
Explanation:
The variable mystr is used to save user's input which is received with the input function
A second variable length is used to save the length of the input string Using a while statement the user is continually prompted to enter a string while length is less than 10.
If length is greater or equal to 10. We check for even or odd using the modulo (%) operator.
We use lower() and upper() to change the case of the string