निम्नलिखित कोड का आउटपुट क्या होगा?
What will be the output of the following code ?
#include <stdio.h>
void solve() {
char ch[5] = "abcde";
int ans = 0;
for(int i = 0; i< 5; i++) {
ans += (ch[i] - 'a');
}
printf("%d", ans);
}
int main() {
solve();
return 0;
}
A
5
B
20
C
40
D
10
Explanation
Code Breakdown:
-
Character Array: The array
ch[5]is initialized with the string"abcde", which meansch[0] = 'a',ch[1] = 'b', and so on. -
Loop & Calculation: The program loops through each character, subtracts the ASCII value of
'a'(which is 97) from each character, and adds the result toans.'a'→97 - 97 = 0'b'→98 - 97 = 1'c'→99 - 97 = 2'd'→100 - 97 = 3'e'→101 - 97 = 4
-
Summing Up: The final sum of
ansis0 + 1 + 2 + 3 + 4 = 10.
Output:
The output will be 10.
Correct Answer:
(D) 10.
Correct Answer: D) 10