Rayhan’s blog (raynux.com)

Rayhan’s Personal Web Blog Site

Entries Comments

A Simple C Program To Sum Two Large Number

12 March, 2008 (12:49) | C, programming

Tags: ,

I have very little knowledge on C. With this little knowledge I am helping a student of EEE department of BUET. I am requested to help in writing a program to perform addition and multiplication of two large number. In C there is no builtin datatype for large number and as a PHP programmer we are not bound to data type like C. So this is a quite tricky for me. As I am require to work and munipulate with string type variable. I always hate the hassle of C in case of Char type variable. Finally I am end up with the following program to perform addition.

This is a simple c program to sum two big number using c programming language.

int chrtoint(char a){
int i;
for (i = 48; i<=57; i++)
if (toascii(i)==a) return i-48;
return 0;

void main(){
char n1[80];
char n2[80];
int rs[80];
int c1, c2;

int i,j,m, cmax, sum;

printf("Enter First Number:");
scanf("%s", &n1);
printf("\nEnter Second Number:");
scanf("%s", &n2);
c1 = strlen(n1);
c2 = strlen(n2);


cmax = c1;
cmax = c2;

for(i=0; i< cmax; i++){
if(c1==c2 || (i < c1 && i < c2)){
sum = m+chrtoint(n1[i])+chrtoint(n2[i]);
}else if(i >=c1){
sum = m+chrtoint(n2[i]);
}else if(i >=c2){
sum = m+chrtoint(n1[i]);
rs[i] = sum%10;
m = sum/10;


printf("\nResult: ");
for(j=0; j < i; j++){
printf("%d", rs[i-j-1]);


Hope I will solve the multiplication problem soon.




Comment from sellah m’masi
Time: September 21, 2009, 6:09 pm

thanks for solving the problem that i had requested. it hasdone me more good than harm. thanks alot

Comment from sellah m’masi
Time: September 21, 2009, 6:13 pm

can you write a program in c for an algorithm for cos x- and a program polynomial division

Comment from Michel
Time: December 21, 2009, 10:09 pm

it’s good but strrev function is Microsoft thing !!! use this reverse strings function:

void strrev(char* str) {
char tmp;
for(uint i = 0, j = strlen(str)-1; i < j; j–, i++) {
tmp = str[i]; str[i] = str[j]; str[j] = tmp;

Comment from Michel
Time: December 21, 2009, 10:11 pm

oops forgot this one :D –> typedef unsigned int uint;

Comment from David
Time: February 18, 2010, 4:55 pm

Can You Write it with Arrays only ?… ( E-mail & Web are fake …)

Comment from trajanski
Time: March 13, 2010, 8:38 pm

Write a program that will generate every third integer, beginning with 2 and continuing for all integers that are less than N. Calculate the sum of those integers that are evenly divisible by 5.

The first line of input is the number of input cases (n), which is a positive number that does not exceed 10. Each line of the input will contain a single positive integer N.

Your program should print the sum in a single line, for each case.

Comment from trajanski
Time: March 13, 2010, 8:39 pm

Greek mathematicians took a special interest in numbers that are equal to the sum of their proper divisors (a proper divisor of n is any divisor less than n itself). They called such numbers perfect numbers. For example, 6 is a perfect number because it is the sum of 1, 2 and 3, which are the integers less than 6 that divide evenly into 6. Similarly, 28 is a perfect number because it is the sum of 1, 2, 4, 7 and 14.

Your job is to create a program that determines if a given positive number is a perfect number or not.

The first line of input is the number of input cases (n), which is a positive number that does not exceed to 30. The succeeding n lines specify the arbitrary number that your program will check if it is a perfect number or not. These numbers are positive integers that should not exceed 4,294,967,296 as well.

For each input case, print out the corresponding answer (“Perfect” or “Not Perfect”) in a single line as standard output.

Comment from trajanski
Time: March 13, 2010, 8:39 pm

Write a program that implements the Euclidean Algorithm for finding the greatest common divisor (GCD) of two given positive integers. This algorithm transforms a pair of positive integers (m,n) into a pair (d,0) by repeatedly dividing the larger integer by the smaller integer and replacing the larger with the remainder. When the remainder is 0, the other integer in the pair will be the greatest common divisor of the original pair (and all the intermediate pairs).

For example, if m is 532 and n is 112, then the Euclidean Algorithm reduces the pair (532,112) to (28,0) by

(532,112) ? (112,84) ? (84,28) ? (28,0)

So, 28 is the GCD of 532 and 112.

Required: Use of functions – input(), gcd() and display() and other functions that you believe are necessary (if there are).


The first line of input is the number of input cases (n), which is a positive number that does not exceed 100. The succeeding n lines specify the pair of integers m and n (separated by a space) of which you are computing the GCD for.

For each line of input, print out corresponding greatest common divisor of the given pair of integers preceded by “#x – “ where x is the line number.

Comment from Weirwindle
Time: August 10, 2010, 8:52 am

I simple long division program. It accepts two floating point numbers and the number of digits to caculate too. I cheated a little by using “/” in the program once.
// Long Division including decimal


int takeawaydec(double, double);
int main(void){
// (Numerator / Denominator)
double num; //Numerator
double den; //Denominator

int c; // For number to the left of decimal place
int i, j=0; // “i” is for sliding decimals to the left. “j” is cycle through printing decimal places
int e; // Number of Decimals Taken off of input numbers
long int final; // Number of demimal place till end
long unsigned int a; //To get rid of deviations created by double precision
long unsigned int b;

printf(” Advanced Long Division Calculator\n”);
printf(“\n Input Numerator: “);
scanf(“%lf”, &num);
printf(“\n Input Denominator: “);
scanf(“%lf”, &den);
printf(“\n Number of decimal places: “);
scanf(“%d”, &final);
if(final!=0){ // We don’t always need the decimal to show up
printf(“\n %.0f.”, floor(num/den)); // Just to get rid of first digits to the left of decimal (This program is made for the right decimal places)
printf(“\n %.0f”, floor(num/den));
e = takeawaydec(den, num); // Will slide of decimal places till only zeros are on the right (Please don’t put any more than 6 decimals on end)
for(i=0; i<=e; i++){
a = floor(num);
b = floor(den);
c = a/b;
while(1){ // This will cycle through one decimal digit at a time and print each
if(j==final+1){ // Must add a decimal place due to it allays being one decimal less than input
return 0;
a = 10*(a-(c*b));
c = a/b;
printf("%d", c);

int takeawaydec(double deno, double nume){
int mult=0; // Number of time decimal point is slide until all 0's are on right
if((deno)!=floor(deno) || (nume)!=floor(nume)){
deno*=10; // Shift decimal by one
return mult;
return 0;

Comment from Jhee
Time: March 15, 2011, 6:43 pm

hi! I just found this site through searching now. And I do think your knowledge in program can really help me alot. I may seem a bit “feeling close” but right now I’m really desperate regarding in my c programming . .Can you please help me. .please ..please ..

Comment from Jhee
Time: March 15, 2011, 6:55 pm

Your brother, Harry, is in the States working. From time to time, he sends you money for some of your school expenses. However, sometimes, since the rate of exchange changes every now and then, you don’t know how much to ask from your brother or how much he has sent you. So, you decided to create a program that would help you and your brother in the conversion from Php to USD and vice versa.

In the program, the conversion should be performed by a function that returns a float value. Call this function from main() and pass the value to be converted. Since the conversion actually depends on the currency in which the value is specified, also pass an indication of the currency to the called function. Take note that you are not going to create an input function.


void display(int choice, float amount);
float convert( … );

int main()
float rate_of_conversion, amount;
int choice;

scanf( “%f”, &rate_of_conversion );
scanf( “%d %f”, &choice, &amount );

while( … )
convert( rate_of_conversion, choice, amount );

float convert( … )
float result;
switch( choice )
case 1:

//computation here

case 2:

//computation here


return …

void display(int choice, float amount) {
switch( choice )
case 1:
printf( “USD%f”, …);

case 2:
printf( “Php%f”, …);



The first line of input indicates the equivalent (in floating-point) of 1 US Dollar in Philippine Peso. Each succeeding line of input contains an integer and a float, which are separated by a space. The integer indicates the conversion, that is, 1 for Peso-to-Dollar and 2 for Dollar-to-Peso. The float is the value to be converted. If the line of input, on the other hand, contains 0, the program will quit running.

Your program should print the converted floating-point number in a single line for each corresponding line of input. Furthermore, if the first value indicated in the input is a value other than 1 or 2, your program should print the line “Invalid Input” for that line.

Comment from rayhan
Time: March 18, 2011, 1:07 am

So Sorry, I am not working with C for a long time, can’t help you this time..

Write a comment