Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pascals triangle using user-define function use. Not working !

pascals triangle using user-define function use. Not working !

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    it works fine until the 3 column but in the 4th column it doesn't.

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

    long int factorial(int num);

    int main(){

    int row, col\_1, col\_2, i, j;
    long int k;
    
    printf("Enter the number of rows: \\n");
    scanf("%d", &row);
    
    col\_1 = row-2;
    
    for(i=0; i<=row-1; i++){
    
    	for(j=0; j<=col\_1; j++){
    		printf(" ");
    	}
    	col\_1--;
    
    	for(col\_2=0; col\_2<=i; col\_2++){
    
    		k = factorial(i)/( factorial(i-col\_2) \* factorial(col\_2) ) ;
    		printf(" %ld ", k);
    	}
    	printf("\\n");
    }
    
    return 0;
    

    }

    long int factorial(int num){

    int total, i;
    
    if(num==0 || num==1){
    	total=1;
    
    }else{
    	
    	total = 1;
    	for(i=1; i<=num; i++){
    
    		total = total\*num;
    		num--;
    	}
    }
    
    return total;
    

    }

    V L CPalliniC 3 Replies Last reply
    0
    • T Tarun Jha

      it works fine until the 3 column but in the 4th column it doesn't.

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

      long int factorial(int num);

      int main(){

      int row, col\_1, col\_2, i, j;
      long int k;
      
      printf("Enter the number of rows: \\n");
      scanf("%d", &row);
      
      col\_1 = row-2;
      
      for(i=0; i<=row-1; i++){
      
      	for(j=0; j<=col\_1; j++){
      		printf(" ");
      	}
      	col\_1--;
      
      	for(col\_2=0; col\_2<=i; col\_2++){
      
      		k = factorial(i)/( factorial(i-col\_2) \* factorial(col\_2) ) ;
      		printf(" %ld ", k);
      	}
      	printf("\\n");
      }
      
      return 0;
      

      }

      long int factorial(int num){

      int total, i;
      
      if(num==0 || num==1){
      	total=1;
      
      }else{
      	
      	total = 1;
      	for(i=1; i<=num; i++){
      
      		total = total\*num;
      		num--;
      	}
      }
      
      return total;
      

      }

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      And did you try to debug your code?

      1 Reply Last reply
      0
      • T Tarun Jha

        it works fine until the 3 column but in the 4th column it doesn't.

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

        long int factorial(int num);

        int main(){

        int row, col\_1, col\_2, i, j;
        long int k;
        
        printf("Enter the number of rows: \\n");
        scanf("%d", &row);
        
        col\_1 = row-2;
        
        for(i=0; i<=row-1; i++){
        
        	for(j=0; j<=col\_1; j++){
        		printf(" ");
        	}
        	col\_1--;
        
        	for(col\_2=0; col\_2<=i; col\_2++){
        
        		k = factorial(i)/( factorial(i-col\_2) \* factorial(col\_2) ) ;
        		printf(" %ld ", k);
        	}
        	printf("\\n");
        }
        
        return 0;
        

        }

        long int factorial(int num){

        int total, i;
        
        if(num==0 || num==1){
        	total=1;
        
        }else{
        	
        	total = 1;
        	for(i=1; i<=num; i++){
        
        		total = total\*num;
        		num--;
        	}
        }
        
        return total;
        

        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        for(j=0; j<=col_1; j++){

        If col_1 has the value 4 in the above, you will actually perform 5 loops. Is that what you expect to happen?

        1 Reply Last reply
        0
        • T Tarun Jha

          it works fine until the 3 column but in the 4th column it doesn't.

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

          long int factorial(int num);

          int main(){

          int row, col\_1, col\_2, i, j;
          long int k;
          
          printf("Enter the number of rows: \\n");
          scanf("%d", &row);
          
          col\_1 = row-2;
          
          for(i=0; i<=row-1; i++){
          
          	for(j=0; j<=col\_1; j++){
          		printf(" ");
          	}
          	col\_1--;
          
          	for(col\_2=0; col\_2<=i; col\_2++){
          
          		k = factorial(i)/( factorial(i-col\_2) \* factorial(col\_2) ) ;
          		printf(" %ld ", k);
          	}
          	printf("\\n");
          }
          
          return 0;
          

          }

          long int factorial(int num){

          int total, i;
          
          if(num==0 || num==1){
          	total=1;
          
          }else{
          	
          	total = 1;
          	for(i=1; i<=num; i++){
          
          		total = total\*num;
          		num--;
          	}
          }
          
          return total;
          

          }

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Your factorial function is flawed (a decrementing num as stop condition for i is really a bad choice), try replacing it with

          long int factorial(int num)
          {
          long int fact = 1;
          int i;
          for (i=2; i<=num; ++i)
          fact *= i;

          return fact;
          }

          In testa che avete, signor di Ceprano?

          T 1 Reply Last reply
          0
          • CPalliniC CPallini

            Your factorial function is flawed (a decrementing num as stop condition for i is really a bad choice), try replacing it with

            long int factorial(int num)
            {
            long int fact = 1;
            int i;
            for (i=2; i<=num; ++i)
            fact *= i;

            return fact;
            }

            T Offline
            T Offline
            Tarun Jha
            wrote on last edited by
            #5

            thank you :)

            CPalliniC 1 Reply Last reply
            0
            • T Tarun Jha

              thank you :)

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              You are welcome.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups