निम्नलिखित कोड का आउटपुट क्या होगा?
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)
B)
C)
D)
Explanation:
The program calculates the sum of the differences between the ASCII values of characters in the string "abcde"
and the ASCII value of 'a'
.
- For each character (
'a'
,'b'
,'c'
,'d'
,'e'
), it calculates the difference with'a'
and adds it toans
. - The sum of these differences is
0 + 1 + 2 + 3 + 4 = 10
.
Correct Answer:
(D) 10.