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/sampleCode/
Some of these will do almost exactly what you ask, with little modification.
Especially:
http://www.kralidis.ca/gis/cPlusPlus/sampleCode/samples/1-32.txt
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?
2007-06-19 18:09:47
·
answer #1
·
answered by gene_frequency 7
·
0⤊
0⤋
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 >> 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>>n;
return (n/1000)+(n%10);
}
2007-06-19 18:20:52
·
answer #2
·
answered by toby d 1
·
0⤊
0⤋
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<
hope this works. Its been a while I've done these.
2007-06-19 18:23:38
·
answer #3
·
answered by killerpens 2
·
0⤊
0⤋
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.
2007-06-19 18:12:12
·
answer #4
·
answered by Tarindel 3
·
0⤊
0⤋
/*
**** This program will add First and Last digits of a given number
(not only 4 digit)
*/
#include
using namespace std;
int main(){
long int num;
cout<<"enter a integer no";
cin>>num;
int sum=(num%10),temp; // sum will initialized with last digit number
// example (1234)%10 = 4 => sum=4
while(num>0){
temp=num%10;
num=num/10;
}
// temp holds the first value
// how mean ex: num=1234
//while(1234 >0 ) this is true , now temp will store the value 4 and num //
//will become 123,, next loop while(123>0) temp=3,num=12,,,, next....
//last while(1>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<
return 0;
}
i hope this will be solution u r are looking.
2007-06-19 20:18:20
·
answer #5
·
answered by Anonymous
·
0⤊
0⤋
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 > 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.
2007-06-19 19:01:35
·
answer #6
·
answered by Andy T 7
·
0⤊
0⤋
This program will help you surely:
#include
#include
void main()
{
int a[4],i,d,sum=0,count,n;
cout<<"Enter the four digit number:";
cin>>n;
count=1;
while(n!=0)
{
d=n%10;
if(count==1)
sum+=d;
if(count==4)
sum+=d;
n=n/10;
count++;
}
cout<<"Sum of first and last number "<
getch();
}
2007-06-19 20:04:28
·
answer #7
·
answered by sathiyendran a 3
·
0⤊
0⤋
Yowee this became a handful of tissues, thank you. i'm an easy country youngster, yet i'm comfortable with that my interest for words and rhyme is joyfully fat not easy minded in any respect whilst the words come I heed their call. extremely i've got thrown away some skinny of their epidermis yet my expressing is what i'm sequestered in
2016-10-18 02:49:56
·
answer #8
·
answered by harren 4
·
0⤊
0⤋
let 5678 is number
first number= 5678/1000 take integer part of answer
last number= 5678%10
now add two
Do programming
be happy
2007-06-19 18:22:38
·
answer #9
·
answered by kashyff 2
·
0⤊
0⤋
#include
#include
void main()
{
clrscr();
int num,count,sum,rem;
count=sum0;
cout<<"enter the no";
cin>>num;
do{
rem=num%10;
count++;
if (count==1||count ==4)
sum+=rem;
num/=10;
}while(num!=0);
cout<<"\n the sum is"<
getch();
}
2007-06-19 18:24:32
·
answer #10
·
answered by arkidmitra 2
·
0⤊
0⤋