Sweet numbers , problems with checking if the number is sweet [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
We put a range from keyboard from what number to what number we want to search the ''Sweet number''.
One number is sweet if each of the digits is an even number. So I started and don't know how to go on
int min,max,i,sweetnumber;
scanf("%d%d",&min,&max);
for(i=min;i<=max;i++)
{
// and now what ? i don't know if the number is three-digit or four-digit or five digit so i can check a digit by digit .. Someone help !
}

It seems you need something like the following
for ( i = min; i <= max; i++ )
{
int tmp = i;
while ( tmp != 0 && tmp % 10 % 2 == 0 ) tmp /= 10;
if ( tmp == 0 ) printf( "%d is a sweet number\n", i );
}

Related

C code that finds the number pi using leibniz formula [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am asked to write the C code that finds the number pi using the Leibniz formula.
However, the result should be 3.14 but result turns 3.23. What is the reason for this?
//Calculating the value of PI
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
//function main begins program execution
int main( void )
{
float pi = 0;
size_t k,n;
for ( n = 0 , k = 0; n <= 10 , k <= 10; n++ ,k++) {
pi += ( pow( -1, n ) * 4 )/ ( 2 * k + 1 );
}//end for
printf(" pi is %.2f\n",pi );
getch();
return 0;
}//end main
I played with it by increasing your iterations.
At "200" I got 3.15.
Basically "10" isn't even close to enough.

What code is faster? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
We have the following 2 snippets of code in c that do the same task.
CODE #1:
int b = 0;
for (int i = 0; i < len; i++)
{
if (x1 == x0[i])
{
if (y1 == y0[i])
{
b = 1;
break;
}
}
}
CODE #2:
int b = 0;
for (int i = 0; i < len; i++)
{
if (x1 == x0[i] && y1 == y0[i])
{
b = 1;
break;
}
}
What faster CODE #1 or CODE #2?
I really searched answer in the internet but did not find anything.
None!
They are both the same code.
They are written differently, but take the exact same instructions and comparisons to achieve the result, therefore, they are the same.
So, none of them is faster than the other.

What is the logic is used in this program [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
A 5 digit positive number is entered by user.how to calculate the sum of the Digits entered by the user with the help of function?
We beginners should help each other.:)
Here you are.
#include <stdio.h>
unsigned int digits_sum( unsigned int value )
{
const unsigned int Base = 10;
unsigned int sum = 0;
do { sum += value % Base; } while ( value /= Base );
return sum;
}
int main( void )
{
unsigned int value = 12345;
printf( "The sum of digits of number %u is %u\n", value, digits_sum( value ) );
return 0;
}
The program output is
The sum of digits of number 12345 is 15
The logic is simple. To get the last digit of a number you should apply operator %. Then you need to divide the number by 10 that in the next step to get the digit before the last and so on.

How to print char value in int array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array board[3][3] of type int. I want to show 8 numbers and on the last place "_" must be shown. I don't know how to do that. Please help
If the 8 numbers you want print is the first 8, you can just use a loop to with printf to print it until the last number:
int j, k;
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
if (j == 2 && k == 2)
printf("_");
else
printf("%d ", board[j][k]);
}
printf("\n");
}

C how to check and point a '0' in a int number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Imagine we've got an int number = 1040 or int number = 105 in a C program, and we want to know if this number contains a 0 and in which position/s are they. How could we do it?
Examples
1040 -> position 0 and 2.
1000 -> position 0, 1 and 2.
104 -> position 1.
56 -> NO ZEROS.
Thanks!
I would divide by 10 and check the remainder. If remainder is 0, then last position of the number is 0. Then repeat the same step until number is less than 10
#include<iostream>
int main(void)
{
long int k = 6050404;
int iter = 0;
while (k > 10) {
long int r = k % 10;
if( r == 0) {
std::cout << iter << " ";
}
k = k / 10;
iter++;
}
}
I would convert it to a string; finding a character in a string is trivial.
As a rule of thumb, if you are doing maths on something, it's a number; otherwise, it's probably (or should be treated as) a string.
Alternatively, something like:
#include <stdio.h>
int main(void) {
int input=1040;
int digitindex;
for (digitindex=0; input>0; digitindex++) {
if (input%10==0) {
printf("0 in position %i\n",digitindex);
}
input/=10;
}
return 0;
}
This basically reports if the LAST digit is 0, then removes the last digit; repeat until there is nothing left. Minor modifications would be required for negative numbers.
You can play with this at http://ideone.com/oEyD7N

Resources