|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
here lies this prorgram
#include <iostream.h> #include <stdlib.h> void main() { a: int x,y,sum=0; char restart; cout<<"Enter the first number "; cin>>x; cout<<"Enter the second number "; cin>>y; if (x%2==0 && y%2==0) { for (int i=x; i<=y; i+=2){ sum=sum+i; } cout<<"Total= "<<sum<<"\n\n"; } if (x%2==0 && y%2!=0) { for (int i1=x; i1<y; i1+=2){ sum=sum+i1; } cout<<"Total= "<<sum<<"\n\n"; } if (x%2!=0 && y%2==0) { for (int i2=x+1; i2<=y; i2+=2){ sum=sum+i2; } cout<<"Total= "<<sum<<"\n\n"; } if (x%2!=0 && y%2!=0) { for (int i3=x+1; i3<y; i3+=2){ sum=sum+i3; } cout<<"Total= "<<sum<<"\n\n"; } cout<<"[If you want to restart the program press y else press n; (Y/ N)] "; cin>>restart; if (restart=='y' || restart=='Y') { system("cls"); goto a; } } Is it correct na ? I made it myself ![]() |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
"Xohaib" wrote:
> here lies this prorgram > > > > > #include <iostream.h> > #include <stdlib.h> > void main() > { > a: > int x,y,sum=0; > char restart; > cout<<"Enter the first number "; > cin>>x; > cout<<"Enter the second number "; > cin>>y; > if (x%2==0 && y%2==0) > { > for (int i=x; i<=y; i+=2){ > sum=sum+i; > } > cout<<"Total= "<<sum<<"\n\n"; > } > if (x%2==0 && y%2!=0) > { > for (int i1=x; i1<y; i1+=2){ > sum=sum+i1; > } > cout<<"Total= "<<sum<<"\n\n"; > } > if (x%2!=0 && y%2==0) > { > for (int i2=x+1; i2<=y; i2+=2){ > sum=sum+i2; > } > cout<<"Total= "<<sum<<"\n\n"; > } > if (x%2!=0 && y%2!=0) > { > for (int i3=x+1; i3<y; i3+=2){ > sum=sum+i3; > } > cout<<"Total= "<<sum<<"\n\n"; > } > cout<<"[If you want to restart the program press y else press n; (Y/ > N)] "; > cin>>restart; > if (restart=='y' || restart=='Y') > { > system("cls"); > goto a; > } > } > > > > Is it correct na ? I made it myself ![]() I would say no. At best it is clumsy. It looks like ( I say looks like because the indentation was lost between you and me) you have four for loops and choosing the proper one depending on the evenness or oddness of x and y. The original question is poorly worded, IMO, but in it, the oddness or evenness of x and y was *not* a criterion. Also, there is no assurance that y is greater than x, I think your program assumes that.. One possible fix involves using the abs() function that lives in <stdlib.h>. The use of the goto is considered extremely poor form. The correct form for the outer loop is a "do loop", since you want to make at least one pass through the loop.. Nitpicking. You are using an old compiler, I suspect your instructor is too lazy to figure out that there are much newer compilers that are *free*. (The use of suffix h on header files is obsolete.) void main() should be int main() Use spaces instead of tabs for Usenet postings, tabs can be lost in transmission.. |
|
![]() |
| Outils de la discussion | |
|
|