Beginner in C! Doubt!

Asked

Viewed 91 times

-1

Hello! Someone could help me in this question here! (C Language)

"Write an algorithm that reads 15 numbers and writes how many numbers larger than 30 were typed."

What would the C algorithm be like to answer that?

1 answer

5


Hello,

The OS community doesn’t usually like questions that look like questions enunciated, because it shows that you didn’t try to research anything before coming here.

You know the basics of C?

The standard library for read and print functions is stdio

You can include it at the beginning of the program with

#include <stdio.h>

This will give you access to functions such as printf and scanf.

To read an integer, you need to declare an integer and a scanf call.

int x;
scanf(" %d", &x);

The & operator scans the x address, where an integer ("%d") will be stored read.

You know how to make a loop in C?

int i;

for(i=0; i < 15; i++) {

    printf("Estou na iteração %d desse laço\n", i);

}

This section will iterate 15 times, just printing this. " n" adds a line break.

You will also need comparisons.

int x = 1;
int count = 0;
if( x > 30 ) {

    count += 1;

}

This stretch will add 1 in Count if x is greater than 30.

Putting it all together:

#include <stdio.h.>

int main(void) {

    int i = 0, count=0, x;
    for(i=0; i < 15; i++) {
        scanf(" %d", &x);
        if( x > 30 ) {
            count +=1;
        }
    }
    printf("%d numeros maiores que 30\n", count);

    return 0;
}
  • Thanks Gabriel! Thank you so much! About my question, how do you think I could have posted it?

  • 1

    If I’ve solved your question, please mark it as correct. Your post could have been something like, "I’m having doubts about putting together a C loop, what’s the syntax?"

  • Something more specific than, "what’s the algorithm?"

  • @Dextermayhew check out the tour and this guide on how to ask a good question.

  • @Dextermayhew , please read the content of the following link and you will understand why your question does not match the policy of the site: https://pt.meta.stackoverflow.com/a/5486/132

Browser other questions tagged

You are not signed in. Login or sign up in order to post.