May i ask about C Programming
|
can you give me some (if else) looping examples... like #include void main() { int x; printf("Enter number X: "); scanf("%d",&x); { if (x%10==0) printf("Number is divisible by 10"); else printf("Number is not divisible by 10n"); } } |
Re: May i ask about C Programming
|
if(true) puts("true"); else puts("false") if(true) { if(false) { if(true) { if(false) puts("you can do many things with loops"); puts("just be careful with the start and the end braces"); } else puts("3f"); } else { puts("by the way there are tons of code from the web just try googling :-)"); puts("by the way puts acts like printf :-)"); puts("a nested loop can be confusing so try to avoid a deep one."); } }
http://czetsuya-travel.blogspot.com
|
Re: May i ask about C Programming
|
what kind of looping is it? your program make me blur... explain slowly... please... i need you to write completly... the puts act to printf that i know... the program that you give me is like you take it from the middle of the program.. so kindly i don't completly understand it.... |
Re: May i ask about C Programming
|
Nope it's not part of any program code, you've ask for some if/else code right so i've given u a nested example of one :-). i know it's seems complex at first glance but if u will analyze it... It's pretty easy... To avoid confusion always use {} braces ex:if(
http://czetsuya-travel.blogspot.com
|
Re: May i ask about C Programming
|
thanks... your program is good.... oh... let see my NEW ONE... #include void main() { int x,y,num; num=1; printf("Enter a number: "); scanf("%d",&x); for(y=1; y<=x; y++) { num=num*y; } printf("%d != %d",x,num); } i just learn it just past few day only... what is your comment about this? is it useful? I would like to have your code too... Write it to me... :) |
Re: May i ask about C Programming
|
That is code for calculating factorial result, right? Here is my example #include void main { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) cout<<(i+j)%2<<" "; cout< } How 'bout it?? Can someone tell me about file input and output. I need code to access binary file for an encryption algorithm I just learn... Thanxx... |
Re: May i ask about C Programming
|
where cin for input. Sides I don't see you declare values for i and j man I was already forgotten about C++ |
Re: May i ask about C Programming
|
//factorial can be easily rewritten to take advantage of recursion like theseint main() { printf("%d", factorial(5)); //will do something like 5 x 4 x 3 x 2... no need for x 1 since the answer is the same return 0; } int factorial(n) { if(n > 1) return (n * factorial(n - 1)); } @werrior - I think i and j were properly declared inside the loops right? :-)
http://czetsuya-travel.blogspot.com
|
Re: May i ask about C Programming
|
Oh now u mention it to think that programming is very hard for me - -; what're those code for?? |
Re: May i ask about C Programming
Link |
by NagisaFudo
on 2006-09-12 17:26:07
|
[quote] That is code for calculating factorial result, right? Here is my example #include void main { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) cout<<(i+j)%2<<" "; cout< } } How 'bout it?? Can someone tell me about file input and output. I need code to access binary file for an encryption algorithm I just learn... Thanxx... [/quote] There are differences between C and C++. Standard input/output in C uses printf and scanf while in C++ it uses cout and cin C++ allow declaration of variables in 'for' loops while C does not. Running for(int i = 0; i < 10; i++) { printf ("i = %d", i); } in C will give you an error. |
Re: May i ask about C Programming
|
Thanks pals..... and the code is to make a program...( maybe ) :) epp.... this is for crystal... i have copy your program to compile but there is 17 error and 5 warning... write the original program from that program to me at here.... and for czetsuya i have not do program like your program before... what kind of hidden file you use and give me the full program too please... :) Attention...Now i just have learn the c programming only...not the C++... so give me c languege program and the software that i use is ~ Microsoft Visual C++ 6.0 ~ i don't know use the C++ yet so give me the c languege codes.... OK... :) New program to be share#include (modify here to - to < ) -stdio.h- ( and change - to > ) main() { int x,y; printf("Initial Number="); scanf("%d",&x); printf("Final Number="); scanf("%d",&y); printf("n"); do { printf("%d ",x); x++; }while(x<=y); printf("nn"); } how about this... |
Re: May i ask about C Programming
|
@William//this code is called recursion, a function that called itself int fact(int n) { if(n < 1) return 1; else return (n * fact(n - 1)); } //inside your main function you can call it like: int main() { printf("%d", fact(5)); //and it will print 120 try it :-) }
http://czetsuya-travel.blogspot.com
|
Re: May i ask about C Programming
Link |
by NagisaFudo
on 2006-09-12 20:28:45
|
@William can you tell me what you are trying to code? your current code just prints the value of x till y (from initial to final) . if you are trying to do factorial like what czetsuya is doing.. its not quite there yet... this line is not wrong (well not really, but): [quote] x=x++; [/quote] x++; does the trick just fine...it automatically increments x by 1... you do not need to assign it back... and the reason why u have errors running crytals' code is because the code is in c++ and not in c.is your file saved as *.c?? |
Re: May i ask about C Programming
|
@ czetsuya thank for your program... if can, give more example for me... :) oh... what is the hidden file that you use... ex: stdio.h , conio.h , math.h @NagisaFudo also thank for you... you are right... no need to put x=x++; just put x++; is enough... i would like to have your program example too... :) /* Program to choose operate*/ #include (The hidden file is stdio.h) void main() { int x,y,t; char opt; printf("Enter the fisrt number:"); scanf("%d",&x); printf("\n\nEnter the second number:"); scanf("%d",&y); printf("\n\nChoose a operate + - * /:"); scanf("%s",&opt); { if (opt1 ='+') t=x+y; else if (opt2 ='-') t=x-y; else if (opt3 ='*') t=x*y; else if (opt4 ='/') t=x/y; else printf("\n\nWrong input"); } printf("\n\n\nThe answer is %d\n\n",t); } |
Re: May i ask about C Programming
|
I've just used char ch = '+'; switch(ch) { case '+' : //do something here break; case '-' : //do something here break; case '*' : //do something here break; case '/' : //do something here break; default : break; }
http://czetsuya-travel.blogspot.com
|
Re: May i ask about C Programming
|
oh this type i know to use already... /*Program to days*/ #include stdio.h void main() { int x; printf("Enter 1~7 to output day:"); scanf("%d",&x); printf("\n\n"); switch(x) { case 1: printf("Monday\n\n\n"); break; case 2: printf("Tuesday\n\n\n"); break; case 3: printf("Wednesday\n\n\n"); break; case 4: printf("Thursday\n\n\n"); break; case 5: printf("Friday\n\n\n"); break; case 6: printf("Saturday\n\n\n"); break; case 7: printf("Sunday\n\n\n"); break; default: printf("Invalid input\n\n\n"); } } is it same... let discuzz another type... |
Re: May i ask about C Programming
|
well you can try a link-list if your bored :-)
http://czetsuya-travel.blogspot.com
|
Re: May i ask about C Programming
Link |
by
on 2006-09-18 16:37:49
|
If you wanted more practice with recursion, you can try writing a recursive Fibonacci sequence function. (i.e., 1 1 2 3 5 8 13, etc.) |
Re: May i ask about C Programming
Link |
by calamity58
on 2006-09-19 00:59:49
|
here's my example and tips About C and C++: The C programming language is a popular and widely used programming language for creating computer programs. Programmers around the world embrace C because it gives maximum control and efficiency to the programmer. If you are a programmer, or if you are interested in becoming a programmer, there are a couple of benefits you gain from learning C: You will be able to read and write code for a large number of platforms -- everything from microcontrollers to the most advanced scientific systems can be written in C, and many modern operating systems are written in C. The jump to the object oriented C++ language becomes much easier. C++ is an extension of C, and it is nearly impossible to learn C++ without learning C first. |
Re: May i ask about C Programming
Link |
by NagisaFudo
on 2006-09-21 23:57:19
|
yeah, thats kinda true.. its not easy if C++ is learnt first because of its OO nature but its not really that difficult. but i do agree to the fact that C++ becomes easier after learning C because C++ handles a lot of things that C does not. proper memory allocation and garbage collection is some of it, i think. if you are bored, you could easily try coding out binary trees, hash tables and such on C. |