1st C Program 08-07-2011, 01:43 AM
#1
Was kinda proud of myself when I did this, of course it's nothing special. I like to think of it as a good example of using If Else loops to have a recurring statement until the right user input is given.(see code 1) On a side note, I'm trying to learn more of C and I'm looking for an easier way to loop a code, say a multiplication code. Right now I'm setting impossible to meet conditions on a while loop but am wondering if theirs something more efficient or useable. (see code 2)
Code:
#include <stdio.h>
int main()
{
int a;
printf ("Insert the right number: \n");
scanf ("%d", &a);
if (a==10){
printf ("Correct!\n");
getchar();
}
else{
int a;
do{
printf ("Try again\n");
scanf("%d", &a);
} while (a!=10);
if (a==10){
printf ("Correct!\n");
getchar();
}
}
getchar();
}
Code:
#include <stdio.h>
int mult (int a, int b);
int main()
{
int a;
int b;
printf("Insert two numbers to multiply\n");
while (a>=0 || a<=0) {
scanf("%d", &a);
scanf("%d", &b);
printf("This quals: %d\n", mult (a, b));
}
getchar();
}
int mult (int a, int b)
{
getchar();
return a*b;
}