Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

integer print wrong number

#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator(){
    int k;
    printf("Masukkan batas bilangan genap = ");
    scanf("%i", &k);
    printf("Bilangan genap dari 0 sampai %i adalah :\n");
    for (int i=0; i<=k;i+=2){
        printf("%i\n", i);
    }
    
}
int main(){
    genap_generator();
    system("pause");
}

I made a program to generate odd numbers with int k as the max number but when i print the integer it doesnt print kprint error

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

in the line where you want to print k: printf("Bilangan genap dari 0 sampai %i adalah :\n");, you didnt pass k.

The line should be: printf("Bilangan genap dari 0 sampai %i adalah :\n", k);.

What the function does print right now is what is placed on the stack where k should have been.

Also, you are printing all the even numbers, if you want to print the odd numbers start from i=1.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading