Search This Blog

Monday, 5 January 2015

SMALL FACTORIALS

PROBLEM:- SMALL FACTORIALS

You are asked to Calculate the factorials of small positive numbers in the range of 100.

INPUT- First line contains an integer T which represents the number of test cases, followed by T lines each containing a single positive Integer.

 OUTPUT- For each integer, print the factorial of that number.

eg:-
Input:-
2 (test cases)
3
4

Output:-
6
24



SOLUTION:-


#include<stdio.h>
int index;
int main()
{
int t,num,temp,x;
scanf("%d",&t);
while(t--)
{
scanf("%d",&num);
int arr[200]={0};
arr[0]=1;
int i=1;
int current=0;
while(i<=num)
{
index=temp=0;
while(arr[index]>0 || index<current)
{
x=(arr[index]*i)+temp;
arr[index]=x%10;
temp=x/10;
index++;
}
while(temp>0)
{
arr[index]=temp%10;
temp/=10;
index++;
}
i++;
current=index-1;
}
index--;
while(index>=0)
{
printf("%d",arr[index]);
index--;
}
printf("\n");
}
return 0;
}

Monday, 1 December 2014

Trailing Zeroes in the factorial of a Number.

Problem:- Write a program to find the number of zeroes at the end of n!  i.e.  the number of trailing zeroes in the factorial of a number.

Solution:-

#include<stdio.h>
int main()
{
   int n,count=0,i;
    scanf("%d",&n);
        for(i=5;n/i>=1;i*=5)
            count+=n/i;
        printf("No. of zeroes at the end of %d! are:-%d\n",n,count);
return 0;
}

Saturday, 29 November 2014

Program in C for the Permutations of a String

Problem :- Write a program in C to print all the permutations of a String.

Solution:-



#include <stdio.h>
#include<string.h>
/* Function to print permutations takes three parameters:-(String, Starting index, Ending index) */
void permutation(char *arr, int i, int n)
{
    int j;
    if (i == n)
    {
        if(strlen(arr)==n)
        {
            printf("%s\n", arr);
        }
    }
    else
    {
        for (j = i; j <= n; j++)
        {
            swap((arr+i), (arr+j));
            permutation(arr, i+1, n);
            swap((arr+i), (arr+j));
        }
    }
}
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
int main()
{
    char arr[100];
    gets(arr);
    int len=strlen(arr);
    permutation(arr, 0, len);
    getchar();
    return 0;
}