ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It is not giving the actual values not even for odd,even positions
We understand that it is not working and why it is not working. I added some detail to my earlier post (after pressing send the first time), so if you read my post too soon, please reread it. Otherwise, you answered too fast to have thought about the things I suggested you think about.
if ( i % 2 == 0 )
{
nEven+=r;
}
else
{
nOdd+=r;
}
if ((i) % 3 == 0 )
{
nThree+=r;
}
}
printf("odd Sum %d ",nOdd);
printf("Even Sum %d ",nEven);
printf("Position Three Sum %d ",nThree);
return;
}
int CountDidgets(int val)
{
int d = 1, c;
if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
else for (c = -10 ; c >= val; c *= 10) d++;
return (c < 0) ? ++d : d;
}
Quite interesting. This is doable in C of course, but just as a proof on concept, we could do it in bash as well:
Code:
#!/bin/bash
read -p "Enter an integer: " INT
shopt -s extglob
if [[ $INT != +([[:digit:]]) ]]; then
echo "That's not an acceptable integer."
exit 1
fi
SUMS=()
for (( I = ${#INT}; I; --I )); do
D=${INT:I - 1:1}
if (( I % 2 )); then
(( SUMS[1] += D ))
else
(( SUMS[2] += D ))
fi
if (( (I % 3) == 0 )); then
(( SUMS[3] += D ))
fi
done
echo "${SUMS[*]}"
1) When posting code, please use CODE tags. Reading code the way you just posted it is difficult. This time I didn't bother. Many experts here will never take the extra effort to read code that is posted badly.
2) When answering homework questions, please do not do the homework. There are lots of programmers in this forum for whom these homework assignments are trivial, so no one will be impressed that someone not taking the class can do the homework.
It is usually harder to give helpful information that enables the OP to do his own homework than it is to provide a solution the OP could copy. But that is the goal here.
For eg: given an integer 158176
your program should add
- 1,8,7 which are in the position 0,2,4 of the integer AND
- 5,1,6 which are in position 1,3,5 of the integer AND
- 8,6 which are in every 3rd place in the integer.
So the answer will be 1+8+7=16, 5+1+6=12 and 8+6=14. The output will be 16,12,14
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.