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
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.
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.
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
in.java Write a program that reads an integer from the user and prints a rectangle of...
in.java Write a program that reads an integer from the user and prints a rectangle of starts of width 5 3 and height N. Sample run 1: Enter N: 5 *** *** *** *** *** Bye Sample run 2: Enter N: 8 *** *** *** *** *** *** *** *** Bye Sample run 3: Enter N: 2 *** *** Bye Sample run 4: Enter N: -2 Bye
Write a program that reads in a single integer. Display a message depending on what is...
Write a program that reads in a single integer. Display a message depending on what is given: When the number is 42, output "42 is the Ultimate Question of Life, the Universe, and Everything." When the number is 2020 output "2020 is the current year." When the number is greater than 1000 and less than 3000 output "1500 could be a year." where 1500 is the number the user originally inputted When none of the above, output "no idea." One...
who to write a program c++ that reads in an integer between 0 and 1000 and...
who to write a program c++ that reads in an integer between 0 and 1000 and adds all the digits in the integer?
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT