Home > Software engineering >  How do I do this "CozaLozaWoza" code perfectly?
How do I do this "CozaLozaWoza" code perfectly?

Time:04-26

Basically...

Our teacher gave us homework to perform "CozaLozaWoza". Here (Picture from Chegg as reference)

I eventually managed to get it working (I guess?), but the problem was that my code was still printing unnecessary stuff on each lines, like printing Coza, Loza and Wozas in places where they shouldn't, can you guys tell me what did I do wrong? or did I missed something? Thanks.

Heres the code:

# include<stdio.h>
# include<conio.h>

main(){
    
    int _a,
        _b=110;
        
    for(_a=1;_a<=_b;_a =1){
            
        if(_a%3==0){
            
            printf(" Coza");
        }
        
        if(_a%5==0){
            
            printf(" Loza");
        }
        
        if(_a%7==0){
            
            printf(" Woza");
        }
        
        if(_a%3==0 && _a%5==0){
                
            printf(" CozaLoza");
        }
        
        if(_a%3==0 && _a%7==0){
            
            printf(" CozaWoza");
        }
        
        if(_a%3!=0 && _a%5!=0 && _a%7!=0){
            
            printf(" %d", _a);
        }
        
        if(_a==0){
            
            printf("\n");
        }
        
    }
}

CodePudding user response:

  • Alternatively, if you use a flag to note when a number is encoded as CozaLozaWoza, you can simplify things.
#include <stdio.h>

#define LINE_BREAK_COUNT    11
#define UPPER_LIMIT         110

int main() {

    for (int num = 1; num <= UPPER_LIMIT; num  = 1) {
        int encoded = 0;
        if (num % 3 == 0) {
            printf ("Coza");
            encoded = 1;
        }
        if (num % 5 == 0) {
            printf ("Loza");
            encoded = 1;
        }
        if (num % 7 == 0) {
            printf ("Woza");
            encoded = 1;
        }
        if (!encoded)
            printf ("%d", num);

        putchar (' ');      // space after each number
        if (num % LINE_BREAK_COUNT == 0)  // Line break
            putchar ('\n');
    }
    return 0;
}
  • As pointed out in the comments, you don't need conio.h & main() should be declared to return int
  • Use #defines in place of magic numbers for ease of maintenance.
  • Avoid using variables prefixed with _ in your code. Let them be succinct to the context they're used in.

CodePudding user response:

Here is the corrected code given below and it works perfectly fine.

There were several mistakes that you had done during coding and those are the reasons which lead you to a wrong output.

  • You hadn't put else if condition.So thatsome number were went though the condition even after evaluated true in another condition.

As an example 15 is multiple of 3 and 5.So as you have programmed it will pass the condition if(_a%3==0 && _a%5==0) and then print CozaLoza and after exiting that if block it will enter to other conditional statements because you have used just if instead of of else if or nested if.So when it enter if(_a%3==0) and if(_a%5==0)it will print the desired word again.

# include<stdio.h>


void main(){
    
    int _a;
    int _b;
    printf("Enter a number: ");
    scanf("%d",& _b);
    for(_a=1;_a<=_b;_a  ){
        if(_a%3 == 0 && _a%5==0 && _a%7==0){
            
            printf(" CozaLozaWoza");
        }
        
        else if (_a%5==0 && _a%7==0){
            
            printf(" LozaWoza");
        }
        
        else if(_a%3==0 && _a%7==0){
            
            printf(" CozaWoza");
        }
        
        else if(_a%3==0 && _a%5==0){
                
            printf(" CozaLoza");
        }
        
        else if(_a%3==0){
            
            printf(" Coza");
        }
        
        else if(_a%5==0){
            
            printf(" Loza");
        }
        
        else if(_a%7==0){
            
            printf(" Woza");
        }
        else{
            printf(" %d", _a);
        }
        if(_a==0){
            printf("\n");
        }
        
    }
}

CodePudding user response:

Building on the answer by @जलजनक (which is quite good), you can simply build a string based on the multiples of 3, 5, 7 and if the string is populated, output the string, otherwise output the digit. You can use a ternary to determine whether to output the leading space or not, e.g.

#include <stdio.h>
#include <string.h>

#define NCHR 16

int main (void) {
  
  int bound;
  
  puts ("Please enter the upper bound:");
  if (scanf ("%d", &bound) != 1 || bound < 1) {   /* validate input */
    fputs ("error: invalid integer argument, or bound < 1.\n", stderr);
    return 1;
  }
  
  for (int i = 1; i <= bound; i  ) {  /* loop 1 - bound */
    char str[NCHR] = "";              /* array for string to print */
    int mod11 = (i - 1) % 11;         /* line break/space output check */
    
    if (i - 1) {  /* check if line break or leading space needed */
      putchar (mod11 == 0 ? '\n' : ' ');
    }
    
    if (i % 3 == 0) {                 /* multiple of 3? */
      strcat (str, "Coza");
    }
    if (i % 5 == 0) {                 /* multiple of 5? */
      strcat (str, "Loza");
    }
    if (i % 7 == 0) {                 /* multiple of 7? */
      strcat (str, "Woza");
    }
    
    if (*str) {
      fputs (str, stdout);    /* print str */
    }
    else {
      printf ("%d", i);       /* print number */
    }
  }
  
  putchar ('\n');   /* tidy up with newline */
}

Example Use/Output

$ ./bin/cozalozawoza
Please enter the upper bound:
110
1 2 Coza 4 Loza Coza Woza 8 Coza Loza 11
Coza 13 Woza CozaLoza 16 17 Coza 19 Loza CozaWoza 22
23 Coza Loza 26 Coza Woza 29 CozaLoza 31 32 Coza
34 LozaWoza Coza 37 38 Coza Loza 41 CozaWoza 43 44
CozaLoza 46 47 Coza Woza Loza Coza 52 53 Coza Loza
Woza Coza 58 59 CozaLoza 61 62 CozaWoza 64 Loza Coza
67 68 Coza LozaWoza 71 Coza 73 74 CozaLoza 76 Woza
Coza 79 Loza Coza 82 83 CozaWoza Loza 86 Coza 88
89 CozaLoza Woza 92 Coza 94 Loza Coza 97 Woza Coza
Loza 101 Coza 103 104 CozaLozaWoza 106 107 Coza 109 Loza
  •  Tags:  
  • c
  • Related