Question

In: Computer Science

Objectives Ability to receive input from keyboard Use the printf statement as an alternative to cout....

Objectives

  1. Ability to receive input from keyboard
  2. Use the printf statement as an alternative to cout.
  3. Ability to use For loop
  4. Ability to use Do-while loop

Program 1:                                                                                                                         

  • Create a program called CountUpBy5.
  • Input: The program should accept a positive integer n from the user as input. If the given input is not a positive input it should prompt the user to retry
  • Hint: use a While-loop for this

  • Output: The program should then count all numbers from 0 up to n that are multiples of 5 and print them out on a single line on the screen.
  • Hint: use a For-loop or a While-loop for this

Program 2:

  • Modify the CountUpBy5 program in Part 1 and call it CountByC to accept another number C, by which it will count up to n, the second user provided number.
  • Obviously, you have to ensure that C is greater than 0.
  • C may also be a fraction, and does not need to be am integer.

## in C Language only ##

Solutions

Expert Solution

PROGRAM 1:

CODE:

#include <stdio.h>
int main() {
    int n=-1;
    printf("Enter a number: ");
    while(n<0)
    {   scanf("%d",&n);
        if(n<0)
        {
            printf("Retry: ");
        }
        else
        {
            break;
        }
    }
    for(int i=0;i<=n;i++)
    {
        if(i%5==0)
        {
    printf("%d ",i);
        }
    }
    return 0;
}

SCREENSHOT OF CODE and OUTPUT:

PROGRAM 2:

CODE:

#include <stdio.h>
#include<math.h>
int main() {
    int n=-1;
    printf("Enter a number N: ");
    while(n<0)
    {   scanf("%d",&n);
        if(n<0)
        {
            printf("Retry: ");
        }
        else
        {
            break;
        }
    }
    float c=-1;
    printf("Enter a number C: ");
    while(c<0)
    {   scanf("%f",&c);
        if(c<0)
        {
            printf("Retry: ");
        }
        else
        {
            break;
        }
    }
    for(int i=0;i<=n;i++)
    {
        if(fmod(i,c)==0)// fmod is a predefined function to find mod with float        {
    printf("%d ",i);
        }
    }
    return 0;
}

SCREENSHOT OF CODE:

OUTPUT:

I hope I solved your query. In case of doubt feel free to ask in comment section.


Related Solutions

Can someone convert this code to use printf & scanf instead of cin and cout #include...
Can someone convert this code to use printf & scanf instead of cin and cout #include <iostream> int main() { int n, i = 0; float RF, PFD, Tn = 0, Ts; std::cout << "**** Welcome to the Time Study Program ****" << std::endl; std::cout << "\nEnter the number of elements for the given operation being performed" << std::endl; std::cout << "Number of Elements: "; std::cin >> n; if (n < 0) { std::cout << "\nNumber of elements cannot be...
Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or...
Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or DecimalFormat Practice programming simple mathematical calculations Instructions: Samwise Gamgee has finally decided to submit his expense report for all of his adventures regarding his travels to Mordor. Part of those expenses are his stay at the Prancing Pony Inn located in Bree. You are to write a simple Java program which will generate an Invoice for his stay at the Inn. Your program should...
This program must be in java, use printf, and request user input for a file name....
This program must be in java, use printf, and request user input for a file name. NameSubsLast3 (30 points) Write a program that does the following. Write a program that does the following. Requests the name of an input file and uses it to create an input file Scanner. Requests the name of an output file and uses it to create a PrintWriter. I have posted firstNames.txt and outNames.txt in the homework for Lesson 6. Calls createArray to create a...
Input a phrase from the keyboard and output whether or not it is a palindrome. For...
Input a phrase from the keyboard and output whether or not it is a palindrome. For example: 1. If the input is Madam I'm Adam Your output should be "Madam, I'm Adam" is a palindrome 2. If your input is Going to the movies? Your output should be "Going to the movies?" is not a palindrome Note the quotes in the output line. Code language java using JGrasp
Input a phrase from the keyboard. If the last letter in the phrase is "a" or...
Input a phrase from the keyboard. If the last letter in the phrase is "a" or "e" (uppercase or lowercase) then output the last letter 3 times. ELSE If the first letter in the phrase is either "i", "o", or "u" (uppercase or lowercase) then output the length of the phrase In all other cases print "no vowel" Code language is java
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
Input a phrase from the keyboard. For example, input could be: Today is a rainy day,...
Input a phrase from the keyboard. For example, input could be: Today is a rainy day, but the sun is shining. Your program should count (and output) the number of "a"s in the phrase, the number of "e"s, the number of "i"s , the number of "o"s, the number of "u"s, and the total number of vowels. Note: The number of "a"s means the total of uppercase and lowercase "a"s etc. Also: the vowels are a,e,i,o,u. In the above example...
Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT