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.
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...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a program called x.c that reads an integer n from standard input, and prints an...
Write a program called x.c that reads an integer n from standard input, and prints an nxn pattern of asterisks and dashes in the shape of an "X". You can assume n is odd and >= 5. Solve this problem using only while loop. Solution: ./x Enter size: 5 *---* -*-*- --*-- -*-*- *---* ./x Enter size: 9 *-------* -*-----*- --*---*-- ---*-*--- ----*---- ---*-*--- --*---*-- -*-----*- *-------* ./x Enter size: 15 *-------------* -*-----------*- --*---------*-- ---*-------*--- ----*-----*---- -----*---*----- ------*-*------ -------*------- ------*-*------...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT