Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cstdlib>
- #include <math.h>
- #include <string.h>
- /* kommentaarid */
- /* kt6 yl3 */
- /* programm "Kahendsüsteem" */
- /* koostaja Killu-Smilla Palk */
- int convertBinaryToDecimal(int n)
- {
- int decimalNumber = 0, i = 0, remainder;
- while (n!=0)
- {
- remainder = n%10;
- n /= 10;
- decimalNumber += remainder*pow(2,i);
- ++i;
- }
- return decimalNumber;
- }
- int convertDecimalToBinary(int n)
- {
- int binaryNumber = 0;
- int remainder, i = 1, step = 1;
- while (n!=0)
- {
- remainder = n%2;
- n /= 2;
- binaryNumber += remainder*i;
- i *= 10;
- }
- return binaryNumber;
- }
- main()
- {
- while (true)
- {
- char qvestioone[1];
- printf("Kas soovite teisendust kahendsusteemist kumnendsusteemi (vastake 'd') \nvoi teisendust kumnendsusteemist kahendsusteemi (vastake 'b')? \nKui soovite valjuda, vastake v.\n");
- scanf("%s", &qvestioone);
- if (strcmp(qvestioone, "v") == 0)
- {
- break;
- }
- else if (strcmp(qvestioone, "d") == 0)
- {
- int A;
- printf("Sisestage arv:\n");
- scanf("%d", &A);
- int vastus = convertBinaryToDecimal(A);
- printf("%d\n\n", vastus);
- }
- else if (strcmp(qvestioone, "b") == 0)
- {
- int A;
- printf("Sisestage arv:\n");
- scanf("%d", &A);
- int vastus = convertDecimalToBinary(A);
- printf("%d\n\n", vastus);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement