Question

In: Computer Science

Write a C program without using if-else construct that does the following. It accepts a sequence...

Write a C program without using if-else construct that does the following. It accepts a sequence of positive integers between 1 and 9 both inclusive from the keyboard. The program will stop accepting input once an integer outside the range is entered. The program will finish by printing the total number multiples of 3 and total number of even integers entered. Test data and expected output: Enter integers between 1 & 9 both inclusive, outside range to stop Enter integer :0 Total no of even integer entered is 0 Total no of multiples of 3 entered is 0 Enter integers between 1 & 9 both inclusive, outside range to stop Enter integer :2 Enter integer :4 Enter integer :6 Enter integer :9 Enter integer :3 Enter integer :1 Enter integer :2 Enter integer :0 Total no of even integer entered is 4 Total no of multiples of 3 entered is 3

Solutions

Expert Solution

#include <stdio.h>
int main()
{
    int a,b,i;//a counts number of even interges and b counts number of multiples of 3
    a=0; // a is initialized to 0
    b=0; // b is initialized to 0
    printf("Enter integers between 1 & 9 both inclusive, outside range to stop");
    do
    {
        printf("\nEnter Integer : ");
        scanf("%d",&i); //taking integer into i 
        a=(i>=1 && i<=9 && i%2==0)?++a:a; 
        // here if the condition (i>=1 && i<=9 && i%2==0) is true then a=++a ,if it is false a=a
        b=(i>=1 && i<=9 && i%3==0)?++b:b;
        // here if the condition (i>=1 && i<=9 && i%3==0) is true then b=++b ,if it is false b=b
        
    }while(i>=1 && i<=9); //iterates if "i" is in between 1 & 9 both inclusive
    
    printf("\nTotal no of even integer entered is %d",a); // printing number of even numbers 
    printf("\nTotal no of multiples of 3 entered is %d",b); // printing number of multiples of 3
    return 0;
}

For indentation please refer the below pics ::

OUTPUT IS AS FOLLOWS ::

OUTPUT IS AS FOLLOWS ::


Related Solutions

C code only, not C++ or anything else // This program accepts the chest size from...
C code only, not C++ or anything else // This program accepts the chest size from the customer // and returns the proper shirt size as well as the price. // function prototype void check_size_price(int size_val, char *size_char, int *price); #include <stdio.h> int main() { int chest_size; //input - entered chest size char size_str; //output - size (S/M/L) int p; //output - price // validation loop for the input – the chest size must be a positive integer // Call...
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
Write a computer program using C++ that computes the value of a Lagrange Polynomial and accepts,...
Write a computer program using C++ that computes the value of a Lagrange Polynomial and accepts, as input: (n+1) data points (x0, f(x0)), (x1, f(x1)),...(xn, f(xn)).. at value x, which the polynomial is to evaluated. As output, the program should produce a statement that reads, "f(x) = (the value of f(x))". As a test, use the data values (1,2);(-1,1);(0,0);(2,4);(-2,3)
Write a Java program that accepts a sequence of commands and print out the number of...
Write a Java program that accepts a sequence of commands and print out the number of successful insertions, the number of successful deletions, the number of successful searches (through the “find” command), the number of items remaining in the list after executing all commands and the final contents of the list. Three commands that will be taken as inputs are “insert”, “delete” and “find”. Input Line 1: The number of transactions m on the list, where 1  m 200. Line 2...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to do the following: Determine what the input...
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT