I wanna write a code which would add the digits before decimal and after decimal.
eg:- 12.22 would add upto 3.4 , 491.941 would give 5.5
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
double a1[5],a ; //array of 5 numbers
int b,len,i,sum=0,cnt = 0;
char ch[10];
for(i = 0;i<5;i++) // accept numbers
scanf("%lf",&a1[i]);
while(cnt<5)
{
a = a1[cnt];
b = (int)(a); //numbers before float
a = a - (int)(a); //remaining number
sprintf(ch,"%lf",a); //copy to string
while(b)
{
sum += b%10;
b/=10;
}
if(sum >= 10)
{
sum = (sum % 10) + (sum /10); //if the resultants sum is a two digit num //convert it to single digit (assuming sum to be two digit)
}
printf("\n%d .",sum);
sum = 0;
i = 2;
while(ch[i])
{
sum += ch[i] - '0';
i++;
}
if(sum >= 10)
{
sum = (sum % 10) + (sum /10);
}
printf("%d",sum);
sum = 0;
cnt++;
}
}
Similar thing can be done by reading it as string array.
But i wanted to know if it could be done without using string array or string functn. plainly by using any numeric datatype.