Question

In: Computer Science

Lab 5 a) Write a program that reads in an unsigned integer K and sums the...

Lab 5

a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner

b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs

* *** * *** * **** * ***** ********** ***** * ****   * *** * *** *

Solutions

Expert Solution

a)

#include <iostream>
using namespace std;

int main()
{
//variable declaration
unsigned int K;
int sum=0;
//display message
cout<<"Enter a number: ";
cin>>K;
for(int i=1; i<=K; i++)
{
//check if number is divisible by 7
if(i%7==0)
sum = sum + i;
}
//display sum
cout<<"Sum = "<<sum;
return 0;
}

INPUT:

Enter a number: 20         

OUTPUT:

Sum = 21  

b)

#include <iostream>
using namespace std;
int main()
{
//variable declaration
int K, space = 1;
cout<<"Enter odd integer greater than two: ";
cin>>K;
//validate the input
if(K%2==0 || K<3)
{
cout<<"Input is wrong";
return 0;
}
K = (K+1) / 2;
space = K - 1;
//print the upper half of diamond
for(int i = 1; i<=K; i++)
{
for(int s = 1; s<=space; s++)
cout<<" ";
space--;
  
for(int s = 1; s<= 2*i-1; s++)
cout<<"*";
  
cout<<endl;
}
  
space = 1;
//print the lower half of the diamond
for(int i = 1; i<= K - 1; i++)
{
for(int s = 1; s<= space; s++)
cout<<" ";
space++;
for(int s = 1 ; s<= 2*(K-i)-1; s++)
cout<<"*";
cout<<endl;
}
return 0;
}
  

INPUT:

Enter odd integer greater than two:  5       

OUTPUT:                                                                                   

*                                                                                                                             

* * *                                                                                                                            

* * * * *                                                                                                                           

* * *                                                                                                                            

*   


Related Solutions

Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0...
Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0 has been entered. Construct three versions of this program, using the while, do-while, and for loops.
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it in horizontal order of the screen
in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and...
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 To achieve...
Write a short main program that reads an integer n from standard input and prints (to...
Write a short main program that reads an integer n from standard input and prints (to standard output) n lines of * characters. The number of *’s per line should double each time, starting with 1. E.g., if n = 5, the output should be as follows: * ** **** ******** ****************
MARS: Use MASKING to convert ASCII characters to Integer Write and run a program that reads...
MARS: Use MASKING to convert ASCII characters to Integer Write and run a program that reads in a string of ASCII characters and converts them to Integer numbers stored in an array USING MASKING, not subtraction. Write a program that: 1. Inputs a 1x8 vector of single-digit integers 2. Stores them into an 8-entry 32-bit Integer array, “V”. After storing the integers in the array: 1. Read the same values using Read Integer and store them in a 32-bit integer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT