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.

Print Diamond

C program to print diamond pattern

Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:
  *
 ***
*****
 ***
  *

C programming code

#include <stdio.h>
 
int main()
{
  int n, c, k, space = 1;
 
  printf("Enter number of rows\n");
  scanf("%d", &n);
 
  space = n - 1;
 
  for (k = 1; k <= n; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");
 
    space--;
 
    for (c = 1; c <= 2*k-1; c++)
      printf("*");
 
    printf("\n");
  }
 
  space = 1;
 
  for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");
 
    space++;
 
    for (c = 1 ; c <= 2*(n-k)-1; c++)
      printf("*");
 
    printf("\n");
  }
 
  return 0;
}
Download Diamond program.
Output of program:
Diamond c program

Print Pattern in C

C program to print patterns of numbers and stars

These program prints various different patterns of numbers and stars. These codes illustrate how to create various patterns using c programming. Most of these c programs involve usage of nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.
    *
   ***
  *****
 *******
*********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.

C programming code

#include <stdio.h>
 
int main()
{
   int row, c, n, temp;
 
   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);
 
   temp = n;
 
   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");
 
      temp--;
 
      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");
 
      printf("\n");
   }
 
   return 0;
}
Download Stars pyramid program.
Output of program:
stars pyramid program
For more patterns or shapes on numbers and characters see comments below and also see codes on following pages:
Floyd triangle
Pascal triangle
Consider the pattern
*
**
***
****
*****
to print above pattern see the code below:
#include <stdio.h>
 
int main()
{
    int n, c, k;
 
    printf("Enter number of rows\n");
    scanf("%d",&n);
 
    for ( c = 1 ; c <= n ; c++ )
    {
        for( k = 1 ; k <= c ; k++ )
            printf("*");
 
        printf("\n");
    }
 
    return 0;
}
Using these examples you are in a better position to create your desired pattern for yourself. Creating a pattern involves how to use nested loops properly, some pattern may involve alphabets or other special characters. Key aspect is knowing how the characters in pattern changes.

C pattern programs

Pattern:
   
   *
  *A*
 *A*A*
*A*A*A*
C pattern program of stars and alphabets:
#include<stdio.h>
 
main()
{
    int n, c, k, space, count = 1;
 
    printf("Enter number of rows\n");
    scanf("%d",&n);
 
    space = n;
 
    for ( c = 1 ; c <= n ; c++)
    {
        for( k = 1 ; k < space ; k++)
           printf(" ");
 
        for ( k = 1 ; k <= c ; k++)
        {
            printf("*");
 
            if ( c > 1 && count < c)
            {
                 printf("A");    
                 count++; 
            }      
        }    
 
        printf("\n");
        space--;
        count = 1;
    }
    return 0;
}
Pattern:
        1
       232
      34543
     4567654
    567898765
C program:
#include<stdio.h>
 
main()
{
      int n, c, d, num = 1, space;
 
      scanf("%d",&n);
 
      space = n - 1;
 
      for ( d = 1 ; d <= n ; d++ )
      {
          num = d;
 
          for ( c = 1 ; c <= space ; c++ )
              printf(" ");
 
          space--;
 
          for ( c = 1 ; c <= d ; c++ )
          {
              printf("%d", num);
              num++;
          }
          num--;
          num--;
          for ( c = 1 ; c < d ; c++)
          {
              printf("%d", num);
              num--;
          }
          printf("\n");
 
      }
 
      return 0;
}

Palindrome Numbers

Palindrome Numbers

Palindrome number in c: A palindrome number is a number such that if we reverse it, it will not change. For example some palindrome numbers examples are 121, 212, 12321, -454. To check whether a number is palindrome or not first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not. C program for palindrome number is given below.

Palindrome number algorithm

1. Get the number from user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.

Palindrome number program c

#include <stdio.h>
 
int main()
{
   int n, reverse = 0, temp;
 
   printf("Enter a number to check if it is a palindrome or not\n");
   scanf("%d",&n);
 
   temp = n;
 
   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }
 
   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);
 
   return 0;
}
Download Palindrome number program.
Output of program:
Palindrome number c prorgram