This program calculates the sum of all the natural numbers from a given value down to 1 using a goto statement. Here's how the program works:
While this program works correctly, using a goto statement is generally not considered good programming practice because it can make the code difficult to read and maintain. A better alternative would be to use a while or for loop to calculate the sum.
#include<iostream> using namespace std; int main() { int a,n=0; cout<<"\nEnter the value :"; cin>>a; start: n=n+a; a--; if(a>=0) { goto start; } cout<<"\nTotal value :"<<n; return 0; }To download raw file Click Here
Enter the value :5 Total value :15
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions