#include<stdio.h>
#include<conio.h>
/* Function Declaration
int pal(int x); */
/*Declaring Second Function*/
int rev(int x);
int main()
{
int a, b, c;
clrscr();
printf("Enter The Number Which You Want To Check Is Palindrome Or Not\n");
scanf("%d", &a);
b = rev(a);
printf("%d", b);
if(b == a) {
printf("%d Is A Palindrome Number", a);
} else {
printf("%d Isn't A Plaindrome Number", a);
}
getch();
return(0);
}
int rev(int x)
{
int d = 0;
while(x) {
d = d * 10 + x % 10;
x = x / 10;
}
return(d);
}
I didn't get the use of while(x) statement. I mean, we attach some condition with while loop i.e. while(x!=0), so what does standalone while(x) means.
while (x) is the same as while (x != 0)
For an integral data type, 0 is false & everything else is true. So while (x) would evaluate to while(true) for all x != 0.
Similarly, you will also come across expressions like while(!x) or if(!x)
If x has value a non zero value, then x is true & !x is false.
If x has value 0, then x is false & !x is true.
So writing (!x) is the same as writing (x == 0)
You will also see similar usage with pointers. For a pointer p, (p) is the same as (p != NULL) and (!p) is the same as (p == NULL).
Related
I've made a program that checks if a given positive integer is a prime or perfect number.The problem I'm facing is I created a function "readNumber" that works as a check loop to ensure that input is a positive integer.But if I enter a negative value and then an acceptable one it shows previous values aswell.I attach a screenshot of the command prompt text to make myself more clear.
Below is my code
#include<stdio.h>
int checkperfectnumber(int);
int checkprimenumber(int);
int readNumber(int);
int main(){
int num, x, y, result;
printf("\nGive a positive integer number: \n");
scanf("%d",&num);
y = readNumber(num);
x = checkperfectnumber(num);
result = checkprimenumber(num);
if (num == 1)
printf("1 is nor a prime neither a perfect number");
else if (x == num)
printf("%d is a perfect number\n",num);
else if ( result == 1 )
printf("%d is a prime number.\n", num);
else
printf("%d is nor prime neither a perfect number.\n", num);
return 0;
}
//perfect number function
int checkperfectnumber(int numbr){
int a=1, sum=0;
while(a < numbr){
if(numbr % a == 0)
sum=sum+a;
a++;
}
return(sum);
}
//prime number function
int checkprimenumber(int a)
{
int c;
for ( c = 2 ; c <= a - 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
return 1;
}
//input check function
int readNumber(int b){
while (b < 0)
{
printf("Wrong input.\nPlease insert a positive integer.");
main();
break;
}
}
Quick fix is have the program exit after executing main() from readNumber().
To do this, Add #include <stdlib.h> and replace break; in the function readNumber() to exit(0);.
Better solution is having the function readNumber(), not main(), read numbers and stop calling main() recursively.
It will be like this:
#include<stdio.h>
int checkperfectnumber(int);
int checkprimenumber(int);
int readNumber(void);
int main(){
int num, x, y, result;
printf("\nGive a positive integer number: \n");
num = readNumber();
x = checkperfectnumber(num);
result = checkprimenumber(num);
if (num == 1)
printf("1 is nor a prime neither a perfect number");
else if (x == num)
printf("%d is a perfect number\n",num);
else if ( result == 1 )
printf("%d is a prime number.\n", num);
else
printf("%d is nor prime neither a perfect number.\n", num);
return 0;
}
//perfect number function
int checkperfectnumber(int numbr){
int a=1, sum=0;
while(a < numbr){
if(numbr % a == 0)
sum=sum+a;
a++;
}
return(sum);
}
//prime number function
int checkprimenumber(int a)
{
int c;
for ( c = 2 ; c <= a - 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
return 1;
}
//input check function
int readNumber(void){
for (;;)
{
int b;
scanf("%d",&b);
if (b >= 0) return b; /* 0 is not positive, but this condition is !(b < 0) */
printf("Wrong input.\nPlease insert a positive integer.");
}
}
This question already has answers here:
Input too big for array
(4 answers)
Closed 2 years ago.
When I use sscanf, I found some other variable is changed as well but I don't know why.
My code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int mul(int a, int b) {
return a * b;
}
int d(int a, int b) {
return a / b;
}
void manipulate(int* result, int x, int y, int op(int, int)) {
printf("%p\n", result);
*result = op(x, y);
}
int main() {
char line[20];
FILE* f = fopen("sample.txt", "r");
int result = 0;
while (fgets(line, 15, f) != NULL) {
char oper[3];
char valA[3];
char valB[3];
sscanf(&line[0], "%s %s %s", oper, valA, valB);
int x;
int y;
printf("%d\n", result);
if (valA[0] == '%') {
x = result;
y = atoi(valB);
} else if (valB[0] == '%') {
x = atoi(valA);
y = result;
} else {
x = atoi(valA);
y = atoi(valB);
}
printf("%d %d\n", x, y);
if (strcmp(oper, "ADD") == 0) {
manipulate(&result, x, y, &add);
} else if (strcmp(oper, "SUB") == 0) {
manipulate(&result, x, y, &sub);
} else if (strcmp(oper, "MUL") == 0) {
manipulate(&result, x, y, &mul);
} else if (strcmp(oper, "DIV") == 0) {
manipulate(&result, x, y, &d);
}
}
}
I used the debugger to track the value of the variable named result. In the first loop, the result becomes the value I want, but when it entre the second loop and finish the line sscanf, the result becomes 0 again. Could anyone tell me why? Thank you so much :)
Your char arrays are too small, because you didn't account for null terminators. The string ADD requires 4 characters ('A', 'D', 'D', and '\0'), but you're storing it in char oper[3], and writing past the end is Undefined Behavior. In this case, that happens to mean that the '\0' gets stored on top of result, setting it to 0.
In this program I keep getting a floating point exception at the end. There are two main files I am working with. The first is the "main" listed below:
int main(){
int ans;
do{
printf("Enter an integer greater than 1:\n");
scanf("%d", &ans);
}while(ans <= 1);
printf("%d = ", ans);
int d = 2;
while(ans >= d){
if(ans == d){
printf("%d ^ %d", d, factor_power(ans, d));
ans = ans / (d ^ (factor_power(ans, d)));
}
else{
printf("%d ^ %d * ", d , factor_power(ans , d));
ans = ans/(d ^ (factor_power(ans, d)));
d++;
}
}
printf("\n");
return 0;
}
The file that contains the factor_power() method is here:
int factor_power(int n, int d){
int p = 1;
do{
if( n % (d ^ p) == 0)
p ++;
}while(n % (d^(p+1)) == 0);
return p;
}
both include my header, math.h, and stdio.h. I am just so lost on where the floating point exception is coming from. The program is supposed to print out the prime factors like:
1200 = 2^4 * 3^1 * 5*2.
Amy feedback is much appreciated.
Try following:
int ipow(int base, int exp)
{
int power = 1;
while (exp)
{
if (exp & 1)
power *= base;
exp >>= 1;
base *= base;
}
return power;
}
int factor_power(int n, int d){
int p = 1;
do{
if( (n % ipow(d,p)) == 0)
p++;
} while((n % ipow(d,(p+1))) == 0);
return p;
}
You may have to include "math.h" if u already hadn't.
Hope it helps!
I want to write a power function that prints "Unable to compute o^o" when asked to do so or return the integer result. how can I accomplish this?
My current code prints the error statement as well as the result statement.
My code:
#include <stdio.h>
double power(int base,int n);
int main() {
int no1,no2;
printf("Enter two numbers:\n");
printf("If you want to compute x^y enter x y\n");
scanf("%i%i", &no1, &no2);
printf("The value of %i^%i is %f", no1, no2, power(no1,no2));
return 0;
}
double power(int base, int n) {
double result = 1;
if( n == 0 && base == 0){
printf("Unable to compute 0^0\n");
}
else if( n == 0 && base != 0) {
result = 1;
}
else if( n>0 ){
for(n ; n>0 ; n--) {
result = result*base;
}
}
else if( n<0 ){
int temp = -n;
result = power(base,temp);
result = (float)1.0/result;
}
return result;
}
EDIT: I am actually a novice. I am in the first chapter of K&R where I found a power function. I wanted to improve that thing and found this hurdle. Hence please provide resources if possible so that I understand your answers.
#BartoszMarcinkowski gives you the correct answer, as an alternative (if you can't pass an extra variable) you can return NAN and check the result with isnan().
In computing, NaN, standing for not a number, is a numeric data type
value representing an undefined or unrepresentable value, especially
in floating-point calculations.
#include <stdio.h>
#include <math.h>
double power(int base,int n);
int main(void)
{
double f;
f = power(2, 2);
if (isnan(f)) {
printf("Unable to compute power\n");
} else {
printf("%f\n", f);
}
return 0;
}
double power(int base, int n) {
double result = 1;
if( n == 0 && base == 0){
return NAN;
}
...
return result;
}
It is common for C functions to return 0 on success or error code in case of failure.
#include <stdio.h>
int power(int base,int n, double *result);
int main() {
int no1, no2;
printf("Enter two numbers:\n");
printf("If you want to compute x^y enter x y\n");
scanf("%i%i", &no1, &no2);
double result;
int error = power(no1, no2, &result);
if(error == 0)
printf("The value of %i^%i is %f\n", no1, no2, result);
return 0;
}
int power(int base, int n, double *result) {
*result = 1;
if(n == 0 && base == 0){
printf("Unable to compute 0^0\n");
return 1;
} else if(n == 0 && base != 0) {
*result = 1;
return 0;
} else if(n>0){
for(; n>0 ; n--) {
*result = *result*base;
}
return 0;
} else if(n < 0){
int temp = -n;
power(base,temp, result);
*result = (float) 1.0 / *result;
return 0;
}
return 1;
}
Note : My answer is in reference to the user's post saying that he is
a novice at learning C, so I have assumed that he might not have yet
be comfortable dealing with pointers.
You can have an error flag in your code that tells the main function that an error has occurred.
You can use a global integer, that is common to all functions of your program to inform of errors in code.
After calling the power function, you check whether the error FLAG is set, which means errors have occurred. So you print the error message instead of the result.
What value you should use for showing a error has occurred is arbitrary. In most cases, a non-zero (sometimes negative) value can indicate the specific error. For example:
FLAG = -1 might indicate invalid input
FLAG = -2 might indicate invalid operation
And so on. etc. Here I have chosen FLAG = non-zero value to mean error has occurred.
#include <stdio.h>
double power(int base,int n);
--> char FLAG = 0;
int main() {
int no1,no2;
---- your code ---
double and = power(no1,no2);
--> if (char) {
printf("error : both values cannot be negative);
char = 0;
} else {
printf("The value of %i^%i is %f", no1, no2, ans);
}
return 0;
}
double power(int base, int n) {
---- your code ---
if( n == 0 && base == 0){
printf("Unable to compute 0^0\n");
--> FLAG = -1;
return 0;
}
else if( n == 0 && base != 0) {
result = 1;
}
else ---- your code ---
}
I am returning values 1 or 0 from function isprime(0 when it is not prime and 1 when it is prime) but when i print the returned value of x(return value of isprime) it is not same as what I returned from isprime. Why?
#include<stdio.h>
int isprime(int b);
main()
{
int a,rem,i;
printf("enter the number");
scanf("%d", &a);
for(i = 1; i < a; i++)
{
rem = a % i;
if(rem == 0)
{
int x = isprime(i);
printf(" value of x returned for i = %d is %d", i, x);
if(x = 1)
{
printf("%d\n", i);
}
}
}
return (0);
}
/**
*
*returns 1 if b is prime else returns 0
*/
int isprime(int b)
{
int x, count = 0;
printf("input recieved %d \n", b);
for(x = 1; x <= b; x++)
{
if (b % x == 0)
{
count = count + 1;
}
printf("the value of count is %d\n", count);
}
if(count == 2) {
printf("returning the value as 1\n");
return 1;
}
else {
printf("returning the value as 0\n");
return 0;
}
}
if(x = 1)
= is assignment. You need == operator instead. You are doing correct in other if conditions though.
Also, the logic of calculating prime numbers is inefficient. You can break the loop once the count is greater than 2.
if (b % x == 0)
{
count = count + 1;
if (count > 2)
{
// This ensures you are returning correct value later.
break;
}
}
Have a look at this algorithm: Sieve of Eratosthenes
This answer is correct.
For removing such kind of mistakes use it like
if(1=x)
using this approach you can avoid such behavior.
Here I am just approaching to avoid typo mistakes.