Question

In: Computer Science

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:

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

Solutions

Expert Solution

#include<stdio.h>
#include<math.h>
void main()                              // main function
{
    int n, i, j, x;                      // declaration of variables
    
    printf("Enter an integer: ");        
    scanf("%d", &n);                     // taking n as user input

    x = pow(2, n);                       // calculating 2^n
                       
    for(i=1; i<x; i*=2)                  // outer loop is running n times
        {
            for(j=1; j<=i; j++)          // inner loop for printing the * 's
                printf("*");             // printing " * "
            printf("\n");                // new line after each iteration of outer loop
        }
}

Output:


Related Solutions

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 *-------------* -*-----------*- --*---------*-- ---*-------*--- ----*-----*---- -----*---*----- ------*-*------ -------*------- ------*-*------...
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)
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> For this project, you are to: 1) You should validate any data coming from the...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> c++ project. need help.
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
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++ 
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT