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…!!

STACK implementation using ARRAY in C


/* stack implementation through array */

#include<stdio.h>

#define MAXSIZE 5

int stack[MAXSIZE];
int top=0; // index pointing to top of stack

void main()
{
void push();
void pop();
void display();
int will=1, i;

while(1)
{
printf(“\n\n\n MAIN MENU:\n\n\n 1. PUSH \n 2. POP \n 3. EXIT \n\n ENTER YOUR CHOICE:”);
scanf(“%d”, &will);
switch(will)
{
case 1:
push();
display();
break;
case 2:
pop();
display();
break;
case 3:
exit(0);
break;
default:
printf(“Invalid Choice”);
}
}  //end of outer while
} //end of main

void push()
{
int num;
if(top>=MAXSIZE)
{
printf(“\n STACK FULL”);
return;
}
else
{

printf(“\n\n ENTER THE STACK ELEMENT:”);
scanf(“%d”, &num);
stack[top++]=num;
}
}

void pop()
{
if(top>0)
top–;
}

void display()
{
int i;
if(top<=0)
printf(“\n\n STACK EMPTY”);

else
for(i=top-1;i>=0;i–)
{
printf(“\n\n %d”, stack[i]);
if(i==(top-1))
printf(“—>TOP”);

}
}

the ‘extern’ keyword in C


The ‘extern’ keyword specifies that the object is declared with external linkage somewhere else in the program.

By preceding a variable name with ‘extern’ , you can declare a variable without defining it. When you need to
refer to a variable that is defined in another part of your program, you can declare that variable using
extern.

    EXAMPLE 1


#include

int main(void)
{
extern float first, last; /* using global vars */
printf(“%f %f”, first, last);
return 0;
}
/* global definition of first and last */
float first = 10.23, last = 20.33;


This program will 10.23 20.33 because the global variables first and last used by the printf( )
statement are initialized to these values. The extern declaration will tell the compiler that first
and last are declared somewhere else (outside main), so the program will be compiled without error.

Extern allows you to declare a variable without defining it. However, if you give that
variable an initialization, the extern declaration becomes a definition.

“Definition” refers to the place where the variable is created or assigned storage; “declaration” refers to the place where nature of variable is stated but no storage is allocated.