🚀 Hurry! Offer Ends In
00 Days
00 Hours
00 Mins
00 Secs
Enroll Now
X

निम्नलिखित कोड का आउटपुट क्या होगा?

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:

Code Breakdown:

  1. Character Array: The array ch[5] is initialized with the string "abcde", which means ch[0] = 'a', ch[1] = 'b', and so on.

  2. Loop & Calculation: The program loops through each character, subtracts the ASCII value of 'a' (which is 97) from each character, and adds the result to ans.

    • 'a'97 - 97 = 0
    • 'b'98 - 97 = 1
    • 'c'99 - 97 = 2
    • 'd'100 - 97 = 3
    • 'e'101 - 97 = 4
  3. Summing Up: The final sum of ans is 0 + 1 + 2 + 3 + 4 = 10.

Output:

The output will be 10.

Correct Answer:

(D) 10.

Latest Updates