Showing posts with label C codes. Show all posts
Showing posts with label C codes. Show all posts

Monday 28 January 2013

1030. Triple Fat Ladies Problem code: EIGHTS

 #include<stdio.h>
int main()
{
    int test;
      long long int k,temp;
      scanf("%d",&test);
   
      while(test--)
      {
          scanf("%lld",&k);
          temp=(k-1)*250+192;
          printf("%lld\n",temp);
      }
      return 0;
}

Thursday 24 January 2013

Perfect Number

////Program to check if a given no. is perfect no.  or not

#include <stdio.h>

/*Function to find the sum of divisors of the number n*/
int sum_of_divisors(n)
{
    int i , sum = 0 ;
   
    for (i=1;i<=n-1;i++)
           if (( n%i )==0)
        sum=sum+i;
    return sum;
}

/*Function to check if the given number is perfect or not by checking the sum of divisors*/
int check_perfect(n)
{
    int sum ;
   
    sum = sum_of_divisors(n);
   
    if (sum==n)
        return 1;
   
    else
        return 0;
}

/*main function*/
int main()
{
    int i , n , check;
   
    printf("enter the no.");
    scanf("%d",&n);

    check = check_perfect(n);
    if (check == 1)
        printf("given no. is perfect no.\n");

    else
        printf ("given no. is not a perfect \n");
}

Wednesday 23 January 2013

2523. Mispelling Problem code: GNY07A

#include<stdio.h>
#include<math.h>
int main()
{
    char c;
    int d,i,j,ct,n;
    scanf("%d",&n);
    ct =1;
    for(j=1;j<=ceil(2*n);j++)
    {
        scanf("%d",&d);
        i=1;
        while((c=getchar())!='\n'&& c!=' ' )
        {
            if (i==1)
                printf("%d ",j/2);
             if (c!=' ' && i!=d)
                printf("%c",c);           
            i++;
        }
            printf("\n");
    }
    return 0;
}

Tuesday 22 January 2013

Reversed Number

//  C code to reverse the digits of a user entered number
#include<stdio.h>

/*Function to reverse the digits of a number*/
void reverse_digits(n)
{
    while(n!=0)  
    {
        printf("%d",n%10);
        n=n/10;
    }
}
int main()
{
    int n;
    printf("Enter a no.\n");
    scanf("%d",&n);

    printf("The reverse no.is\n");
    reverse_digits(n);
    printf("\n");
}

Perfect Number

////Program to check if a given no. is perfect no.  or not

#include <stdio.h>

/*Function to find the sum of divisors of the number n*/
int sum_of_divisors(n)
{
    int i , sum = 0 ;
   
    for (i=1;i<=n-1;i++)
           if (( n%i )==0)
        sum=sum+i;
    return sum;
}

/*Function to check if the given number is perfect or not by checking the sum of divisors*/
int check_perfect(n)
{
    int sum ;
   
    sum = sum_of_divisors(n);
   
    if (sum==n)
        return 1;
   
    else
        return 0;
}

/*main function*/
int main()
{
    int i , n , check;
   
    printf("enter the no.");
    scanf("%d",&n);

    check = check_perfect(n);
    if (check == 1)
        printf("given no. is perfect no.\n");

    else
        printf ("given no. is not a perfect \n");
}

Centigrate to Fahrenheit

//Program to convert temperature from degree centigrade to Fahrenheit:
// (f-32)/180 = c/100

#include<stdio.h>

int main()
{
    float c,f;

    printf("enter temp in centigrade:\n ");
    scanf("%f",&c);
   
    f=(1.8*c)+32;
    printf("temp in Fahrenheit=\n%f",f);
    return 0;
}

Diamond Shape

/*C code to print a diamond shape when an odd number is entered*/

#include <stdio.h>

/*Function to check whether the number entered is valid or not*/
int check_validity(n)
{
    if (n%2 ==0 || n<=0)
        return 0;
       
    else
        return 1;
}

/*Function to print upper part of the diamond shape for an odd entered number*/
void diamond_upper_shape(n)
{
    int  i , j ,  m ;

    for (i=1 ; i<=n ; i=i+2)
    {       
        for (m=1 ; m<=(n-i)/2 ; m++)
            printf ("  ");
               
        for (j=1;j<=i;j=j+1)           
            printf (" *");                                           

        printf ("\n");
    }       
}

/*Function to print the lower part of the diamond shape for an odd entered number*/
void diamond_lower_shape(n)
{
    int  i , j ,  m ;
   
    for (i=n-2 ; i>=1 ; i=i-2 )
    {   
        for (m=1 ; m<=(n-i)/2  ; m++)
            printf ("  ");
                                       
        for (j=1 ; j<=i ; j=j+1)
            printf (" *");                                           
                                       
        printf ("\n");   
    }

}
/*main function*/
int main()
{
    int  i , j ,  n  , m ;
   
    printf ("enter an odd no.");
    scanf ("%d",&n);
   
    if (check_validity(n)!=0)
    {
        diamond_upper_shape(n);   
        diamond_lower_shape(n);
    }
   
    else
        printf("Enter an odd number\n");
}

Monday 21 January 2013

Pascal Triangle

/*Program to print the element of pascal-triangle when the row and column numbers are given.\

pascal triangle:


                1
             1    1
          1    2    1
       1    3    3    1   
    1    4     6    4    1
  ................................
......................................

*/
#include<stdio.h>

int pascal(int a,int b)
{
    if (a==b)
        return 1;

    else if (b==1)
        return 1;
       
    else 
        return pascal(a-1,b-1)+pascal(a-1,b);
}

int main()
{
    int a,b;
   
    printf("Enter row number : ");
    scanf("%d",&a);
   
    printf("Enter column\n number : ");
    scanf("%d",&b);
   
    if ( (b>a)  ||  (b<=0)  ||  (a<=0)  )
        printf("Invalid Entry\n");
   
    else 
        printf("The (%d,%d)th element of pascal triangle is : %d\n",b,a,pascal(a,b));

   return 0;
}

Fibonacci Levels

#include<stdio.h>

/*Print the levels of fibonacci recursion and also returns the fibonacci value*/
int fib(int n,int level)
{
    int i;

    for(i=0;i<4*level;i++)  //a*level adjusts the horizontal space between two levels
        printf(" ");

    printf("fib(%d)\n",n);

    if(n==0)
        return 0;

    else if(n==1)
        return 1;

    else 
        return (fib(n-1,++level))+(fib(n-2,level));    //fib(n) = fib(n-1)+fib(n-2) ; n>=2
}

//main function
int main()
{
    int n , f;
   
    printf ("Enter the no. whose fibonacci no. is to be found;  ");
    scanf("%d",&n);
    
    printf("fib(%d)=%d\n", n , fib(n , 0) );
 
   return 0;
}

Matrix Rotation

// Program to rotate the nxn matrix to the right by 90 degree..
#include<stdio.h>
#define max 100

/*Function to store  the matrix*/
void read_matrix(int a[][max] , int order)
{
    int i , j , n = order-1;
    for (i=0;i<=n;i++)
        for (j=0;j<=n;j++)
           scanf("%d",&a[i][j]);
}


/*Function to rotate a matrix to the right by 90 degress*/
void rotate_right( int a[][max] , int b[][max] , int order)
{
    int i , j , n = order -1;
    for (i=0;i<=n;i++)
        for (j=0;j<=n;j++)   
             b[j][n-i]=a[i][j];
}

/*Function to print the matrix*/
void print_matrix(int a[][max] , int order)
{
    int i  , j , n =  order-1;
    for (i=0;i<=n;i++)
    {
        for (j=0;j<=n;j++)
          printf("%d\t",a[i][j]);
       
        printf("\n");
    }   
}

/*main function*/
int main()
{
    int  i , j , n ;
    int a[max][max];
    int b[max][max];
   
    printf("enter the order of matrix\n");
    scanf ("%d",&n);

    printf ("enter the nxn matrix\n");
    read_matrix (a , n);
    rotate_right( a , b , n);

    printf ("before rotation: \n");
    print_matrix( a , n);
    printf ("\n");

    printf ("after rotation: \n");
    print_matrix(b , n);
    printf("\n");
   
    return 0;
}

Circular Prime

 /* C code to check whether a number obtained by circularly shifting its digits is prime or not*/
#include <stdio.h>
//function for finding the number of digits in a given number
int get_num_digits(n)
{
    int k = 0;
    while (n!=0)
    {
        n = n/10;
        k++;
    }
    return k;
}

//function for finding a circular number
int get_shifted_num(n)
{

    int i , d, k = 1 , last_digit , new_num;

    d= get_num_digits(n);

    for (i=1 ; i<=(d-1); i++)
        k = k*10;

    last_digit = n/k;

    new_num = (n - last_digit*k)*10 + last_digit;
    return new_num;
}

//function to check whether a number is prime or not
 is_prime(n)
{
    int  i , circular_num =n;   
    for (i=1; i<=circular_num/2 ; i++)
        if (circular_num%(i+1) == 0)
                return  0 ;
    return 1;           
}

int main ()
{
    int i , n , d ,  circular_num , z;

    printf("enter a number : ");
    scanf("%d", &n);

    d = get_num_digits(n);
   
    circular_num =n;

    for (i = 1; i<=d ; i++)
    {
        printf ("shifted no. is  : %d\n", circular_num);
        z = is_prime(circular_num);

        if (z==1)
            printf ("%d is prime\n\n", circular_num);
        else
            printf ("%d is not prime: \n\n" , circular_num);

        circular_num = get_shifted_num(circular_num);
    }
    return 0;
}

1724. Counting Triangles Problem code: TRICOUNT

#include<stdio.h>
int main()
{
        long long int test,n;
        scanf("%lld",&test);
        while(test--)
            scanf("%lld",&n)&&printf("%lld\n",n*(n+2)*(2*n+1)/8);
        return 0;
}

9948. Will it ever stop Problem code: WILLITST

#include<stdio.h>
int main()
{
    long long int  n;
    while(scanf("%lld",&n)!=EOF)
    {
        if((n&(n-1))==0)
            printf("TAK\n");
        else
            printf("NIE\n");
    }
    return 0;
}

302. Count on Cantor Problem code: CANTON

#include<stdio.h>
#include<math.h>
int main()
{
    long long int n , x ,i , j ,sub , sum , t;
   
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&x);
        n=ceil((-1+sqrt(1+8*x))/2);
        sub = x - n*(n-1)/2;
        sum = n+1;
        if (n%2==0)
            printf("TERM %lld IS %lld/%lld\n",x , sub , sum-sub);   
        else
            printf("TERM %lld IS %lld/%lld\n",x, sum -sub ,sub);           
    }
    return 0;
}

2123. Candy I Problem code: CANDY

#include<stdio.h>
int main()
{
    int n, k,avg,sum,count;
    while(1)
    {
        scanf("%d",&n);

        if(n==-1)
            break;

        int a[n+1];   
        sum=0;   
        count=0;

        for(k=0;k<n;k++)       
        {
            scanf("%d",&a[k]);
            sum=sum+a[k];
        }   
            avg=sum/n;
            if(avg*n!=sum)
                printf("-1\n");
            else
            {
                for(k=0;k<n;k++)
                    if(a[k]<avg)
                        count=count+avg-a[k];
                printf("%d\n",count);           
            }   
           
    }
    return 0;
}

10286. DOTA HEROES Problem code: DOTAA

#include<stdio.h>
int main()
{
    int t,n,m,D,count,i,H;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&D);
        count=0;
        while(n--)
        {
            scanf("%d",&H);
            if (H>D)
            {
                while(H>0)
                {
                    H=H-D;
                    count++;
                }
                count--;
            }
        }
        if (count<m)
            printf("NO\n");
        else
            printf("YES\n");   
    }
    return 0;
}

11354. Amusing numbers Problem code: TSHOW1

#include<stdio.h>
#include<math.h>
int main()
{
    long long int x,y,sum;
    int n ,k , t, a[51],i=0,j;
    scanf("%d",&t);
    while(t--)
    {
    scanf("%lld",&x);
    i=0;
    n = ceil(log2((x+3)/2));
    sum = 2*(pow(2,n-1)-1);
    y = x -sum-1;
    while(1)
    {
        a[i++]=y%2;       
        y=y/2;               
        if(y==0)
        {
            k= n-i;
            if(i!=n)
               
                while(k--)
                    a[i++]=0;    ;               
            break;
           
        }
    }
    for(j=i-1;j>=0;j--)
        if(a[j]==0)       
            printf("5");   
        else if(a[j]==1)
            printf("6");
    printf("\n");
    }   
    return 0;
}

Life, the Universe, and Everything

1. Life, the Universe, and Everything Problem code: TEST


#include <stdio.h>
int main()
{
    int N;
    while( 1 )
    {
        scanf( "%d", &N );
   
        if( N == 42 )
                break;
       
        printf( "%d\n", N );
    }
    return 0;
}

Feynman

3410. Feynman Problem code: SAMER08F

#include<stdio.h>
int main()
{
    int n;
       while(1)
       {
              scanf("%d",&n);
              if(n==0)
                  break;
              printf("%d\n", (n*(n+1)*(2*n+1))/6 );
        }
        return 0;
}

Family Problems

5297. Family Problems Problem code: FAMILYP

#include<stdio.h>
#include<math.h>
int main()
{
    long long int N;
    int ans;
    float n;
    char c;
       
       while((scanf("%lld",&N)) !=EOF)
       {
            n=-1+sqrt(1+8*N);
            n=n/2;
       
            long k;
            k=ceil(n);
          ans=k%27-1;
          printf("TERM %lld IS %c\n",N,65+ans);
         
        }
        return 0;
}