Question

Can i get help with this C program requirement. Modify the program Figure 2.13 (letter/while). Create...

Can i get help with this C program requirement.

Modify the program Figure 2.13 (letter/while). 
Create a variable at the top of your code named power2 and assign it the value 8
Create a variable at the top of your code named result and assign it the value 0
Ask the user to enter FOUR 1's and 0's followed by a '*'
In the loop, when the letter is '1' add power2 to result and store it back into result
In the loop, change power2 to be the next lower power of 2

After the loop do a cout to print the result and explain it is decimal

In other words, the program used to do this:
0101*
0101

Now it should do this 
0101*
That is 5 in decimal

If you run it again:
1010*
That is 10 in decimal

If you run it again:
0111*
That is 7 in decimal

In other words, you just wrote a binary to decimal converter.

Here is my code:

#include <stdio.h>
#include <stdlib.h>

int main() {
  
char letter;
printf("Please enter FOUR 1's and 0's and add * at the end;\n");
scanf("%c", &letter);
  
while(letter != '*'){
printf("%c* power2 =", letter);
printf("\n");
  
scanf("%c", &letter);
}

return (0);
}

Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

int main() {

char letter;

int power2=8,result=0;

printf("Please enter FOUR 1's and 0's and add * at the end;\n");

scanf("%c", &letter);

while(letter != '*'){

if(letter=='1')

result=result+power2;

scanf("%c", &letter);

power2=power2/2;

}

printf("That is %d in decimal",result);

return (0);

}

Output

clang-7 -pthread -lm -o main main.c
./main
Please enter FOUR 1's and 0's and add * at the end;
0111*
That is 7 in decimal

Add a comment
Know the answer?
Add Answer to:
Can i get help with this C program requirement. Modify the program Figure 2.13 (letter/while). Create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help with this. Modify the program Figure 2.6 (exam1/exam2). Ask the user to enter two...

    Please help with this. Modify the program Figure 2.6 (exam1/exam2). Ask the user to enter two numbers Find the result when the first number is divided by the second Also find the remainder when the first number is divided by the second In other words, the program used to do this: 68 85 score = 81 Now it should do this 22 2 Divide = 11 Remainder = 0 If you run it again: 11 2 Divide = 5 Remainder...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • Working in C, modify the algorithm created in the Topic 1 assignment "Non-Linear Flow Chart" to...

    Working in C, modify the algorithm created in the Topic 1 assignment "Non-Linear Flow Chart" to instead compute the nth term in the series by calling a recursive function. Note that the value of "n" will need to be an input argument, along with the two starting values of the series. Discuss the efficiency of this method versus the iterative logic that you wrote previously. Previous Code: //main.c #include<stdio.h> #include<limits.h> int main() {    //declare a integer data type variales...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • How do I make this program run the same using a do while loop instead of...

    How do I make this program run the same using a do while loop instead of the for loop? I'm supposed to replace the for loop with a do-while. Also, how would i replace it with a while loop? #include void main(void) { int n, sum, product, i; printf("n?\n"); scanf("%i", &n); printf("You entered %i\n", n); sum = 0; product = 1; for (i=1; i<=n;i++) { if((i%3!=0) && (i%5!=0)){ sum = sum + i; product = product * i;}      ...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • Hello, I need help with the following C code. This program asks the user to enter...

    Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT