Convert any number into roman number


#include <stdio.h>
int numbercount(int);//function to count the number of digits.
int main (int argc, const char * argv[])
{
  
    int number;
    printf("\nEnter the number:");    scanf("%d",&number);
  
    //number = numbercount(number);
    // for calculating 1000th position
    //----------------------------------
    int quotient = number/1000;
    if (quotient>=1)
    {
        number = number%1000;
        for (int i=1; i<=quotient; i++)
        {
            printf("m");
        }
    }
  
    //for calculating 100th position
    //-----------------------------------
    int hunquotient = number/100;
  
    if (hunquotient>=1)
    {
      
        if (hunquotient>=5)
        {
            printf("d");
            hunquotient = hunquotient - 5;
        }
      
        number = number%100;
        for (int j=1; j<=hunquotient && hunquotient!=0; j++)
        {
            printf("c");
        }
    }
  
    //for calculating 10th position
    //-----------------------------------
  
    int tenquotient = number/10;
  
    if (tenquotient>=1)
    {
        if (tenquotient>=5) {
            printf("l");
            tenquotient = tenquotient-5;
        }
      
        number = number%10;
        for (int k=1; k<=tenquotient && tenquotient!=0; k++)
        {
            printf("x");
        }
    }
  
    //for calculating last digit
    //-------------------------------------
  
    if (number>=5) {
        number = number - 5;
        printf("v");
    }
    for (int l=1; l<=number; l++) {
        printf("i");
    }

    return 0;
}
//function to count the number of digits
int numbercount(int num)
{
    int count = 0;
    while (num>0)
    {
        num = num / 10;
        count++;
    }
  
    return count;
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

ArmStrong Number


#include <stdio.h>

void armstrong(void);
int main (int argc, const char * argv[])
{
    armstrong();  
    return 0;
}
void armstrong(void)
{
    int remainder,sum,n;
  
    for (int i=1; i<500; i++)
    {
        sum = 0;
        n=i;
      
        while (n>0)
        {
            remainder = n%10;
            sum += remainder*remainder*remainder;
            n  = n/10;
        }
        if (sum==i)
        {
            printf("\n%d",i);
        }
    }
  
  
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Recursive and Normal addition


A 5-digit positive integer is entered , write a function to calculate sum of digits of the 5-digit number:
(1) Without using recursion (2) Using recursion



#include <stdio.h>
int add(int);
int recadd(int);
int sum=0;
int r;
int main (int argc, const char * argv[])
{
    printf("%d",recadd(12111));
    return 0;
}
//without using recursion
int add(int num)
{
  
  
    while (num>0)
    {
        r = num%10;
        sum = sum +r;
        num = num/10;
    }
  
    return sum;
}
//using recursion
int recadd(int num)
{
    if (num>0)
    {
        r = num%10;
         sum = sum + r;
        recadd(num/10);
    }
 
    return sum;
  
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

pattern printing2

         1  
       2   3  
     4   5   6  
   7   8   9   10 


#include <stdio.h>
int main (int argc, const char * argv[])
{
    int print=1;
  
    for (int i=0; i<=3; i++)
    {
  
        for (int j=4-i; j>=1; j--)
        {
            printf("  ");
        }
      
      
        for (int k=0; k<=i; k++)
        {
                printf(" %d",print);
                printf(" ");
                printf(" ");
                print++;
                   
        }
        printf("\n");
    }
  
  
    return 0;
}


  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Printing pattern

 A B C D E F G F E D C B A
 A B C D E F   F E D C B A
 A B C D E       E D C B A
 A B C D           D C B A
 A B C               C B A
 A B                   B A
 A                       A


#include <stdio.h>
int main (int argc, const char * argv[])
{
    int j,k,l;
  
    for (int i=1; i<=7; i++)
    {
        for (j=65; j<=71-(i-1); j++)
        {
            printf(" %c",j);
        }
        for ( k=1; k<=(i-1); k++)
        {
             printf(" ");
             printf(" ");
            if (k>1)
            {
                printf(" ");
                printf(" ");
            }
        }
        for (l=71-(i-1); l>=65; l--)
        {
            if (l==71)
            {
               continue;
            }
            printf(" %c",l);
        }
      
        printf("\n");
    }
  
    return 0;
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

21 Match stick problem



Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows:
There are 21 matchsticks. 
The computer asks the player to pick 1, 2, 3, or 4
matchsticks. 
After the person picks, the computer does its picking.
  Whoever is forced to pick up the last matchstick loses the game.


#include <stdio.h>
int main (int argc, const char * argv[])
{
    int matchstick = 21;
    int user,computer;
  
    while (matchstick>=1)
    {
      
        if (matchstick==1)
        {
            printf("\nMatch stick status:%d",matchstick);
            printf("\nYou loose!!!!!!:(:(");               break;
        }
      
        printf("\nMatch stick status:%d",matchstick);      
        printf("\nEnter the choice (1,2,3,4)):");        scanf("%d",&user);
      
        printf("\nYou picked %d",user);      
        if (user>=5 || user <=0)
        {
            printf("\nInvalid value");            continue;
        }
      
        computer = 5 - user;
      
        printf("\nComputer picked%d",computer);      
        matchstick = matchstick - computer - user;
      
    }
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Gregorian calendar Date


According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

#include <stdio.h>enum days
    {
    Monday,Tuesday,wednesday,thursday,friday,saturday,sunday
    };
int main (int argc, const char * argv[])
{
  
    int sourceyear = 1900;
    int currentyear;
    printf("\nEnter the currentyear:");    scanf("%d",&currentyear);
  
    int x = ((currentyear -sourceyear )%7);
    switch (x)
    {
        case Monday:
            printf("Monday");
            break;
        case Tuesday:
            printf("Tuesday");
            break;
        case wednesday:
            printf("wednesday");
            break;
        case thursday:
            printf("thursday");
            break;
        case friday:
            printf("friday");
        case saturday:
            printf("saturday");
        case sunday:
            printf("sunday");
        default:
            break;
    }
    return 0;
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS