Friday, 27 June 2014

C program for fibonacci series

Program :
#include<stdio.h>
void main()
 {
   int j=0,k=1,sum,i;
   printf("The fibonacci series is : %d %d",j,k);

   /* let us assume that we have to print 10 more numbers of this series*/
   for(i=1;i<=10;i++)
   {
     sum=j+k;
     printf("%d ",sum);
     j=k;
     k=sum;
   }
}














C program for Prime number detection

Program :
#include<stdio.h>
void main()
 {
   int num,a=2;
   printf("Enter any number");
   scanf("%d",&num);
   while(a<num)
     {
        if(num%a==0)
          {
             printf("%d is a non Prime number", num);
             break;
          }
        a++;
      }

    if(a==num)
     {
        printf("%d is a Prime number",num);
      }
   else if( num==0 || num==1)
      {
         printf("%d is neither Prime nor composite number",num);
       }
  }

C program for AND gate implementation

Program :
#include<stdio.h>
void main()
 {
   unsigned int x:1;  
   unsigned int y:1;                                   /* because of this x and y will only take either 1 or 0 value*/
   printf("Enter 1st binary number");
   scanf("%u",&x);
    printf("Enter 2nd binary number");
   scanf("%u",&y);
   x<=y ?  printf("Answer of AND operation is %u",x) : printf("Answer of AND operation is %u",y);
 }

C program for OR gate implementation

Program :
#include<stdio.h>
void main()
 {
   unsigned int x:1;  
   unsigned int y:1;                                   /* because of this x and y will only take either 1 or 0 value*/
   printf("Enter 1st binary number");
   scanf("%u",&x);
    printf("Enter 2nd binary number");
   scanf("%u",&y);
   x<=y ?  printf("Answer of OR operation is %u",y) : printf("Answer of OR operation is %u",x);
 }

C program for even and odd number detection

Program :
#include<stdio.h>
void main()
 {
   int num;
   printf("Enter any number");
   scanf("%d",&num);
   num%2==0 ?  printf("%d is even",num) : printf("%d is odd",num);
 }

Thursday, 26 June 2014

Examples of scanf() and printf()

Example 1 : Take a character input and print it.

Program :
#include<stdio.h>
void main()
 {
   char var;                                              /* Variable declaration*/
   printf("Please enter a character");         /* This is not necessary we can skip it */
   scanf("%c",&var);                               /*  Input stored in variable var*/
   printf(" You entered %c",var);             /*display of input value*/
}

Output :

Note : -  Press Enter key after typing each input to enter it in program. Like in above after typing @ press enter to give it as input to program.

Explanation : 
  • To use a variable in program, we have to first declare its name along with its type in starting of code. It is an error if you declare them later in code or after their use. Program starts with all variable declarations (if any) then further coding is done.
  • Second line is optional, if you won't write it the also you code works properly, but I have used it to convey my message properly to the user of program.
  • Third line takes input of a character from keyboard and store it in variable var. &var is necessary to write so for meanwhile learn it as it is, I'll explain this later. Every time you take input in a variable, don't forget to add & sign just before its name.
  • Last line displays the content of the variable. This is new use of printf() function. The printf() displays the content of var at same place where its type specifier is written.
Example 2 : Take age, 1st letter of name and height as input and display it back.


Program :
#include<stdio.h>
void main()
 {
   char name;
   int age;
   float height;
 
   printf("Enter your age :");
   scanf("%d",&age);
   printf("Enter 1st letter of your name");
   scanf("%c",&name);
   printf("Enter your height");
   scanf("%f",&height);

   printf("Your name : %c Your age %d Your height %f",name,age,height);
 }

Output :

Explanation :
  • 1st three lines declare required variables.
  • Next 6 lines takes input.
  • In last line printf prints whole string as it is but replacing all type specifiers in it with the values in variables. These variables are placed in list in the order which is similar to occurrence of their relevent type specifier.
We will see more such usage with time. Let us get introduced to some basic mathematical operators.


                      <<Back        Next>>

What are Variables

Variables are the arrangements in a programing language that are used to store data which is manipulated during the execution of the program.
Variable can store character or integer (whole numbers with their negative images) or a real number(called as floating point value).
For different types of variables write these statements:
char var;                            /*for character variable*/
int var;                               /*for integer variable*/
float var;                            /*for floating variable*/

  • A character variable can store only single character in it like : 'a' , '$' , '3' , '\n' etc.
  • An integer variable can store a number between -32768 to 32767.
  • A float variable can store -3.4E+38 to +3.4E+38
Naming Rules of Variables: Click Here

there are other types of variables also but we will see them later as we encounter their use.

<<Back        Next>>

Understanding scanf()

c and c++ tutorials // online learning spot
scanf(<field 1>,<field 2>) has 2 fields or 2 sections or we can say 2 parameters. These parameters are seperated by each other using a comma. It is compulsory to give 1st parameter and second parameter is optional ( we saw in previous example scanf("%c") ).

Now question arises what is this parameter?
To understand the word "parameter" let us have an example :
To calculate volume of a cube we needed its length breadth and height. These are called parameters of cube, that are needed to calculate its volume.
Similarly scanf() needed 1 compulsory parameter and 1 is optional.

What are types of scanf's parameters?
1st parameter is a string that contains type specifier of input and 2nd is a list of variables, these variables will store those input values.

What are Type Specifier?
They are used to identify the type of incoming data stream.
Some basic type specifier
  • character input : %c
  • integer input : %d
  • real number input : %f
we will later understand them more, now take a look on variables in next lesson.

<<Back        Next>>



Wednesday, 25 June 2014

How to take input in C

c and c++ tutorials // online learning spot
Now we will have a look on a new aspect, it is taking input in a progam of C:
There are many ways to handle the input task but our very first encounter will be with a simple function called scanf().
It is a function of stdio.h header file.

Let us make a program where we ask user to enter any character key from keyboard and after he enters, we will thank him.

Program

#include<stdio.h>
main()
 {
   printf("Enter a character from keyboard");
   scanf("%c");
   printf("Thank you !!!");
}

Explanation :
  • 1st line of code ask for a character input.
  • Then the code waits for an input.
  • Only after any valid input program prints Thank you !!!
  • We will learn more about scanf in next post.
<<Back        Next>>

Friday, 6 September 2013

E P 1 P 5 S

#include<stdio.h>
#include<conio.h>
#include<dos.h>
main()
{
printf("I");
delay(200);
printf("N");
delay(200);
printf("D");
delay(200);
printf("I");
delay(200);
printf("A");
delay(200);
clrscr();       /*clears all written on screen*/
printf("I");
delay(200);
printf("S");
clrscr();        /*clears all written on screen*/
delay(200);
printf("G");
delay(200);
printf("R");
delay(200);
printf("E");
delay(200);
printf("A");
delay(200);
printf("T");
delay(200);
}

This program give a feel of typing a text. Here we printed each letter individually and a very minute delay of only 200 milli-seconds, After completing each word we clear screen as only word we have to show.

Thursday, 5 September 2013

E P 1 P 3 S

#include<stdio.h>
#include<dos.h>
#include<conio.h>
main()
{
printf("I");     /* prints I*/
delay(1000);
clrscr();      /* Executed after 1 second and clears screen*/
printf("  N");  /*prints N leaving 2 spaces before it*/
delay(1000);
clrscr();
printf("    D ");  /*prints D leaving 4 spaces before it*/
delay(1000);
clrscr();
printf("      I");  /*prints I leaving 6 spaces before it*/
delay(1000);
clrscr();
printf("        A"); /*prints A leaving 8 spaces before it*/
delay(1000);
clrscr();
printf("I N D I A"); /*prints full I N D I A */
delay(1000);
}


E P 1 P 4 S

This program includes the use of one more Escape sequence i.e. back-space : '\b', it moves the cursor one position backward.

#include<stdio.h>
#include<dos.h>
main()
{
printf("I");
delay(1000);
printf("       A");
delay(1000);
printf("\b\b\b\b\b\b\bN");
delay(1000);
printf("   I");
delay(1000);
printf("\b\b\bD");
delay(2000);
}




  • We will step by step see the effect of code through pictures

1. printf("I");
    delay(1000);
    printf("       A");

2. printf("\b\b\b\b\b\b\bN");

you can clearly see the effect of 7 '\b' we put in our code. Cursor moves to next position after printing a letter always that is why after printing A we put 7 '\b' instead of 6. Count of '\b' depends on calculation.
  • The program proceed similarly and complete output is given with problem




E P 1 P 2 S

C and C++ // online learning spot
#include<stdio.h>
#include<conio.h>
#include<dos.h>
main()
{
 printf("I");          /* It prints I */
 delay(1000);    /* It compels program to wait for 1 second */
 clrscr();            /* It clears the screen and reset cursor at upper left corner */

 printf("N");
 delay(1000);
 clrscr();

 printf("D");
 delay(1000);
 clrscr();

 printf("I");
 delay(1000);
 clrscr();

 printf("A");
 delay(1000);
 clrscr();

 printf("INDIA");
 delay(1000);    /* Here delay is is used so that before ending of program it waits for 1 second and you can see output */


}



E P 1 P 1 S

c and c++ tutorials // online learning spot
point to note -- /* comment */ this is a format of a comment in between code. This has no significance or effect on code.

#include<stdio.d> /* included to use printf() in code */
#include<conio.h> /* included to use getch() in code */
main()
{
 printf("I");  /*prints I on screen*/
 getch();    /*wait for execution until any key pressed*/
 printf("\n  N"); /*prints N on screen in new line and leaving 2 spaces then program proceed similarly */
 getch();
 printf("\n    D");
 getch();
 printf("\n      I");
 getch();
 printf("\n        A");
 getch();
}


Wednesday, 4 September 2013

Exercise Programs 1

c and c++ tutorials // online learning spot
Problem 1. Print INDIA in steps (shown below), next letter should be printed when a key is pressed.

see solution


Practice Programs 1

c and c++ tutorials // online learning spot
Problem 1. Make a program to print your name on screen and program closes after you press any key.

#include<stdio.h>
#include<conio.h>
main()
 {

Introdution to delay getch clrscr

c and c++ tutorials // online learning spot
Till now we saw how to print some thing and how to format that thing, but now we will see some more monitor based functions that will add some pleasure to your program learning.
In this chapter we will learn about :
  • getch()
  • clrscr()
  • delay
and learn to use them.

Tuesday, 3 September 2013

Use of Escape Sequences

c and c++ tutorials // online learning spot
Escape Sequences are embedded in between the Strings that we compose to print on screen.
To print : online
                          learning
                                       spot

Understanding program elements

c and c++ tutorials // online learning spot
The elements we used are listed below :
  1. #
  2. include
  3. < >
  4. stdio.h
  5. main()
  6. { }
  7. printf("Hi this is my First C program")
  8. ;

First Program

c and c++ tutorials // online learning spot
To start with we will do some input - output work.....
So to just print any thing on screen in C, here is the program :

#include<stdio.h>
main()
{