Wednesday, September 23, 2015

Using if else construct in C - II

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

void main ()
{
                clrscr();
                int a;
                printf("FORTUNE TELLER\n");
                printf("please Enter a number between 1 and 5:\t");
                scanf("%d", &a);
                if (a==1)
                {
                                printf("You will have a nice day\n");
                }
                else if (a==2)
                {
                                printf("You will meet some one special today\n");
                }
                else if (a==3)
                {
                                printf("You will win a lottery today\n");
                }
                else if (a==4)
                {
                                printf("You will get a special gift today\n");
                }
                else if (a==5)
                {
                                printf("You will find your true love today today\n");
                }
                else
                {
                                printf("Please enter a number between 1 and 5\n");
                }
                getch();
}
Output:  
FORTUNE TELLER
Enter a number between 1 and 5:   5
You will find true love today.


Using if else construct in C

#include <stdio.h>
#include <conio.h>
void main
{
                clrscr();
                int a;
                printf("Please enter a number: \n");
                scanf("%d",&a);
                if
                                (a<20)
                {
                                printf("%d is less than 20\n", a);
                }
                else
                {
                                printf("%d is greater than 20\n", a);
                }
                getch();
}

Output:  
Please enter a number: 22
22 is greater than 20.

Tuesday, September 15, 2015

C Programming - Scanning and Printing

#include <stdio.h>
#include <conio.h>
int main()
{
                clrscr();
                char adjective[20];
                char food[20];
                char chore[20];
                char furniture[20];
                printf("Enter an adjective:");
                scanf("%s",&adjective);
                printf("Enter a food:");
                scanf("%s",&food);
                printf("entr a household chore (past tense):");
                scanf("%s",&chore);
                printf("Enter an item of furniture:");
                scanf("%s",&furniture);
                printf("\n\nDont touch that %s %s!\n",adjective,food);
                printf("I just %s the %s!\n",chore,furniture);
                getch();
                return(0);
}

Output:  
Dont touch that (adjective) (food)

I just (chore) the (furniture)!

Learning C - print your name

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

void main()
{
                clrscr();
                char name[20];
                printf("Enter your name ");
                scanf("%s",&name);
                printf("Hi %s, glad to meet you. ",&name);
                getch();
}


Output: Hi (your name) glad to meet you.

First C Program




#include <stdio.h> 
#include <conio.h> 
void main()
{
            clrscr();
/* my first program in C */
            printf("Hello, World! \n"); 
            getch();
}

1.      The first line of the program #include <stdio.h> is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation.
2.      The second line of the program #include <conio.h> is a preprocessor command, which tells a C compiler to include conio.h file before going to actual compilation.
3.      The next line void main() is the main function where program execution begins. Void line tells the compiler that the main() function returns no values.
4.      clrscr() function is for clearing  the console.
5.      The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.
6.      The next line printf(“Hello, World!\n”); is another function available in C which causes the message "Hello, World!" to be displayed on the screen. \n is for entering to a new line.
7.      The getch(); helps to read from keyboard. The function helps to keep the console displayed.


This will be theresult displayed in the console.


Hello, World!