## Monday, June 26, 2006

### Sum of First and Last Digits of a Four-Digit number.

This program is based on the "sum of digits" program discussed previously.
This program also has some usage of the Modulus Operator.

If a four-digit number is input through the keyboard,
write a program to obtain the sum of the first and the last digit of this number.

`/*HINT: If a number is divided using % , then the number to the right side of the decimal pointis the result. (This applies only to integers.) */#include<stdio.h>main (){int number, last_digit, first_digit, total;printf (" Enter the number which is to be operated on: ");scanf ("%d", &number);last_digit = number % 10;total = last_digit;first_digit = (number / 1000) % 10;total = total + first_digit;printf ("The total of the first and the last digit of the entered number is: %d", total);}/*This program is based on the "Sum of Digits" Program */`

The File can be found out at:

Anonymous said...

This Program Only Shows Last Digits.

Anonymous said...

i am searching for a code which adds up sum of first and last digit of a number... eg:- 3456778= 3+8= 11.

Anonymous said...

but this code gives only for 4 digit number... and how to implement for different inputs... a single code which can do for 5 digit and 7 digit and so on.. please do update with it...

Anonymous said...

just use 1000000 instead of 1000 in the line no 9. the code will work for 3+8=11

Anonymous said...

For that sort or any program contact me
mayankmehra95@gmail.com

ಸುಧೇಶ್ ಶೆಟ್ಟಿ said...
This comment has been removed by the author.
ಸುಧೇಶ್ ಶೆಟ್ಟಿ said...

You can use while loop for calculating the sum for a given number of any lengh....

12:25 AM, A

Vivek said...

hi,,
hey how should i do for a four digit number sum?
imgn i need the sum of 4 digits of my car number 2569..
any four digit num which the end user typs i want the sum in single digit..lyk 2+5+6+9=22=>2+2=4
i want result as 4..how?

Indu said...

hye it halp me for compel me my h.w....

Kiran K Pittampally said...

Here you go for the program which will sum the first and last digit of a 'n' digit number
----------------------

int main()
{
int num,len,last;
printf("\nEnter n digit number \n");
scanf("%d",&num);
last=num%10;
while(num>0)
{
num=num/10;
len=len+1;
}
num=num/(pow(10,len-1));
num=num%10;
printf("the result is %d",last+num);
getch();
return 0;

}

Anonymous said...

/*Hey
guys check dis out, It will work*/
void main()
{
int num,sum=0,rem;
printf("Enter a number");
scanf(" %d",&num);
rem=num%10;
sum=sum+rem;
while(n>10)
{
num=num/10;
}
rem=num%10;
sum=sum+rem;
printf("sum of 2 digits is: %d",sum);
getch();
}

Anonymous said...

Have a tutorial section on nested loop using for loop and while that helps in learning complex shape

This comment has been removed by the author.

i want a program Sum of first and fifth digit of a 5 or more than 5th digit number

Anonymous said...

good evening sire
can you help me with my problem:
a cashier has a currency notes of denominations 10 50 and 100. if the amount to be withdrawn is input through the keyboard in hundreds, find the total number of currency notes of each denomination the cashier will have to give to the withdrawer

thank you so much!!!
you can reach me through this email
princeryoma17@yahoo.com

Anonymous said...

Can anyone explain to me why did we use first digit=(n/1000)%10? Isn't first digit=n/1000 enough? Thank you.