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 :

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.

Leave a Reply