GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming
Go To First  |  Previous Question  |  Next Question 
 Programming  |  Question 2 of 11    Print  
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.

  
Total Answers and Comments: 3 Last Update: April 18, 2008     Asked by: rishi 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 29, 2007 12:28:10   #1  
sarathi trichy Member Since: October 2007   Contribution: 106    

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.

#include<stdio.h>
#include<conio.h>
void main()
{
int x y z k 0;
clrscr();
printf("enter the number");
scanf(" d" &x);
while(x>0)
{
y x 10;
k k+y;
x x/10;
}
printf(" d" k);

getch();
}



 
Is this answer useful? Yes | No
January 05, 2008 04:51:16   #2  
Susil Kumar Member Since: January 2008   Contribution: 11    

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

}


 
Is this answer useful? Yes | No
April 18, 2008 04:57:31   #3  
gatadivaddavinod Member Since: April 2008   Contribution: 2    

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.
#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();
}

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape