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 point

is the result. (This applies only to integers.) */#include<stdio.h>

main (){intnumber, 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:

Download File

## 16 comments:

This Program Only Shows Last Digits.

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

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...

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

For that sort or any program contact me

mayankmehra95@gmail.com

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

12:25 AM, A

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?

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

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;

}

/*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();

}

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

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

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

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

Post a Comment