#include<stdio.h>
main()
{
int i,n,sum=0;
printf("Enter a number\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
sum=sum+i;
}
printf("%d\n",sum);
}
Output:-
Enter a number
10
55
Explain:
प्रोग्राम प्रीप्रोसेसर निर्देश "#include <stdio.h>" के साथ मानक इनपुट/आउटपुट लाइब्रेरी को शामिल करके शुरू होता है। मुख्य फ़ंक्शन "int main()" से शुरू होता है और इसमें कोई तर्क नहीं होता है। फिर, यह तीन पूर्णांक चर घोषित करता है: num1, num2, और sum। प्रिंटफ स्टेटमेंट का उपयोग उपयोगकर्ता को दो नंबर दर्ज करने के लिए प्रेरित करने के लिए किया जाता है, और स्कैनफ स्टेटमेंट का उपयोग उपयोगकर्ता से इनपुट को पढ़ने और इसे वेरिएबल num1 और num2 में संग्रहीत करने के लिए किया जाता है। कथन "sum = num1 + num2" दो संख्याओं को जोड़ता है और परिणाम को वेरिएबल "sum" में संग्रहीत करता है। अंत में, प्रिंटफ स्टेटमेंट का उपयोग उपयोगकर्ता द्वारा दर्ज किए गए दो नंबरों का योग प्रदर्शित करके परिणाम प्रिंट करने के लिए किया जाता है। प्रोग्राम रिटर्न स्टेटमेंट "रिटर्न 0;" के साथ समाप्त होता है। जो ऑपरेटिंग सिस्टम को इंगित करता है कि प्रोग्राम सफलतापूर्वक निष्पादित हो गया है।आप इस प्रोग्राम को जीसीसी जैसे सी कंपाइलर का उपयोग करके संकलित करके और फिर निष्पादन योग्य चलाकर चला सकते हैं। यह ध्यान रखना महत्वपूर्ण है कि प्रोग्राम केवल पूर्णांक संख्याओं को इनपुट के रूप में स्वीकार करेगा, यदि आप अन्य प्रकार की संख्याओं का उपयोग करना चाहते हैं, तो आपको स्कैनफ फ़ंक्शन में संबंधित प्रारूप विनिर्देशक का उपयोग करना होगा।
The program starts by including the standard input/output library with the preprocessor directive "#include <stdio.h>". The main function starts with "int main()" and it takes no arguments. Then, it declares three integer variables: num1, num2, and sum. The printf statement is used to prompt the user to enter two numbers, and the scanf statement is used to read the input from the user and store it in the variables num1 and num2. The statement "sum = num1 + num2" performs the addition of the two numbers and stores the result in the variable "sum". Finally, the printf statement is used to print the result by displaying the sum of the two numbers entered by the user. The program ends with the return statement "return 0;" which indicate to the operating system that the program has executed successfully.
You can run this program by compiling it using a C compiler, such as GCC, and then running the executable. It is important to note that the program will only accept integer numbers as input, if you want to use other types of numbers, you need to use the corresponding format specifier in the scanf function.