Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Just in case you need to know what the program I'm working on -It's a homework question:A five-digit number is entered through the keyboard. Write a function to obtain the reversed number and another function to determine whether the original and reversed numbers are equal or not. Use this functions inside the main() and provide the necessary arguments to get a result.
My code is:
#include <stdio.h>
int Reversed(int rev);
int Equality(int equ);
int main (){
int num,result;
printf("Please enter a number that has five digits:");
scanf("%d", &num);
result=Equality(num);
return 0;
}
int Reversed(int num){
int number=num;
int rev=0;
int digit;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
while ((num>0));
return rev;
}
int Equality(num){
int reve,numb;
if ( (numb=num)== (reve=Reversed(num);) )
printf("number equals the reversed number");
else
printf("number doesn't equal the reversed number");
}
There are some points to fix:
Remove semicolon in if ( (numb=num)== (reve=Reversed(num);) ), which is not ought to be. ( the main problem)
Format your code properly.
Equality() doesn't have return statement, so what it returns is not usable.
I guess Equality() is supposed to only determine that, not to print the result.
The variables reve and numb in Equality(), and number in Reversed() are not used other than being assigned, so you won't need them.
Type of function arguments shouldn't be omitted.
corrected code:
#include <stdio.h>
int Reversed(int rev);
int Equality(int equ);
int main (void){
int num;
printf("Please enter a number that has five digits:");
if(scanf("%d", &num) != 1){
puts("read error");
return 1;
}
if(Equality(num))
printf("number equals the reversed number");
else
printf("number doesn't equal the reversed number");
return 0;
}
int Reversed(int num){
int rev=0;
int digit;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
while (num>0);
return rev;
}
int Equality(int num){
return ( num == Reversed(num) );
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I use the following code to count the amount of digits in a while loop, so "0" should be 1, "10" should be 2 etc. - however the code does not seem to work. Can you please help me?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
division=x/10;
counter++;
}
while(division!=0);
printf("This number contains : %d digits",counter);
return 0;
}
please change division=x/10; to x /= 10 and corresponding while condition. x is not changed your code, thus you get stucked in you while loop forever
This line:
division = x / 10;
Will be performed forever since the condition given in the while logic never becomes false.
If you do:
do {
x = x / 10;
counter++;
} while (x != 0);
It'll work.
Enhanced version of your code:
#include <stdio.h>
int main() {
int x;
int counter = 0;
printf("Enter a number : ");
// looping until a correct format is provided
while (scanf("%d", &x) == 0) {
printf("Incorrect values, enter again: ");
fseek(stdin, 0, SEEK_END);
}
do {
x = x / 10;
counter++;
} while (x != 0);
printf("This number contains : %d digits.", counter);
return 0;
}
The intention behind the "enhanced version" is to verify if the input is correctly given as formatted in coding (i.e. accepting an integer and nothing else) which isn't in your program.
Also, you don't need to include stdlib.h for your own code. That works without it too.
You'll then get the following sample output:
Enter a number : asdlfjal;sdk
Incorrect values, enter again: asdf sdf
Incorrect values, enter again: 33334
This number contains : 5 digits.
You are not changing the division value. This should work
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
x=x/10;
counter++;
}
while(x!=0);
printf("This number contains : %d digits",counter);
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This is my attempt at recursion. It compiles and runs, but doesn't display the factorial of the number that I input. I'm attempting this with Geany on Ubuntu.
#include <stdio.h>
int fact(int n);
int main() {
int n;
printf("Give me a number");
scanf("%6d", &n);
fact(n);
}
int fact(int n) {
if (n <= 1)
return 1;
else
return n * fact(n - 1);
}
You are missing the print statement.
You could save the result in a variable and then print it.
printf("%d",fact(n));
Your code is fine, but you forget to to print the return value of function fact() change this portion of your code
scanf("%6d", &n);
fact(n);
to this:
scanf("%6d", &n);
printf("%d", fact(n));
Your work will done. after replacing your main() function will look like:
int main()
{
int n;
printf("Give me a number");
scanf("%6d", &n);
printf("%d", fact(n));
}
Note: The factorial of 17 or higher is not adjust in integer limit.
doesn't display the factorial of the number that I input.
in
int main()
{
int n;
printf("Give me a number");
scanf("%6d", &n);
fact(n);
}
you do not print the result of factorial, then it is not print
why are you using "%6d" rather than "%d" in the scanf ? you do not print so you do not need that
I also encourage you
to add a separator after Give me a number else the input number will seem attached to it (I used a ':' below)
to test the result of scanf
So, for instance :
int main()
{
int n;
printf("Give me a number:");
if (scanf("%d", &n) == 1)
printf("fact(%d)=%d\n", n, fact(n));
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
#include <stdio.h>
#include <conio.h>
void main() {
int a, n, x;
clrscr();
printf("enter a number");
scanf("%d", &a);
n > 1;
a != n && n < a;
if (a / n == x)
printf("a is not a prime no");
else
printf("a is a prime no");
}
If I run this and put a composite number, it still shows it as prime.
your if statement is never true duo to n and x are not initialized. Therefore you only get your else as return. Moreover your expression n>1; and a != n && n < a; return a bool which is not compered to anything. In that case you need to use a for loop.
Here is a link About for loops
int main()
{
int a,n,x = 0;
printf("enter a number: ");
scanf("%d",&a);
for(n=2; n<=a/2; ++n)
{
if(a%n==0)
{
x=1;
break;
}
}
if (x==0)
printf("",n);
else
printf("a is not a prime no");
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();//clearing the screen
int n,x=2,count=0;//Here count is initialised to 0,if it is not prime it remains the same,else it will be equal to 1.You will understand this as you go down
//A number is a prime number if it is not divisible by any other number from 2 and the number before it.
printf("Enter a number : ");
scanf("%d",&n);
while(x<n)//As this checking process should continue till the number just preceding it
{
if(n%x==0)//checking if the number n is divisible by x or not
{
count++;//IF divisible,there is no meaning in continuing,So we are coming out of the loop by incrementing the variable "count"
break;
}
else
x++;
}
if(count==0)
{
printf("%d is a prime number",n);
return 0;//Here if number is prime,There is no need to go further and execute till end,To reduce time complexity ,We will write a return statement to stop executing the code.
}
printf("%d is not a prime number",n);
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to make an array that accepts each number just once and if the user tries to insert a number more than once,then he must enter an other number...can anyone help me please?
I have tried this so far:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int a[5];
int i,j,num;
scanf("%d",&num);
a[0]=num;
for(i=1;i<5;++i){
again: scanf("%d",&num);
if(a[i]!=a[i-1])
a[i]=num;
else
goto again;
}
for(i=0;i<5;i++)
printf("%4d\n",a[i]);
system("pause");
return 0;
}
but the code just doesn't work and i don't know why
it is observed from your code that given array must be filled but it should not contain redundant values.
following code iterates until the array is filled with no redundant value, once the array is filled it terminates.
int a[5],i=1,k=0,p;
int num;
scanf("%d",&num);
a[0]=num;
while(i<5)
{
scanf("%d",&num);
for(p=0;p<=k;p++)
{
if(a[p]==num)
{
break;
}
if(p==(k))
{
a[i]=num;
k=i;
i++;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
hope this could help you
You are just comparing the new entered value with the previous one in a[i]!=a[i-1].
You better create a function to test the new value with the entire array, like
int valueExists(int num, int a[], int len) {
int i;
for (i = 0; i < len; i++) {
if (a[i] == num) {
return 1;
}
}
return 0;
}
Make sure you adding the new value to the array only after testing the value is not there.
again: scanf("%d",&num);
if (valueExists(num, a, i)) {
goto again;
} else {
a[i] = num;
}
(The loop you created with the goto can be replaced by a do-while loop, but that is not the issue here)
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 8 years ago.
Improve this question
I've been asked to write a program that accepts a list of numbers until a non-numeric is encountered (up to 30 numbers), putting the numbers into an array, and keeping track of how many numbers were inserted. Then it should scan through the array to find the largest number, and print the largest.
This is what I've come up with:
#include<stdio.h>
int main()
{
const int INPUT = 30 ;
int size [INPUT];
int i, big;
printf("Type integer numbers, followed by q to quit: ");
while (scanf("%d", &size[INPUT]) != 'q')
{
for(i=0;i<size;i++)
scanf("%d",&INPUT[i]);
big = INPUT[0];
for(i=1;i<size;i++)
{
if(big<INPUT[i])
big=INPUT[i];
}
printf("The largest number is %d",big);
return 0;
}
Besides the problems, I listed in the comments. You seems to be comfused by the varaible names~ Anyway, I made some code for you.
#include<stdio.h>
int main()
{
const int MAX_INPUT = 30 ;
int input[MAX_INPUT];
int size=0, big;
printf("Type integer numbers, followed by q to quit: ");
while(size < MAX_INPUT){
if(scanf("%d", &input[size]) != 1){
break;
}
++size;
}
if(size ==0){
return 0;
}
big = input[size-1];
while( size-- > 0)
{
if(big<input[size]){
big=input[size];
}
}
printf("The largest number is %d\n",big);
return 0;
}
Tested with GCC 4.1.2 and Linux.
Return value of scanf:
Upon successful completion, these functions return the
number of successfully matched and assigned input items
further, you are mixing the size and input, you actually want the size to be a constant and input to be an array:
const int SIZE = 30 ;
int input[SIZE];
So the while loop should look like:
while (scanf("%d", &input[some_index]) == 1)
and of course this is wrong:
scanf("%d",&INPUT[i]); // should be ==> &input[i]