Question

In: Computer Science

You are to write a program in C to do the following in a loop for...

  1. You are to write a program in C to do the following in a loop for the KL46Z .
    • Prompt the user for a positive integer greater than 1 and sanity-check the input.
    • If the number is a prime number, it is to be printed on a new line in red text.
    • If the number is evenly divisible by 7, it is to be printed on a new line in green text.
    • If the current number is evenly divisible by the last number entered, it is to be printed on a new line in blue.
    • If the least significant digit of the number is 3, the number is to be printed on a new line underlined.

If none of the above conditions are met, simply print a blank new line

Solutions

Expert Solution

#include<stdio.h>
#include<math.h>
int prime(int x)
{
   if(x<2)return 0;
   if(x==2)return 1;
   for(int i=2;i<=sqrt(x);i++)
   {if(x%i==0)return 0;
   }
   return 1;
}
int main()
{int b=-1;
   while(1)
   {
       int a;
       printf("enter number ");//enter -1 for break
       scanf("%d",&a);

if(a==-1)break;
       if(prime(a))
       {
       printf("\033[1;31m");//convert next written statment as red
       printf("%d\n",a);
       printf("\033[0m");//reset
       }
       else if(a%7==0&&(a/7)%2==0)
       {
       printf("\033[1;32m");//convert next written statment as green
       printf("%d\n",a);
       printf("\033[0m");//reset
       }
       else if(b!=-1&&a%b==0&&(a/b)%2==0)
       {
       printf("\033[1;34m");//convert next written statment as blue
       printf("%d\n",a);
       printf("\033[0m");//reset
       }
       else if(a%10==3)
       {printf("\033[4m");//convert next written statment as underlined
       printf("%d\n",a);
       printf("\033[0m");//reset
       }
       else
       printf("\n");
       b=a;
   }
}

input ;- this is coloured check on your terminal

enter number 2   
2
enter number 14
14
enter number 28
28
enter number 7
7
enter number 5
5
enter number 10
10
enter number 33
33
enter number
1

enter number


Related Solutions

Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table size: 10 N N^2 Square root of N --------------------------------------------- 1 1 1.00 2 4 1.41 3 9 1.73 ….. 10 100 3.16
1. Write pseudocode for the following program. You do not have to write actual C++ code....
1. Write pseudocode for the following program. You do not have to write actual C++ code. Assume you have a text file, that has been encrypted by adding 10 to the ASCII value of each character in the message. Design a decryption program that would reverse this process, and display the original message to the console.to the new array. 2.Write the code for the specified program. Use proper style and naming. Test and upload your code as a CPP file....
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT