Fibonacci series in c

Fibonacci series in c

Fibonacci series in c programming: c program for Fibonacci series without and with recursion. Using the code below you can print as many numbers of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science.

Fibonacci series in c using for loop

/* Fibonacci Series c language */
#include<stdio.h>
 
int main()
{
   int n, first = 0, second = 1, next, c;
 
   printf("Enter the number of terms\n");
   scanf("%d",&n);
 
   printf("First %d terms of Fibonacci series are :-\n",n);
 
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
 
   return 0;
}
Output of program:
Fibonacci series program

Fibonacci series program in c using recursion

#include<stdio.h>
 
int Fibonacci(int);
 
main()
{
   int n, i = 0, c;
 
   scanf("%d",&n);
 
   printf("Fibonacci series\n");
 
   for ( c = 1 ; c <= n ; c++ )
   {
      printf("%d\n", Fibonacci(i));
      i++; 
   }
 
   return 0;
}
 
int Fibonacci(int n)
{
   if ( n == 0 )
      return 0;
   else if ( n == 1 )
      return 1;
   else
      return ( Fibonacci(n-1) + Fibonacci(n-2) );
} 
Recursion method is less efficient as it involves function calls which uses stack, also there are chances of stack overflow if function is called frequently for calculating larger Fibonacci numbers.

C program to generate and print armstrong numbers

C program to generate and print armstrong numbers

Armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.

C programming code

#include <stdio.h>
 
int main()
{
   int r;
   long number = 0, c, sum = 0, temp;
 
   printf("Enter an integer upto which you want to find armstrong numbers\n");
   scanf("%ld",&number);
 
   printf("Following armstrong numbers are found from 1 to %ld\n",number);
 
   for( c = 1 ; c <= number ; c++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }
      if ( c == sum )
         printf("%ld\n", c);
      sum = 0;
   }
 
   return 0;
}
Download Generate Armstrong numbers program.
Output of program:
Generate armstrong numbers c program

Armstrong number c program

Armstrong number c program

Armstrong number c program: c programming code to check whether a number is armstrong or not. A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.

C programming code

#include <stdio.h>
 
int main()
{
   int number, sum = 0, temp, remainder;
 
   printf("Enter an integer\n");
   scanf("%d",&number);
 
   temp = number;
 
   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;
   }
 
   if ( number == sum )
      printf("Entered number is an armstrong number.\n");
   else
      printf("Entered number is not an armstrong number.\n");
 
   return 0;
}
Download Check Armstrong number program.
Output of program:
Check armstrong number c program

Bytelandian gold coins

COINS
#include<iostream>
#include<map>

typedef unsigned long long int int64;
using namespace std;
map<int64,int64> a;

int64 max(int64 x,int64 y)
{
      return x>y?x:y;
}


int64 f(int64 b)
{
 if(a[b]!=0)return a[b];
 else a[b] = max(b,(f(b/2)+f(b/3)+f(b/4)));     
 return a[b];     
}


int main()
{
a[0] = 0;
a[1] =1;
a[2] = 2;
a[3] = 3;
a[4] = 4;
a[5] = 5;
   
    int64 n;
    while(cin>>n)
    {
                 if(n==0||n==1||n==2||n==3)cout<<n<<endl;
                 else
       cout<<max(n,(f(n/2)+f(n/3)+f(n/4)))<<endl;        
    }           
return 0;   
}

Street parade

 STPAR
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t)
    {
    queue<int> num;
    stack<int> rest;
    stack<int> pass;
    int temp;
        for(int i=0;i<t;i++)
            {
                cin>>temp;
                num.push(temp);
            }
        while(num.front()!=1)
            {
            //    cout<<num.front()<<endl;
            //    system("pause");
                rest.push(num.front());
                num.pop();
            }
    //    cout<<"out of first"<<endl;
        int start = 1;
        int flag;
    while(!num.empty()){
            while(!num.empty() && num.front()==start)
            {
            //    cout<<num.front();
            //    system("pause");               
                pass.push(num.front());
                num.pop();           
                start++;
            }
        //    cout<<"out of second"<<endl;
            while(!num.empty() && (num.front()!=start))
            {
                //cout<<num.front();
            //    /system("pause");
                //cout<<"out of third"<<endl;
                if(!(rest.empty()) && rest.top()==start)
                {
                //    cout<<rest.top();
                //    system("pause");                   
                    pass.push(rest.top());
                    rest.pop();
                    start++;
                }
                else if(!num.empty())
                {
                //    cout<<num.front()<<endl;
                //    system("pause");
                    rest.push(num.front());
                    num.pop();
                }   
            //    cout<<"out of fourth"<<endl;
            }
        }    while(!rest.empty())
            {
                //cout<<rest.top()<<" "<<pass.top()+1<<endl;
                if(rest.top()==pass.top()+1)
                    {pass.push(rest.top());rest.pop();}
                else {flag=1;goto ny;}
            }
           
        ny:
        if(flag==1){cout<<"no"<<endl;flag=0;}
        else cout<<"yes"<<endl;
        cin>>t;       
    }
return 0;
}

Algorithm


C program for prime number

C program for prime number

Prime number program in c: c program for prime number, this code prints prime numbers using c programming language. To check whether a number is prime or not see another code below. Prime number logic: a number is prime if it is divisible only by one and itself. Remember two is the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications in computer science and mathematics. A number greater than one can be factorized into prime numbers, For example 540 = 22*33*51

Prime number program in c language

#include<stdio.h>
 
int main()
{
   int n, i = 3, count, c;
 
   printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);
 
   if ( n >= 1 )
   {
      printf("First %d prime numbers are :\n",n);
      printf("2\n");
   }
 
   for ( count = 2 ; count <= n ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
         printf("%d\n",i);
         count++;
      }
      i++;
   }
 
   return 0;
}
Download Prime number program.
Output of program:
Prime number c program

C program for prime number or not

#include<stdio.h>
 
main()
{
   int n, c = 2;
 
   printf("Enter a number to check if it is prime\n");
   scanf("%d",&n);
 
   for ( c = 2 ; c <= n - 1 ; c++ )
   {
      if ( n%c == 0 )
      {
         printf("%d is not prime.\n", n);
  break;
      }
   }
   if ( c == n )
      printf("%d is prime.\n", n);
 
   return 0;
}

C program for prime number using function

#include<stdio.h>
 
int check_prime(int);
 
main()
{
   int n, result;
 
   printf("Enter an integer to check whether it is prime or not.\n");
   scanf("%d",&n);
 
   result = check_prime(n);
 
   if ( result == 1 )
      printf("%d is prime.\n", n);
   else
      printf("%d is not prime.\n", n);
 
   return 0;
}
 
int check_prime(int a)
{
   int c;
 
   for ( c = 2 ; c <= a - 1 ; c++ )
   { 
      if ( a%c == 0 )
  return 0;
   }
   if ( c == a )
      return 1;
}
There are many logic to check prime numbers, one given below is more efficient then above method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
Only checking from 2 to square root of number is sufficient.
There are many more efficient logic available.