C program to print Pascal triangle

C program to print Pascal triangle

Pascal Triangle in c: C program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user. First four rows of Pascal triangle are shown below :-
   1
  1 1
 1 2 1
1 3 3 1

Pascal triangle in c

#include <stdio.h>
 
long factorial(int);
 
int main()
{
   int i, n, c;
 
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);
 
   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");
 
      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
 
      printf("\n");
   }
 
   return 0;
}
 
long factorial(int n)
{
   int c;
   long result = 1;
 
   for( c = 1 ; c <= n ; c++ )
         result = result*c;
 
   return ( result );
}
Download Pascal triangle program.
Output of program:
Pascal triangle program

C program to print Floyd's triangle

C program to print Floyd's triangle

C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.

C programming code

#include <stdio.h>
 
int main()
{
  int n, i,  c, a = 1;
 
  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);
 
  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }
 
  return 0;
}
Download Floyd triangle program.
Output of program:
Floyd triangle program

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;
}