Friday, July 23, 2010

C program that adds the digits of whole numbers

------------------------------------
#include
int main() {
int in,temp,sum=0;
printf("Enter a value: ");scanf("%d",&in);
while(in!=0){
temp=in%10;
sum+=temp;
in/=10;
}
printf("%d\n",sum);
getch();
}
------------------------------------
input <123>
the output would be<6>

Monday, July 19, 2010

Program that adds 2 sets of time info

This program adds two sets of time information in days,hours and minutes


-----------------------------------------------------------
#include
void TimeAdd(int nday,int nhrs,int nmin,int aday,int ahrs,int amin){
int carryh=0,carryd=0;
nmin+=amin;
if (nmin>=60){
carryh++;
nmin-=60;
}
nhrs+=+ahrs+carryh;
if (nhrs>=24){
carryd++;
nhrs-=24;
}
nday+=aday+carryd;
printf("Total: %d days, %d hours & %d miniutes",nday,nhrs,nmin);
}
int main(){
int nday,nhrs,nmin,aday,ahrs,amin;
printf("Type first set of time information in days,hours and miniutes ");
scanf("%d%d%d",&nday,&nhrs,&nmin);
printf("Type second set of time information in days,hours and miniutes ");
scanf("%d%d%d",&aday,&ahrs,&amin);
TimeAdd(nday,nhrs,nmin,aday,ahrs,amin);
}
------------------------------------------------------------------------
It works you can copy it to notepad & compile it your self