Write a program to read a four digit integer and print the sum of its digits.Write a program that reads a floating point number and then displays the right-most digit of the integral part of the number.

Showing Answers 1 - 2 of 2 Answers

Program that reads a floating poing number and then displays the right-most digit of the integral part of the number

Steps to do it:

1. Read the floating value
2. Convert it to a string
3. Consider the string having array of characters
4. Store the characters in an array of characters
5. Print the array
6. Stop

#include<stdio.h>
main(){
               float digit;
               char c[10],r_digit[10];
               printf("nEnter the floating number:");
               scanf("%f",&digit);
               sprintf(c,"%s",digit); /* Floating value is stored as a string in c*/

                len=strlen(c)-1;
                
                i=0;
                j=0;
                while(i<len){
                                       if(c[i]=='.'){
                                                   break;
                                       }
                         i++;
                 }                

                i++;

                while(i<len){

                                      r_digit[j]=c[i];

                                      j++;

                                      i++; 

                }
                 r_digit[i]='';

               printf("nThe right-most digit from the given integer:%s",r_digit);

}

  Was this answer useful?  Yes

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,r,sum=0;
cout<<"Enter any four digit number";
cin>>n;
while(n>0)
{
r=n%10;//To get remainder
sum=sum+r;
n=n/10;//To get coefficient
}
cout<<"The sum of the given four digt number is:"<<sum;
getch();
}

  Was this answer useful?  Yes

mahamad

  • Apr 17th, 2012
 

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3. main()

  4. {

  5.         float f;

  6.         int s,r,k=1;

  7.         printf("Enter the number:=>");

  8.         scanf("%f",&f);

  9.         s=f/1;

  10.         while(k==1)

  11.         {

  12.                 r=s%10;

  13.                 printf("%d

  14. ",r);

  15.                 k++;

  16.         }

  17.         return 0;

  18. }


  Was this answer useful?  Yes

karuna gaur

  • Apr 15th, 2013
 

456 sum is 15

Code
  1. int sumDigits(int n) {

  2. int sum = 0;

  3.  

  4. while( n > 0 ) {

  5. sum += (n % 10); // add last digit of n to sum

  6. n /= 10; // divide n by 10 to "chop off" the last digit

  7. }

  8.  

  9. return sum;

  10.  

  11. getch();

  12. }

  Was this answer useful?  Yes

gaurav kumar jaiswal

  • Aug 31st, 2014
 

write a c++ program to the sum and product of digits of an integers

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions