GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Programming

 Print  |  
Question:  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.




January 01, 2008 04:51:16 #2
 Susil Kumar   Member Since: January 2008    Total Comments: 10 

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

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);

}

     

 

Back To Question