Problem in obtaining output, if statement maybe? - c

#include <stdio.h>
int main()
{
int rev_num=0,x=0,n,a;
printf("Enter a number");
scanf("%d",&n);
for(int i=n+1;i<i+1;i++)
{
a=i;
while(a>0)
{
rev_num=rev_num*10+a%10;
a/=10;
}
for(int j=2;j<i;j++)
{
x=0;
if(i%j==0)
{
x=1;
break;
}
}
if(x==0 && i==rev_num)
{
printf("%d is the next prime palindrome",i);
break;
}
}
return 0;
}
We have to find next prime palindrome of a number, but the code is not giving any o/p, nor any error. It just keeps asking for input indefintely.Why????

Related

Getting negative numbers or zero as output of factorial in C

I've written a very simple program which calculate the factorial of a number, the problem is that from 30 to 60 approximately, it returns a negative number and from 70 it returns 0.
I don't what I've done wrong. Could this problem depend on the computing power of my computer?
Here's the code:
#include <stdio.h>
int main(){
int x, i;
long long int f = 1;
printf("Insert a number:");
scanf("%d", &x);
if (x == 0){
printf("0! = 1");
}
else {
for (i = 1; i <= x; i++){
f *= i;
}
printf("%d! รจ = %lli", x, f);
}
return 0;
}
Here is my code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void Carry(int bit[],int pos)
{
int i,carray=0;
for(i=0;i<=pos;i++)
{
bit[i]+=carray;
if(bit[i]<=9)
{
carray=0;
}
else if(bit[i]>9&&i<pos)
{
carray=bit[i]/10;
bit[i]%=10;
}
else if(bit[i]>9&&i>=pos)
{
while(bit[i]>9)
{
carray=bit[i]/10;
bit[i]%=10;
i++;
bit[i]=carray;
}
}
}
}
int main()
{
int num,pos,digit,i,j,m,n;
double sum=0;
int *fact;
printf("input want to calculute factorial num:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
sum+=log10(i);
}
digit=(int)sum+1;
if(!(fact=(int *)malloc((digit+1)*sizeof(int))))
{
printf("malloc failed\n");
return 0;
}
for(i=0;i<=digit;i++)
{
fact[i]=0;
}
fact[0]=1;
for(i=2;i<=num;i++)
{
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
for(j=0;j<=pos;j++)
{
fact[j]*=i;
}
Carry(fact,pos);
}
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
m=0;
n=0;
for(i=pos;i>=0;i--)
{
printf("%d",fact[i]);
m++;
if(m%4==0)
{
printf(" ");
}
if(m==40)
{
printf("\n");
m=0;
n++;
if(n==10)
{
printf("\n");
n=0;
}
}
}
printf("\n\n");
return 0;
}

unable to find Prime number using Continue control flow

i wrote continue which brings back control to iteration and until the remainder is not zero so output should be printed. but it is not . why ?
#include<stdio.h>
main()
{
int n,i;
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i!=0)
{
continue;
printf("prime\n");
}
}
}
Can you spot the difference?
int main(void)
{
int n,i;
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i!=0)
{
continue;
}
else
{
printf("not prime\n");
return 0;
}
}
printf("prime\n");
}

Prime Generator(spoj)

For exact question see this link
Here I defined three function, called them selves one within another.
Function call is not being done
#include<stdio.h>
int primegen(int x1,int x2);
int isprime(int j);
int main(){
int x,n1,n2,i;
printf("Enter the number of test cases:");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("enter the starting point and ending point:");
scanf("%d %d",&n1,&n2);
primegen(n1,n2);
}
return 0;
}
int primegen(int x1,int x2){
int k;
if(x2>x1){
for(k=x1;k<x2;k++){
if(isprime(k))
{
printf("%d",k);
}
}
return 0;
}
}
int isprime(int j){
int i,c=0;
for (i=1;i<=j;i++)
{
if(j%i==0){
c++;
}
if(c!=2){
return 0;
}
else{
return 1;
}
}
}
Output
There is no output for this code.
Take following outside loop:
if(c!=2){
return 0;
}
else{
return 1;
}
The problem is in your loop in isprime().
In this you are using the value of c when it is still 0. So, c!=2 would result in true and 0 will be returned and you would not get any answer.
Remove this particular statement from the for loop as you need to calculate total no. of divisors.
for (i=1;i<=j;i++)
{
if(j%i==0)
c++;
}
if(c!=2)
return 0;
else
return 1;
Or you can do like this:
int isprime(int j){
int i,k;
k=sqrt(j); //include math.h for this
for(i=2;i<=k;i++)
{
if(j%i==0)
return 1;
}
return 0;
}
You only need to find any divisor up to the square root of the number.
It's better to return 0 if it is found to be divisible instead of using counter and incrementing it.
int isprime(int j)
{
int i;
for(i=2;i<j;i++)
if((j%i)==0)
return 0;
return 1;
}
#include <stdio.h>
#include <math.h>
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
unsigned int low,high,i=0,j=2,k,x=0,y=0,z;
unsigned long int a[200000],b[200000];
scanf("%d",&low);
scanf("%d",&high);
for(i=low;i<=high;i++)
a[x++]=i;
for(i=2;i<=32000;i++)
b[y++]=i;
i=0;
while(b[i]*b[i]<=high)
{
if(b[i]!=0)
{
k=i;
for(;k<y;k+=j)
{
if(k!=i)
{
b[k]=0;
}
}
}
i+=1;j+=1;
}
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=low && b[i]<=sqrt(high)))
printf("%d\n",b[i]);
}
int c=0;
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=1 && b[i]<=sqrt(high)))
b[c++]=b[i];
}
int m=a[0];
for(i=0;i<c;i++)
{
z=(m/b[i])*b[i];k=z-m;
if(k!=0)
k += b[i];
for(;k<x;)
{
if(a[k]!=0)
{
a[k]=0;
}
k+=b[i];
}
}
for(i=0;i<x;i++)
{
if(a[i]!=0 && (a[i]>=2 && a[i]<=(high)))
printf("%d\n",a[i]);
}
printf("\n");
}
return 0;
}

Nptel-pascal programming assignment

i am pursuing the course on programming and data structures in nptel's MOOC.
A programming assignment in the course requires us to calculate sum of selected coefficients.
Now the program i wrote calculated the answer , but i am experiencing a runtime error.
Now i had this inclination of using gets() instead of scanf() so as to speed up my input.
how do i do that?
the code is as follows:
#include<stdio.h>
int combi(int ,int);
long fact(int);
int main()
{
int r,i,v[20],p[20],t,l=1,b=0,sum=0,a=0,flag=0;
char ch='a';
scanf("%d",&r);
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&v[0]);
if(getchar()==' ')
{
if(r<v[0])
{
while(ch!='\n')
{
scanf("%d",&v[l]);
l++;
if(getchar()=='\n')
{
p[a]=-1;
a++;
l=1;
break;
}
}
}
else
{
while(ch!='\n')
{
scanf("%d",&v[l]);
if(v[l]>v[0])
{
flag=1;
}
l++;
if(getchar()=='\n')
{
if(flag>0)
{
p[a]=-1;
a++;
l=1;
sum=0;
flag=0;
break;
}
else
{
for(b=1;b<l;b++)
{
sum=sum+combi(v[0],v[b]);
}
p[a]=sum;
a++;
sum=0;
l=1;
break;
}
}
}
}
}
}
for(i=0;i<t;i++)
{
printf("%d\t ",p[i]);
}
printf("\n");
}
int combi(int x,int y)
{
int a=fact(x)/(fact(x-y)*fact(y));
return a;
}
long fact(int z)
{
int i=1;
long f=1;
while(i<=z)
{
f=f*i;
i++;
}
return f;
}

Pattern Using for loop

I have to print the following pattern using for loop only.
I have written the following code which gives me till the the numbers increment. How can I decrement then in the other half.
#include<stdio.h>
int main(void)
{
int x,r;
for(r=1;r<5;r++)
{
for(x=3;x>=r;x--)
{
printf(" ");
}
for(x=1;x<=r;x++)
{
printf("%d",x);
}
printf("\n");
}
return 0;
}
just add a second loop from r-1 to 1?
for (x = r - 1; x > 0; x--)
{
printf("%d",x);
}
Added one more loop
for(x=1;x<=(r-1);x++)
{
printf("%d",x);
}
whole code.
#include<stdio.h>
int main(void)
{
int x,r;
for(r=1;r<5;r++)
{
for(x=3;x>=r;x--)
{
printf(" ");
}
for(x=1;x<=r;x++)
{
printf("%d",x);
}
for(x=1;x<=(r-1);x++)
{
printf("%d",x);
}
printf("\n");
}
return 0;}
code running at http://codepad.org/oAdx20ai

Resources