i did a program that can check if a number contained in another number , for example :
1234
234
does 1234 contain 234 ? yes . print true
1234
943
does 1234 contain 943 ? no . print false
my problem that i don't know how should i print false , i have an problematic if , so how i can print false only if the number not contained ? if the program prints true its also print false anyway , so how i can make it print false only if the number not contained ?
my code :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
int main() {
int firstnumb, tobechecked, numberofdigits = 0, hezka;
scanf("%d %d", &firstnumb, &tobechecked);
numberofdigits = log10(tobechecked) + 1;
hezka = pow(10, numberofdigits);
while (firstnumb > 0) {
if (firstnumb % hezka == tobechecked) {
printf("true");
}
firstnumb = (firstnumb / 10);
}
printf("false");
}
edited :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
int main() {
int firstnumb, tobechecked, numberofdigits = 0, hezka , result = 0;
scanf("%d %d", &firstnumb, &tobechecked);
numberofdigits = log10(tobechecked) + 1;
hezka = pow(10, numberofdigits);
while (firstnumb > 0) {
if (firstnumb % hezka == tobechecked) {
result = 1;
}
firstnumb = (firstnumb / 10);
}
if (result) {
printf("true");
}
else {
printf("false");
}
}
You can count the number of time the number to be checked was found. Then, after the while loop, use that count to determine if you print true or false.
int count = 0;
while (firstnumb > 0) {
if (firstnumb % hezka == tobechecked) {
count++;
}
firstnumb = (firstnumb / 10);
}
if (count == 0)
{
printf("false");
}
else
{
printf("true");
}
Related
i am trying to make a function to return true if the number is palindrome and return false if the number is not , so first i created a function that reverse the number then another function to tell if the reversed number is equal to the original number , but it not returning for me the correct output , any help ?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
//Reversing a number
int DigitsReversing(int num)
{
int resultofR = 0;
while (num > 0)
{
resultofR = resultofR * 10 + num % 10;
num = num / 10;
}
return resultofR;
}
//telling if the original number equal to the reversed number if yes return if not return false
int isPali(int num)
{
DigitsReversing(num);
int resultofR;
if (num == resultofR)
return true;
else
return false;
return 0;
}
//calling the function
int main()
{
int num;
scanf("%d", &num);
printf("%d", isPali(num));
return 0;
}
edited **** :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int DigitsReversing(int num)
{
int resultofR = 0;
while (num > 0)
{
resultofR = resultofR * 10 + num % 10;
num = num / 10;
}
return resultofR;
}
bool isPali(int num , int resultofR)
{
resultofR = DigitsReversing(num);
if (num == resultofR)
return true;
else
return false;
}
int main()
{
int num, resultofR;
scanf("%d", &num);
resultofR = DigitsReversing(num);
isPali(num, resultofR);
C does have a boolean datatype. Usually, 0 is false and 1 is true. They return value 1 for true and 0 for false.
As #exnihilo and #bob__ suggested C does have boolean datatypes.
#include<stdio.h>
#include<math.h>
//Reversing a number
int DigitsReversing(int num)
{
int resultofR = 0;
while (num > 0)
{
resultofR = resultofR * 10 + num % 10;
num = num / 10;
}
return resultofR;
}
//telling if the original number equal to the reversed number if yes return if not return false
void isPali(int num)
{
int resultofR= DigitsReversing(num);
if (num == resultofR)
printf("True");
else
printf("False");
}
//calling the function
int main()
{
int num;
scanf("%d",&num);
isPali(num);
return 0;
}
OUTPUT
For non-palindrome numbers.
9998
False
--------------------------------
Process exited after 10.35 seconds with return value 0
Press any key to continue . . .
For palindrome numbers.
9999
True
--------------------------------
Process exited after 10.35 seconds with return value 0
Press any key to continue . . .
The posted snippets show some confusion about fundamental topics like the correct way to pass arguments to a function and collect the returned value or just the differnece between an "output" and a "returned" value.
Consider this, for example:
bool isPali(int num , int resultofR)
{ // ^^^^^^^^^ It's passed to the function...
resultofR = DigitsReversing(num); // <- and immediately overwritten by another value
if (num == resultofR)
return true;
else
return false;
}
int main()
{
int resultofR;
// ...
resultofR = DigitsReversing(num); // <-- This is effectually called twice
isPali(num, resultofR); // <-- The returned value is ignored
}
You just need to write one function
#include <stdbool.h>
bool is_palindrome(long num)
{
long reversed = 0;
for (long i = num; i > 0; i /= 10)
{
reversed = reversed * 10 + i % 10;
}
return reversed == num;
}
Then, call it and interpret its return value properly, to generate the wanted output
int number;
scanf("%d", &number);
printf("%s\n", is_palindrome(number) ? "True" : "False");
I'm practicing recursion and my solution to the problem doesn't seem to work.
I'm trying to write a recursive code that will determine if the digits of a number are in ascending order or not. here's my code:
#include <stdio.h>
int isAscending(int num);
int main(){
int result;
result = isAscending(123);//Should print "The number is in ascending order!"
if (result == 0) {
printf("The number is in ascending order!\n");
}
else {
printf("The number is not in ascending order!\n");
}
}
int isAscending(int num) {
int new = num / 10;
int result = 0;
if ((num % 10) == 0) {
return 0;
}
else if ((num % 10) > (new % 10)) {
result += isAscending(num / 10);
return result;
}
else {
return 1;
}
}
Here's another (bare-bones) way to go about it. The basic idea is that if we have a single digit, we return affirmative, else we check if the rightmost number is greater than the one just to it's left. And we do this for the remaining digits.
#include <stdio.h>
int isAsc(int i)
{
int rem = i % 10; // remainder
int quo = i / 10; // quotient
if (rem == i)
return 1;
else if (rem <= (quo % 10))
return 0;
else
return 1 && isAsc(quo);
}
int main(void)
{
int i = 123123;
if (isAsc(i))
printf("%s\n", "Ascending");
else
printf("%s\n", "Not ascending");
return 0;
}
Can you please try below recurrsive code:
`
boolean isascending(int num){
if(num == 0) return true;
if(num%10>num%100) return isascending(num/10);
else return false;
}`
or you can use while loop:
while(num>0){
if(num%10 > num%100){
num = num/10;
continue;
} return false;
} return true;
It would be better to use another parameter to store the last digit, which is going to be "dropped" in the current iteration.
So I came up with the following recursive logic:
use a parameter which stores the last digit dropped
base case: if the number is 0, return 0 (true)
calculate the current last digit of the number (number%10)
if the current last digit is greater than the last digit dropped: is this case, return 1 (false)
if not, return isAscendingRecursive() on the new number dropping the current last digit and pass it as the next iteration last digit.
Code:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** args){
int num=0;
printf("Insert a number:\n");
scanf("%d",&num);
if(isAscending(num)==0)
printf("Ascending\n");
else
printf("Not ascending\n");
}
int isAscending(int num){
return isAscendingRecursive(num,9);
}
int isAscendingRecursive(int num, int lastDigit){
if(num == 0)
return 0;
int temp = num%10;
if(temp > lastDigit)
return 1;
else
return isAscendingRecursive(num/10, temp);
}
This solution returns 0 on failure otherwise some other integer on success. It seems that isDescending() is easier to write when returning 0 as a failure value but I contorted this accordingly:
#include <stdio.h>
#include <stdlib.h>
int isAscending(int num) {
int quotient = num / 10;
int remainder = num % 10;
if (quotient != 0) {
int result = isAscending(quotient);
if (result == 0 || result >= remainder) {
return 0;
}
}
return remainder;
}
int main(int argc, char **argv) {
if (isAscending(atoi(argv[1]))) {
printf("The number is in ascending order!\n");
} else {
printf("The number is not in ascending order!\n");
}
return 0;
}
TESTS
% ./a.out 123
The number is in ascending order!
% ./a.out 321
The number is not in ascending order!
% ./a.out 101
The number is not in ascending order!
%
No, it doesn't handle negative numbers! It also doesn't handle '0' correctly as an input -- other single digit numbers are fine.
Again, isDescending() is easier to write but unfortunately, !isDescending() != isAscending()
Your tests are incorrect. You should return true for numbers with a single digit, false if the last digit is less or equal to the previous one and recurse for the rest:
int isAscending(int num) {
int new = num / 10;
if (new == 0) {
return 1;
} else
if (num % 10 <= new % 10) {
return 0;
} else {
return isAscending(new);
}
}
This kind of recursion is called tail recursion as you return the result of the recursive call. Good compilers will generate iterative code equivalent to this:
int isAscending(int num) {
for (;;) {
int new = num / 10;
if (new == 0) {
return 1;
}
if (num % 10 <= new % 10) {
return 0;
}
num = new;
}
}
I fixed my code and it works, thanks for the help!:
#include <stdio.h>
int isAscending(int num);
int main(){
int result;
result = isAscending(2589);//Should print "The number is in ascending order!"
if (result == 0) {
printf("The number is in ascending order!\n");
}
else {
printf("The number is not in ascending order!\n");
}
}
int isAscending(int num) {
int new = num / 10;
int result = 0;
if ((num % 10) == 0) {
return 0;
}
else if ((num % 10) > (new % 10)) {
return isAscending(num / 10);
}
else {
return 1 + isAscending(num / 10);
}
}
I wish to transform the basic Fibonacci function:
int find_fib(int fib_to) {
if (fib_to == 0) {
return 0;
}
else if (fib_to == 1) {
return 1;
}
else {
return (find_fib(fib_to - 1) + find_fib(fib_to - 2));
}
}
into one that would use only ONE recursive call. I searched up many sites that tells me to store the value found by (find_fib(fib_to - 1) + find_fib(fib_to - 2)) into some array and then make use of the array, but doing so requires 2 recursive calls.
Any tips on how to solve the problem?
You mean something like that?
#include <stdio.h>
int fibonacci(int number_iterations, int number_a, int number_b)
{
if(number_iterations <= 0) {
printf("error: 'number_iterations' must be >= 1\n");
return -1;
}
if(number_iterations == 1) {
printf("%d ", number_a + number_b);
return number_a + number_b;
}
printf("%d ", number_a + number_b);
return fibonacci(number_iterations - 1, number_b, number_a + number_b);
}
int main()
{
int a = 1;
int b = 1;
int number_of_iterations = 0;
printf("Enter a number >= 1: ");
int err = scanf("%d", &number_of_iterations);
int final_result = -1;
if (err != EOF) {
printf("fibonacci: %d %d ", a, b);
final_result = fibonacci(number_of_iterations, a, b);
}
printf("Final fibonacci: %d\n", final_result);
return 0;
}
would return you:
Enter a number >= 1: 10
fibonacci: 1 1 2 3 5 8 13 21 34 55 89 144
Final fibonacci: 144
I am doing the first problem on Project Euler.
I have the following code:
#include <stdio.h>
int main() {
int number;
int sum;
while (number < 1000) {
if (number % 3 == 0 || number % 5 == 0) {
sum += number;
number++;
}
else {
number++;
}
}
printf("The answer is %d", sum);
return 0;
}
When I compile this via compileonline.com, I get 233168. When I compile this in gcc I get 2686824. What causes this difference?
Compileonline probably initializes the variables.
You have to initialize them manually.
#include <stdio.h>
int main() {
int number = 0;
int sum = 0;
while (number < 1000) {
if (number % 3 == 0 || number % 5 == 0) {
sum += number;
number++;
}
else {
number++;
}
}
printf("The answer is %d", sum);
return 0;
}
I have typed the following program in C language:
#include <stdio.h>
int main ()
{
int i = 1, a = 2;
while (i <= 300)
{
while (a < i)
{
if (i % a == 0)
break;
else
printf ("%d\n", i);
a++;
}
i++;
}
return 0;
}
The program is printing many non-prime values too like 295, 275, etc...
Please help, I'm a beginner and lacks much experience.
Beside the fact that you're not resetting a to 2, the printf() statement is not placed correctly. This will print any number of i that cannot be divided by 2 (even if it can be divided by another number).
Change your code like this:
#include <stdio.h>
int main ()
{
int i = 1, a = 2, is_prime;
while (i <= 300)
{
is_prime = 1;
while (a < i)
{
if (i % a == 0) {
is_prime = 0;
break;
}
a++;
}
if(is_prime)
printf ("%d\n", i);
a = 2;
i++;
}
return 0;
}
You need to set a back to 2 before you enter the loop:
#include <stdio.h>
int main ()
{
int i = 1, a = 2;
while (i <= 300)
{
a = 2; // add this line
while (a < i)
{
if (i % a == 0)
break;
else
printf ("%d\n", i);
a++;
}
i++;
}
return 0;
}
Your method isn't very fast. You should read this page http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes