Thursday, July 30, 2009

Simple C++ question - PLEASE help.?

"If a four digit number is input through the keyboard, write a program to obtain the sum of the first and the last digit of this number."





Can someone please tell me what code to write for this and explain it? I'm not asking to do my homework - it's summer, but I'm trying to learn C++ on my own and this is an exercise I came across in a book. Any help is greatly appreciated!

Simple C++ question - PLEASE help.?
/*


**** This program will add First and Last digits of a given number


(not only 4 digit)


*/


#include%26lt;iostream%26gt;


using namespace std;





int main(){





long int num;





cout%26lt;%26lt;"enter a integer no";


cin%26gt;%26gt;num;





int sum=(num%10),temp; // sum will initialized with last digit number


// example (1234)%10 = 4 =%26gt; sum=4





while(num%26gt;0){


temp=num%10;


num=num/10;


}


// temp holds the first value


// how mean ex: num=1234


//while(1234 %26gt;0 ) this is true , now temp will store the value 4 and num //


//will become 123,, next loop while(123%26gt;0) temp=3,num=12,,,, next....


//last while(1%26gt;0) temp=1,num=0..next loop fails and the first digit stored


//inside "temp"





sum+=temp; // now adding temp value to sum will result 1+4=5





cout%26lt;%26lt;sum;





return 0;





}





i hope this will be solution u r are looking.
Reply:Myriads of ways to do this, since this is not some assignment that you are dictated to do it certain wau/





Finishing Tarindel's comment, clever combination of / and % is:


while (x %26gt; 0) {


int current = x % 10;


x = x / 10;


}





Although I would do it by inputting as a texture string. That cin will function according to the type of storage variable, use atoi() of probably strlib.h on the splitted up input.
Reply:ok, first up you need to use the cin object in stdio to read a number


so you need an "int n; " somewhere at the top of your function, and cin %26gt;%26gt; n; to get that number from the keyboard.


now you have the number, how do you get the 1st digit?


I am going to assume the number is an integer in base 10, and we don't need to do any error checking (mostly because i am lazy)





we are lucky (if you can call it that for more complex problems) that the '/' operator discards fractions for integer arguments, so n/1000 takes off the last 3 digits,


now how do we get the last digit? n%10 (% is the modulus operator, it returns the remainder when dividing the first argument by the second)


so, this is a function that will do that, what they mean by a program that 'obtains' a number is beyond me, but this is a start (btw i have in no way checked this, but i have reason to believe it will work)





int firstLastSum(void){


int n;


cin%26gt;%26gt;n;


return (n/1000)+(n%10);


}
Reply:I won't tell you how to write the program, but I will give you an idea how you might solve it at least part of it.





I am going to assume the four digit number is an integer. If that is the case, you can use the % (modulus) operator to do this.





The modulus operator returns the remainder of an integer division operation. Consequently, doing nValue % 10 will return the last digit of a number.





Getting the first number is trickier. But you should be able to do it through clever combination of the / and % operators.
Reply:let 5678 is number


first number= 5678/1000 take integer part of answer


last number= 5678%10


now add two


Do programming


be happy
Reply:This program will help you surely:





#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int a[4],i,d,sum=0,count,n;


cout%26lt;%26lt;"Enter the four digit number:";


cin%26gt;%26gt;n;


count=1;


while(n!=0)


{


d=n%10;


if(count==1)


sum+=d;


if(count==4)


sum+=d;


n=n/10;


count++;


}


cout%26lt;%26lt;"Sum of first and last number "%26lt;%26lt;sum;


getch();


}
Reply:You need some samples! Stuff you can just cut a paste into your compiler workbench and compile and run!





I recommend:


http://www.kralidis.ca/gis/cPlusPlus/sam...





Some of these will do almost exactly what you ask, with little modification.





Especially:


http://www.kralidis.ca/gis/cPlusPlus/sam...








That's how I learned C/C++. I gota book and Turbo C. Then, I downloaded other people's code and compiled it. I would just change it a little bit then, and see what that did after compiling and running again.





I kept changing little bits, sometimes until I broke it, but I learned the whole way. It wasn't long before I could write stuff on my own....whole programs.





I could write the code for you in 30 minutes, but we cripple people by doing their work for them. I think you want to learn C/C++, a valuable skill! But how does one learn if the code is always written for him?
Reply:#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


clrscr();


int num,count,sum,rem;


count=sum0;


cout%26lt;%26lt;"enter the no";


cin%26gt;%26gt;num;


do{


rem=num%10;


count++;


if (count==1||count ==4)


sum+=rem;


num/=10;


}while(num!=0);


cout%26lt;%26lt;"\n the sum is"%26lt;%26lt;sum;


getch();


}
Reply:store the number twice


ex: 4098 was the input from the key board


int x = 4098;


double y = 4098;


divide x by 1000 you get 4.098, but since x is only an int you'll get 4 and make the answer equal to x. so it goes something like this: x = x/4098;


to get the first digit you'll need some casting (turning some type into another, in this case a double into an int). once you get the decimal number you multiply this by 10 to get the one's digit. So it should look something like this:


y = (y/10 - (int)(y/10))*10 , where (int)(y/10) is where the casting happens.


Then add x and y: cout%26lt;%26lt;x + y;


hope this works. Its been a while I've done these.


No comments:

Post a Comment