is it possible to write this without flag?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rpapaiof
    New Member
    • Oct 2006
    • 27

    is it possible to write this without flag?

    is it possible to write the same program wit same commands without flag and also please describe flag

    #include <stdio.h>


    int main(void){
    int j,n,i,flag;
    printf("Please enter a number : ");
    scanf("%d",&n);
    if(n<2) {
    printf("\nNo prime numbers available");
    return 1;}
    printf("Thank you : 2");
    for(i=2;i<=n;i+ +){
    for(j=2;j<=i-1;j++){
    if(i%j==0){
    flag=0;
    break;}
    flag=1;
    }
    if(flag==1)
    printf("%5d",i) ;
    }

    return 0;
    }
  • jessy
    New Member
    • Oct 2006
    • 106

    #2
    Of course it's possible to write programs without flags ....flags are nothing but a way to shorten your code (minimize it ) and make it easier to implement ...

    it's just like if you have a for loop and you need to make a certain operation based on the no. of times this loop is repeated so we make a COUNTER inside the for loop

    int counter=0;
    for( ; ; )
    {
    counter++; } and when the loop ends this counter is taken a value which is the no. of repetition of this loop so we can then make whatever we want like
    if (counter==0 ) prinf( no looping ...)

    THE FLAG : is just the same like a counter not in counting of course but in the way it acts it's just like ...hmmm..a flag " what does the word flag means to you " like a guide or a sign of what is going on ..

    you set this flag =0 or ONe based on your program and then controls this flag by any way ..
    you can ignore this flag and write your code as if you don't know what a flag is but you will see that your code is a bit longer and that you really need it ..

    believe me you won't learn untill you try something of your OWn and if you want to know how important is something Just Ignore it and code without it and you will know how important it was ......

    Comment

    Working...