|
|
back to boardmy program always find 5 answers of the input number why this is so? Posted by sultan 16 Aug 2002 01:35 my program is getting 5 answers for every input pls tell me the sequence what will be the gap in the my outputs sir here is my program pls read it and tell me what is wrong with it pls ///////////////////////////#include <iostream.h> #include <stdio.h> #include <ctype.h> #include <string.h> int function(int); void main() { long number,see; long half,result; int length,counter=0; long store,store1; int y; char string[10]; char string1[10];
cout<<" enter the number "; cin>>number; half=number/2; sprintf(string,"%ld",number); //converting int into string. length=strlen(string); // cout<<length; y=function(length-1); //second number sprintf(string,"%ldf",number); if(strlen(string)>=2 && string[0]!=0) { store=half+y; store1=number-store; sprintf(string,"%ld",store); sprintf(string1,"%ld",store1); int see2=strlen(string); if(store1 < 0) { int val=9; for(int z=1;z<((see2-1));z++) { val*=10; } y=y-val; store=half+y; store1=number-store; } see=strlen(string1); if(strlen(string)<=strlen(string1)) { int y=1; for(int i=1;i<=(see-1);i++) { y*=10; } y-=1; if((see-1)>=2) y+=100; if(store1>y) { store1-=y; if(store1<0) { store1+=y; y-=100; store1-=y; } store+=y; } } cout<<endl; cout<<5<<endl; sprintf(string,"%ld",store); sprintf(string1,"%ld",store1); // clrscr(); // cout<<string<<endl<<string1; //cout<<strlen(string)<<endl; // cout<<strlen(string1)<<endl; while(counter!=5) { if(strlen(string) >strlen(string1)) { if(((string[strlen(string)-2])=='.') && (string[strlen(string)-1]=='5')) //&&(string1[(strlen(string1)-2)] =='.')) { store-=0.5; store+=1; store1-=0.5; } result=store+store1; if(strlen(string)-strlen(string1)==2) cout<<store<<" "<<"+"<<" 0"<<store1<<" "<<"="<<" "<<result<<endl; else if(strlen(string)-strlen (string1)==3) cout<<store<<" "<<"+"<<" 00"<<store1<<" "<<"="<<" "<<result<<endl; else if(strlen(string)-strlen (string1)==4) cout<<store<<" "<<"+"<<" 000"<<store1<<" "<<"="<<" "<<result<<endl; else if(strlen(string)-strlen (string1)==4) cout<<store<<" "<<"+"<<" 0000"<<store1<<" "<<"="<<" "<<result<<endl; else
cout<<store<<" "<<"+"<& |
|
|