I wrote the following code, but it is not giving the correct output. For input n = 5, the output should be 4 6 7 9 10, but my output is 4 5 6 7 8. Can anyone help me out? Where am I going wrong?
#include<stdio.h>
int isFibo(int n){
if(n<=0)
return 1;
if(n==1)
return 1;
if(n==2)
return 1;
int t1=0;
int t2=1,t3;
//int i=1;
while(t3<n){
t3=t1+t2;
t1=t2;
t2=t3;
if(t3==n)
return 1;
}
return 0;
}
void printNonFibo(int n){
int i=0,count=0;
while(count<n){
if(!isFibo(i))
{
printf("\nNon Fibo: %d ",i);
count++;
}
i++;
}
}
int main(){
int n;
printf("Enter n: ");
scanf("%d",&n);
printNonFibo(n);
return 0;
}
Related
I need help with the program to display first 4 perfect numbers in the standard output and also funciton perfect(int,int*). Arguments of this function are natural number and the adress where you neeed to write the adders (of the perfect number I suppose). Function has to return 1 if the number is perfect, and 0 if it's not. This is what I've done so far. Help please.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int perfect(int,int*);
int main()
{
int *arr,a;
int i,j;
perfect(a,arr);
}
int perfect(int n,int *arr)
{
int lim=8128,i,sum;
for(n=1;n<=lim;n++)
{
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(n==sum)
printf("%d ",n);
}
}
I think this code is helpful for you. The perfect function returns 1 when perfect otherwise return 0. The global array divisor which is used for collecting the addr. When the perfect function returns 1 then I print the divisor array and initiate the deivisor_count =0. Please have a look:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int divisor[1000], deivisor_count = 0;
int perfect(int n)
{
int sum=0, i, j = 0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
divisor[deivisor_count]= i;
deivisor_count = deivisor_count + 1;
}
}
if(n==sum){
return 1;
}
return 0;
}
int main()
{
int i, j =0, is_perfact, n=100000, k;
for (i =2 ; i<=n; i++){
deivisor_count = 0;
is_perfact = perfect(i);
if(is_perfact == 1){
j = j + 1;
for(k = 0; k <deivisor_count; k++){
printf("%d", divisor[k]);
if (deivisor_count -1 == k){
printf("=");
}
else{
printf("+");
}
}
printf("%d\n", i);
}
if (j==4){
break;
}
}
return 0;
}
#include <stdio.h>
int main(){
int i,j;
int flag = 0;
int x [3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for (i = 0;i<3;i++){
if (flag ==1){
for (j=2;j<0;j--){
printf(" %d",x[i][j]);
}
flag =0;
}
else {
for (j=0;j<3;j++)
{
printf(" %d ",x[i][j]);
}
flag =1;
}
}
return 0;
}
i'm trying to print the numbers in the array in a zigzag form the expected output should be 123654789 but all i got 123789 for some reason i dont enter the loop in the flag condition i want to know the reason.. thanks in advance
Instead of
for (j=2;j<0;j--){
use
for (j=2;j>-1;j--){
and (only for the nice formatting) instead of
printf(" %d ",x[i][j]);
(near the end, with a space after %d) use
printf(" %d",x[i][j]);
(without that space).
Try this, it will give you the output what you want.
#include <stdio.h>
int main(){
int i,j;
int flag = 0;
int x [3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for (i = 0;i<3;i++){
if (flag ==1){
for (j=2;!(j<0);j--){
printf(" %d",x[i][j]);
}
flag =0;
} else {
for (j=0;j<3;j++) {
printf(" %d",x[i][j]);
}
flag =1;
}
}
return 0;
}
Output:
./a.out
1 2 3 6 5 4 7 8 9
I have to write a C program that converts decimal to binary using numbers between 0 to 255. using 3 functions and no global variables. When I get it to run it just prints all zeros for some reason. This is what I have so far:
#include <stdio.h>
int getNumber();
int dectoBin(int, int binarray[], int);
void printBin(int binary[], int dec);
int main(){
int M = 7;
int binarray[M];
int dec = getNumber();
printf("The decimal number you entered was: %d", dec);
*binarray = decToBin(dec, binarray, M);
printBin(binarray, dec);
return 0;
}
int getNumber(){
int dec;
printf("Enter any a number between 0 and 255: ");
scanf("%d",&dec);
return 0;
}
int decToBin(int dec, int binarray[], int M){
int i, j;
if(dec>255)
printf("Please enter a number between 0 and 255");
else
for(i=7; i>=0;i--){
j = dec >>i;
if(j & 1){
binarray[i] = 1;
}
else {
binarray[i] = 0;
}
}
return *binarray;
}
void printBin(int binary[], int dec){
int i;
if(dec > 255){
printf("PLese use another number");
main();
}
else
{
for(i =7; i > 0;i--){
printf("%d", binary[i]);
printf("\n");
}
}
}
In your function:
int getNumber(){
int dec;
printf("Enter any a number between 0 and 255: ");
scanf("%d",&dec);
return 0;
}
You return 0, you should return dec:
int getNumber(){
int dec;
printf("Enter any a number between 0 and 255: ");
scanf("%d",&dec);
return dec;
}
In printBin you only prints 7 bits instead of 8 bits.
Change for(i =7; i > 0;i--){ to for(i = 7; i >= 0; i--){
And in main change: int M = 7; to int M = 8;. You need 8 bits for the range 0..255.
*binarray = decToBin(dec, binarray, M); is plain wrong and not necessary, write decToBin(dec, binarray, M) ;. The decToBin function takes already care of filling the binarray array.
Otherwise the architecture of your program is poor:
The test if the input is between 0 and 255 should be in getNumber or
in main, but not in printBin.
The call to main() inside the printBin function is completely
wrong, even if the program works correctly.
I leave this as an exercise to the reader to find out why.
Factorial number is a number that is multiplied by it's previous numbers. For example it's 5. 1*2*3*4*5 is it's factorial number.
I already made a program which prints factorial of any number, but i don't know how to make it to print N first factorial number in c.
For example i type 10. It must show first 10 numbers along with their factorials (Making a table)
Here is what i was made to print factorial of any number.Is there any possibility to do with while/ if else statements/ and for loop?
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
printf("Factorial of %d js %d\n", n, fakt);
getch();
}
You probably want this:
Program:
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i=1;i<= n;i++) //use braces to write more than one statement inside the loop
{
fakt=fakt*i;
printf("Factorial of %d is %d\n", i, fakt);
}
getch();
}
Output:
Enter a number:
5
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
#include <conio.h>
#include <stdio.h>
void main()
{
int f=1,i,v;
clrscr();
printf("Enter the number :");
scanf("%d",&v);
for(i=1;i<=v;i++)
{
f=f*i;
printf("num =%d and fac=%d\n",i,f);
}
getch();
}
this code will work
That code already uses the for loop. The while loop equivalent is:
i = 1;
while (i <= n) {
fakt = fakt*i;
i++;
}
#include <stdio.h>
int factorial(int n)
{
int i,fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
return fakt;
}
int main()
{
int n;
printf("Enter a number:\n");
scanf("%d", &n);
int i = 0;
for(i=1;i<=n;i++)
{
printf("Factorial for %d is %d\n",i,factorial(i));
}
return 0;
}
I think this will do the job just fine.
You can do a nested loop.
Run the parent loop from 1 to n,
and the nested loop will be your already working for loop.
You may want this:
#include <stdio.h>
int main()
{
int n, i, num, factorial;
printf("Enter the number of terms: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
num = i;
factorial = 1;
while(num)
factorial *= num--;
printf("%d \t %d\n", i, factorial);
}
return 0;
}
Output:
Enter the number of terms: 10
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Use this fastest version of factorial using recursion with if ..else statement
#include<stdio.h>
int fact(int n);
void main()
{
int n;
printf("\nEnter an integer:");
scanf("%d",&n);
fact(n);
}
int fact(int n)
{
int a;
if(n==0)
{
printf("The Factorial of 0 is 1\n");
return 1;
}
else
{
a=n*fact(n-1);
printf("The Factorial of %d is %d\n",n,a);
return a;
}
}
#include<stdio.h>
int main(int n){
int fact;
clrscr();
printf("Enter a number and type exit:\n");
scanf("%d",&n);
if(n!=0){
fact=n*main(n-1);
printf("Factorial of %d is %d\n",n,fact);
getch();
return fact;
}
else{
printf("Factorial of 0 is 1.\n");
getch();
return 1;
}
}
Here's the problem statement:
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
My code gives me a partial output. Only 5 or 6 of the eleven required primes are being outputted, 3797 not being one of them. So to find the error, I manually (on a piece of paper) ran the code for 3797 and somehow can't manage to find the glitch.
I think the error is in the second part, the part of code which checks whether the number is truncatable from the left.
Code:
#include<stdio.h>
int isprime(int n) //Checks whether the number is prime or not
{
int i;
if(n==1)
return(0);
for(i=2;i<n/2+1;i++)
{
if(n%i==0)
{
return(0);
break;
}
}
return(1);
}
int main(void)
{
int count=0,z=0;
int i;
int n;
int x=1;
int reverse2=0;
int z1=0;
int p;
int count1=0;
int digit;
int k=1000000;
int reverse=0;
for(i=2;i<k;i++)
{
if(isprime(i)==1)
{
n=i;
p=i;
while(n>0) // This function removes the digits of the prime number from the right
{
n=n/10;
if(isprime(n)==1)
{
count++;
}
z++;
}
if(z==count)
{
while(p>0) //Checks whether number is left truncatable
{
digit=p%10;
p=p/10;
if(z1==0)
{
reverse=digit;//here reverse doesn't refer to reversing the number. It builds the number one digit at a time from right to left.
}
else
{
reverse=digit*x*10+reverse;
x++;
}
if(isprime(reverse)==1)
{
count1++;
}
z1++;
}
if(z1==count1)
printf("%d ",i);
}
z=0;
z1=0;
count1=0;
count=0;
reverse=0;
reverse2=0;
x=1;
}
}
}
Your left truncatable check is wrong. I did it differently, simpler.
#include<stdio.h>
int isprime(int n) //Checks whether the number is prime or not
{
int i;
if(n==1)
return(0);
for(i=2;i<n/2+1;i++)
{
if(n%i==0)
{
return(0);
break;
}
}
return(1);
}
int power(int a, int b){
int r = 1;
int i=0;
for (i=0;i<b;i++){
r = r * a;
}
return r;
}
int main(void)
{
int count=0,z=0;
int i;
int n;
int z1=0;
int p;
int count1=0;
int digits;
int k=1000000;
for(i=2;i<k;i++)
{
if(isprime(i)==1)
{
z = 0;
count = 0;
n=i;
p=i;
while(n>0) // This function removes the digits of the prime number from the right
{
n=n/10;
if(isprime(n)==1)
{
count++;
}else{
count = -1;
break;
}
z++;
}
if(z==count)
{
z1= 0;
count1=0;
n = i;
p= i;
while(p>0) //Checks whether number is left truncatable
{
digits=n%power(10,z1+1);
p = p /10;
if (isprime(digits)==1)
{
count1++;
}else{
count1 =-1;
break;
}
z1++;
}
if(z1==count1)
printf("%d\n ",i);
}
}
}
}