Calculating sum of digits in “100!”


C program for calculating sum of digits in 100!

#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];

int main(){
int num;
int i,k ;
long int r, sum=0;

printf(“Enter number : “);
scanf(“%d”,&num);

fact[0]=1;

factorialof(num);
for(i=length;i>=0;i–)
{
sum=sum+fact[i];
}
printf(“sum of digits is: %d”, sum );
return 0;
}
void factorialof(int num)
{
int i;
for(i=2;i<=num;i++)
{
multiply(i);
}
}
void multiply(int num)
{
long i,r=0;
int arr[MAX];
for(i=0;i<=length;i++)
{
arr[i]=fact[i];
}

for(i=0;i<=length;i++){
fact[i] = (arr[i]*num + r)%10;
r = (arr[i]*num + r)/10;

}
if(r!=0){
while(r!=0){
fact[i]=r%10;
r= r/10;
i++;
}
}
length = i-1;
}

Reversing a character string without using library function “strrev()”


This post is in response to my friend Anubhav Singh‘s query regarding how to reverse a character string without using the standard library function strrev(), and is a very common question asked in the technical round for TCS, Wipro, Infosys etc.

Hope this will help!!

/* Webdevil’s BLOG POST */
#include<stdio.h>
#include<string.h>
main()
{
char str[50], revstr[50];  //declare a character array to store the input string and reverse string
int i=0,j=0;
printf(“Enter the string to be reversed : “);
scanf(“%s”,str);
for(i=strlen(str)-1;i>=0;i–)
{
revstr[j]=str[i];
j++;
}
revstr[j]=”;
printf(“Input String : %s”,str);
printf(“\nOutput String : %s”,revstr);

}

The output will be like this ->

Queries are most welcome…!!