ghasem_008
Full Member level 4
hi.
is there anyone who can write a full program that reverse a float number in C language?
I want to use from following commands:
1. if/for/while loops
2. I dont want use from any Function.please write a code with basic commands.
I wrote a code,but it's not true for some numbers.
for example : 12.001
//my code:
View attachment my_code.txt
is there anyone who can write a full program that reverse a float number in C language?
I want to use from following commands:
1. if/for/while loops
2. I dont want use from any Function.please write a code with basic commands.
I wrote a code,but it's not true for some numbers.
for example : 12.001
//my code:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 #include <stdio.h> int main() { double a; char ans_exit='y'; while(ans_exit='y') { printf("enter a number:"); scanf("%lf",&a); // integer and decimal sections: int a1 = (int)a; float a2 = a-a1; printf("\n\nfull number is %f\n",a); printf("integer section of %f is:%d\n",a,a1); printf("decimal section of %f is:%f\n\n",a,a2); int i=0,j=1; int b; do { b = a1%10; a1/=10; if (j==1) { // decimal to integer! int a2_1; float a2_2; int ax; while ((a2-floor(a2))!=0) { a2*=10; //printf("\n\na2=%f , int(a2)=%d\n\n",a2,ax); } int a3,c2; a3 = round(a2); printf("\n\na2 = %f , a3=%d\n\n",a2,a3); do { c2 = a3%10; a3/=10; printf("%d",c2); }while(a3!=0); printf("."); } printf("%d",b); j++; }while(a1!=0); printf("\n\nDo you want to repeat program?(yes=y,no=n):"); ans_exit = getche(); printf("\n\n"); } return 0; }
View attachment my_code.txt
Last edited by a moderator: