Thread: Simple For Loop

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Simple For Loop

    What is wrong here?

    Code:
    #import<stdio.h>
    int main (){
    
    
    	int start, end, fact=0;
    	
    	printf("Please enter a starting and larger ending integer so \n " 
    "I can find the factorial of each of those integer numbers between and including \n"
    "the starting and ending integer numbers which are divisible by 2 as well as by 3\n");
    scanf("%d%d",&start,&end);
     for (int i=start;i=end;i++){
     	if(i%2&&i%3==0){
    	 	fact*=fact;
    		 fact=fact-1;
    		 printf("%d",fact);
    	 }
     		
    
     }
     
    return 0;	
    }

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by jjzarabi View Post
    What is wrong here?

    Code:
    #import<stdio.h>
    int main (){
    
    
    	int start, end, fact=0;
    	
    	printf("Please enter a starting and larger ending integer so \n " 
    "I can find the factorial of each of those integer numbers between and including \n"
    "the starting and ending integer numbers which are divisible by 2 as well as by 3\n");
    scanf("%d%d",&start,&end);
     for (int i=start;i=end;i++){
     	if(i%2&&i%3==0){
    	 	fact*=fact;
    		 fact=fact-1;
    		 printf("%d",fact);
    	 }
     		
    
     }
     
    return 0;	
    }
    Is "import" a C++ function? I'm not sure as I only know C. If C, then try using "#include"

    Also it would help if you'd post what error you're getting since I'm not a very good
    mind reader.

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Several programing languages use "import" directive, but, AFAIK, it's more like C++ "using" then C/C++ preprocessor's "include". Maybe OP has previous experience in one such language, and typed "import" instead of "include" by reflex.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Furthermore:
    Code:
    for (int i=start;i=end;i++)
    The loop condition is broken, as the result of an assignment statement is the value of left hand operand after assignment. I strongly suspect that you intended to write i == end, but that wouldn't work either, as the loop condition wouldn't be met, unless start is equal to end. The condition you want it probably i <= end, which will cause the loop to execute while i is less then or equal to end.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Objective-C?

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I was thinking Python, maybe, because that's where I remember seeing "import" used. But after a quick glance at Obj-C's Wikipedia page, I'd say you've guessed correctly.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    I meant #include
    There is no output
    This is a C program

  8. #8
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Red face

    Quote Originally Posted by jjzarabi View Post
    There is no output
    Oh ok, too bad you didn't mention that minor detail from the start. Sometimes we ask
    people, if they want help that it would be much easier for us to read your code if you
    could use indentation. Just in case you don't know what that means, I made your code
    (with 2 corrections) more readable by indenting your code.

    What was my reward? Well, I got output alright, but I don't think it is your desired
    result.

    The two corrections: (In Green) you declared an int i inside a for loop so I moved it with the
    other int declarations. Second, I took msh's suggestion, and used " i <= end " because
    that makes sense without additional detail.

    Code:
    #include <stdio.h>
    
    int main ()
    {
    
        int i, start, end, fact=0;
        
        printf("Please enter a starting and larger ending integer so \n " 
            "I can find the factorial of each of those integer numbers between and including \n"
            "the starting and ending integer numbers which are divisible by 2 as well as by 3\n\n");
    
        scanf("%d%d",&start, &end);
    
        printf("\n\n");
    
        for (i=start; i <= end; i++)
        {
             if(i%2 && i%3 == 0)
            {
                 fact*=fact;
                fact=fact-1;
                printf("%d",fact);
            }        
        }
    
        printf("\n\n");
     
        return 0;    
    }

    Code:
    Here was my input:  1 100
    Here was my output: -10-10-10-10-10-10-10-10-1
    You wanted output? Well, there ya go!
    Last edited by Char*Pntr; 10-13-2010 at 11:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

Tags for this Thread