palindrome number - compiler neither compiling code nor showing any error - c

can anyone help me this c program.i am trying to execute this palindrome check code but its not getting executed after i enter number.is there any error?
#include<stdio.h>
int main()
{
int num,rev=0,r,temp;
printf("enter the number: ");
scanf("%d",&num);
temp=num;
while(num>0)
{
r=num%10;
rev=(rev*10)+r;
temp=temp/10;
}
if(num==rev)
{
printf("the number is palindrome %d: ",temp);
}
else
{
printf("%d is not a palindrome",temp);
}
return 0;
}
the block shows nothing, neither it stops executing.i tried it in code block and some online websites.

I believe you have an infinite loop here as num does not change within the loop, so the outcome of num>0 never changes:
while(num>0)
{
r=num%10;
rev=(rev*10)+r;
temp=temp/10;
}

#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
i still don't get this issue but its working perfectly.

Related

How to take input multiple times even after the execution of program in C?

so I was writing this program to find the number of times a digit is repeated in a number.How can I ask the user to enter inputs again and again even after the execution at the first time
#include <stdio.h>
int main()
{
int rem,flag,n;
int frequency[10]={0};
printf("enter a number");
scanf("%d",&n);
while(n>0){
rem=n%10;
frequency[rem]++;
n=n/10;
}
for(int i=0;i<10;i++)
{
if(frequency[i]>1){
flag=1;
printf("%d is repeated %d times",i,frequency[i]);
}
}
if(flag==0){
printf("no number is repeated");
}
return 0;
}
One way to do this would be to wrap your code in a while (1) loop (loop forever). I've added an extra check: if a user enters the number 0, the program will break out of the loop and exit. There has to be some way to stop.
#include <stdio.h>
int main()
{
while (1){
int rem,flag=0,n;
int frequency[10]={0};
printf("enter a number");
scanf("%d",&n);
if (n==0){
break;
}
while(n>0){
rem=n%10;
frequency[rem]++;
n=n/10;
}
for(int i=0;i<10;i++)
{
if(frequency[i]>1){
flag=1;
printf("%d is repeated %d times",i,frequency[i]);
}
}
if(flag==0){
printf("no number is repeated");
}
}
return 0;
}
For a cleaner solution, you may want to create a function for everything within the while loop, and call that function.
Side note: flag has to be set to 0 at the start of the loop.

My C program is not giving desired output

I am learner and new to C. I am making a number guess game using C but it is not executing properly, my if statement is not getting executed in this code please help out.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int num, guess, count;
count = 1;
srand(time(0));
num = (rand()%10 + 1);
do
{
printf("Guess the number: \n");
scanf("%d", guess);
if(guess>num)
{
printf("too large");
}
else if(guess<num)
{
printf("too small");
}
else
{
printf("you won!\nIn %d count.", count);
}
count++;
}while(guess != num);
return 0;
}
Code was supposed to give output as
Guess the number
5
too small!
Guess the number
7
You won in 2 count
But it is not executing if else statement and breaking the loop after scanf function. Please help.
Your scanf is incorrect:
scanf("%d", guess); // should be scanf("%d", &guess);
There is no problem with your if-statement. The problem is that scanf() takes a format string and a pointer, but not a format and a non-pointer variable.

My C code for a genetic algortihtm is not functioning, it dosent enter into the if condition

This is GA for a timetable problem. I'm trying to create an initial population, but it isn't working as it isn't entering the if condition. can someone point out the error?
I tried inserting statements in each condition, but everything checks out. Still, I don't seem to find a solution.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
int random_number_creator(int upper, int lower)
{
int n;
n = rand() % (upper-lower)+ lower;
return n;
}
struct pop{
int subjects[6];
int days[5][9];
}oldpop, bench;
main()
{
int i,j,s=1,d,h,stop=1,cou=0;
for(i=0;i<5;i++)
{
for(j=0;j<9;j++)
if(j!=6)
bench.days[i][j]=0;
else
bench.days[i][j]=11111;
}
for(i=0;i<6;i++)
{
if(i<4)
oldpop.subjects[i]=3;
else
oldpop.subjects[i]=2;
}
for(i=0;i<5;i++)
{
printf("\n");
for(j=0;j<9;j++)
printf(" %d",bench.days[i][j]);
}
for(i=0;i<6;i++)
{
printf(" \n %d",oldpop.subjects[i]);
}
cou=0;
for(i=0;i<6;i++)
{
cou=cou+ oldpop.subjects[i];
}
printf("\n%d\n",cou);
do
{
s=random_number_creator(5,0);
printf("\nsubject number:%d\n",s);
printf("\nloop 1 entery %d",cou);
do
{
printf("\nloop 2 entry\n");
d=random_number_creator(5,0);h=random_number_creator(8,0);
printf("\nDay Number:%d \nHour Number:%d\n",d,h);
if(bench.days[d][h]==0&&oldpop.subjects[s]!=0)
{
printf("\nif condition reached\n");
oldpop.days[d][h]=10+s;
bench.days[d][h]=11111;
stop=0;
cou--;
oldpop.subjects[s]--;
}
else
{
printf("\nIf condition not satisified.\n");
break;
}
}while(stop!=0);
}while(cou!=0);
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
}
I want the oldpop variable to be initialized for this timetable problem but the code does not enter the if condition in the do while loop.
The problem in your program comes from the subject selection (after adding the missing }):
s=random_number_creator(5,0);
Will return a random number between 0 and 4 included.
To correct this, just replace this line by
s=random_number_creator(7,0);
To pick a number between 0 and 6. So the cou variable will be able to reach 0
Your code can be improved:
Instead of this kind of block:
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
Create a function, learn to use printf:
void print_table(struct pop pop, const char *label)
{
int i, j;
printf("\n%s\n", label);
for (i=0;i<5;i++)
{
for (j=0;j<9;j++)
{
printf(" %5d",pop.days[i][j]);
}
}
}
And use it this way
print_table(oldpop, "oldpop");

Can't seem to find why my Prime Number Program doesn't work properly after reprompt for number

Im having trouble with my prime number section part of my program. When I compile and run my program, the prime numbers print out fine for the first number, but when reprompted for another number. It usually prints out either no numbers or it cuts off the prime numbers from a certain point down. Im new to coding and this forum so I'm sorry about any formatting issues in my post.
#include <stdio.h>
#include <math.h>
int main(void)
{
int number, n=1;
long factorial=1;
int a=1, b=0,c;
int q=2, r=2, w=0;
int prime, count;
printf("Enter a Number:\n");
scanf("\n%d", &number);
while(number!=1000)
{
if(number<1||number>1000)
printf("Input is Invalid\n");
else
{
if(number==1000)
printf("Goodbye\n");
if(number<15)
{
factorial=number;
n=number-1;
while(n>=1)
{
factorial=factorial*n;
n--;
}
printf("The Factorial of %d is: %ld\n", number, factorial);
}
c=a+b;
printf("Fibonacci Sequence up to %d\n ", number);
while(c<number&&a+b<number)
{
c=a+b;
a=b;
b=c;
n++;
printf("%d\t", c);
count=n;
if(count%10==0)
printf("\n");
}
printf("\nTotal:%d\n", n);
printf("\nPrime numbers up to %d:\n", number);
while(q<=number)
{
prime=0;
for(r=2;r<q;++r)
{
if(q%r==0)
{
prime=1;
}
}
if(prime==0)
{
printf("%d\t", r);
w++;
}
q++;
}
count=r;
if(count%10==0)
printf("\n");
printf("\nTotal:%d\n", w);
}
printf("\nEnter a Number:\n");
scanf("\n%d", &number);
a=1;
b=0;
}
return(0);
}
You are never resetting your variable q, which is the control variable for your prime number loop. It is best practice to create all of your variables which need to be reset inside your loop. Either reset q at the beginning/end of your loop, or create q at the beginning of your outer loop and set it to 2.
Like this:
while(number != 1000) {
int q = 2;
//other assignments below which need to be reset
...
//all other code below
...
}

Kattis - Odd Man Out - Segmentation Fault

Please Note that this problem is for homework.
With the following code, I am inputting the data on the Kattis website for this problem. The code compiles, however, I'm getting a segmentation fault after the 'Number of Partygoers #:' printf, and I'm not sure why. Please help!
In Kattis, the description of the problem is basically: There are a number of party goers, you realize someone isn't supposed to be here because you sent all of the invitations to couples. Each party goer has a code on their invitation that matches another's. Find the odd man out.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N,i,n,j,a,count=0,num=1;
int guest[1000],cas[1000];
scanf("%d",&N);
printf("Initial N: %dn",N);
while(N!=0)
{
printf("While #: %dn",N);
scanf("%d",&n);
printf("Number of Partygoers: %dn",n);
for(i=0;i<n;i++)
{
scanf("%d",guest[i]);
printf("Goer's Id: %dn",i);
guest[i]=cas[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(cas[i]==guest[j])
{
printf("Is Equaln");
if(count==2)
{
break;
i++;
j=0;
printf("Count Equaln");
}
count++;
j++;
}
else
{
printf("Case #%d: %dn",num,cas[i]);
}
}
}
N--;
num++;
}
return 0;
}
scanf is expecting a pointer and you are passing a value.
Change
scanf("%d",guest[i]);
to
scanf("%d",&guest[i]);
There is another problem, you get a value with scanf but then you overwrite this value with an uninitialized value (garbage):
for(i=0;i<n;i++){
scanf("%d",&guest[i]);
printf("Goer's Id: %d\n",i);
guest[i]=cas[i]; /* cas[i] is used uninitialized */
}
Do you mean cas[i]=guest[i];?

Resources