Powered By Blogger

Tuesday 16 April 2013

Some Basic Examples

Q. WAP to Enter 5 Numbers and display the Sum ?

 #include<stdio.h>
 main()
{
int val1,sum=0,i;
printf("Please Enter 5 Numbers:\n");
for(i=0;i<5;i++)
{
scanf("%d",&val1);
sum+=val1;
}
printf("Sum is: %d",sum);
}

Q. WAP to Reverse a three digit Number using simple Logic ?


#include<stdio.h>
main()
{
            int a;
            printf("Enter a three digit number\n");
            scanf("%d",&a);
            int p = a % 10;
            int y=  a / 10;
            int q = y % 10;
            int o = y / 10;
            int r = o % 10;
            int c =  p * 100 + q * 10 + r * 1;
            printf("%d",c);
}

Q. WAP to Find Sum of N Numbers ?

#include <stdio.h>
main()
{
    int N, sum = 0;
    printf("Program for sum of all numbers from 0 - N\n");
    printf("Enter N: ");
    scanf("%d", &N);
    sum = N * (N+1) / 2;
    printf("The sum of all numbers between 0 and %d is: %d", N, sum);
}

Q. WAP to Display the Table of any Number Entered by user ?

#include<stdio.h>
main()  
{
    int i,num;
    printf("Please Enter the Number:\n");
    scanf("%d",&num);
    printf("Table of  %d  is: \n",num);
    for(i=1;i<=10;i++)
    {
    printf("%d * %d = %d\n",num,i,num*i);    
    } 
}

Q. WAP to Reverse any digit Number ?

#include<stdio.h>
main ()
{
int no,r,res;
printf("Enter any value: ");
scanf("%d",&no);
r=res=0;
while(no>0)
{
r=no%10;
no=no/10;
res=(res*10)+r;
}
printf("\nReverse is %d",res);
}

Thursday 17 January 2013

Constructs Examples (Contd..)

------------
do...while
------------

#include<stdio.h>
using namespace std;
main()
{
 int a;
 do
 {
  printf("Enter a:\n");
        scanf("%d",&a);

 }
 while(a!=2);
}



----------
for loop
----------

#include<stdio.h>
using namespace std;
main()
{

 for(int a=1;a<=5;a++)
 {
  printf("\t \n%d\t\t",a*5);
 }
}



























Constructs Examples

--------
if..else
--------

#include<stdio.h>
using namespace std;
main()
{
 int a,b;
 a=2;
 b=3;
 if(a>b)
 {
  printf("a is greater than b\n");
 
 }
 else
 {
  printf("b is greater than a\n");
 
 }
}
 


---------------
switch..case
---------------

#include<stdio.h>
using namespace std;
main()
{
    int op;
    float a;
    float b;
    float result;
    while(1)
    {
    printf("Select the Arithmetic Operation: \n 1 - Addition\n 2 - Subtraction\n 3 - Multiplication\n 4 - Division\n \n \n");
    scanf("%d", &op);
    printf("Enter first number: ");
    scanf("%f", &a);
    printf("Enter second number: ");
    scanf("%f", &b);
   
    switch (op)
    {
        case 1:
            result = a + b;
            break;
        case 2:
            result = a - b;
            break;
        case 3:
            result = a * b;
            break;
        case 4:
            if (b == 0)
            {
                printf("Cannot divide by zero.");
            }
            result = a / b;
            break;
      
            default:
                {
                 printf("invalid choice");
                 break;
                }   
}
printf("The result is: %f\n",result);
printf("\n");
}
}


-------------
while loop
-------------

#include<stdio.h>
using namespace std;
main()
{
 char ch;
 printf("Enter your choice:\n");
 scanf("%c",&ch);
 while(ch!='y')
 {
 
  printf("No\n");
  printf("No\n");
  printf("No\n");
  printf("No\n");
  break;
 }
}

Tuesday 15 January 2013

Constructs

Constrcuts are used for specific purpose ,like if you want to include statemtnts like ,the number is greater or number is b greater or you can say ,if you want to check this i good or that is good or this is bad and that is good like statements .

Constructs are of two main types :
Conditional Constructs
Loop Constructs

Conditional Constructs are used when you specify conditions in you C Program .
They are :

      if...else
     switch...case

Syntax for if...else :-

if(condition)
{
statement to be executed;
}
else
{
statement to be executed;
}

Syntax for switch...case :-

switch(variable/condition)
{
case 1:
statement to be executed;
break;
case 2:
statement to be executed;
break;
.
.
.
.
.
default:
statement to be executed;
break;
}

*break statement is used to terminate the case statement and move the compiler to the outer area of the construct or to the given statement.
*default is used for an additional case to be executed ,in case of any unexpected condition occurs .

Loop Constructs are used if you want to repeat/ Iterate the program or a specified condition for several times .
They are :-

     while loop
     do..while loop
     for loop

 Syntax for while loop :-

while(Condition)
{
statement to be executed;
}

Syntax for do...while loop :-

do
{
statement to be executed;
}
while(Condition)

Difference between while loop & do...while loop :

while loop & do..while loop are almost same ,but the only major difference is that do..while loop checks the condition after the execution of body .

 Syntax for for loop :-

for(Initializing; condition; Increment /Decrements )
{
statement to be executed;
}

the first stage in for loop is initializing the variable ,then condition to be checked ,the what will happen if the specify given condition is true ,increment or Decrements .

Some Operators for Constructs :

Arithmetic Operators 
(+ ,- ,* ,/ ,%)

+ is used for addition
- is used for difference
* is used for multiplication
/ is used for division
% is used to find out remainder

Logical Operators
(&&, ||, !)

&& is AND Operator ,used to find out the whole condition is true or false
|| is OR Operator ,used to find out any condition is true or false
! is NOT Operator ,used to find out or you can say define Negative Result

Unary Operators
(++, --)

++ used to increment the value of any variable or an operand by 1
-- used to decrements the value of any variable or an operand by 1
    also applied on single operands .

Comparison Operators
(>, <, >= ,<= ,==, !=)

> is greater than
< is less than
>= is greater than equal to
<= is less than equal to
== equal equal to
!= not equal to

and many more ...

Monday 14 January 2013

I-P-O

I-P-O stands for Input-Process-Output ,each and every program in any programming language performs I-P-O operation ,I/O (Input-Output) is all we need in our C Program .
 
In C Language :
stdin is used for Input
stdout is used for Output
 
Input & Output takes place as a stream of character .
we can perform or use two types of ,mainly two types of Input & Output ,
 
1.Character-Based I/O
2.String-Based I/O
 
 
Character-based I/O includes :-
 
#getc()
#putc()
#getchar()
#putchar()
 
getc() : getc() is also a function/inbuilt function ,or you can say a keyword .getc() is used to accept a single character .example : getc(a); ,so this statement will accept a single character 'a' .
 
putc() : putc() is also a function/ inbuilt function ,putc() is used to generate an output or to display the accepted character on console window (Console window is the responsible window ,where you actually visualize you output of you C Program) .
 
Illustration :
 
#include<stdio.h>
main()
{
 char c;
 c=getc(stdin);
 putc(c,stdout);

 
so here we are declaring a character type variable c ,c=getc(stdin); statement is storing and accepting variable's value ,putc(c,stdout); is used to display the stored variable's value onto the console :
 

 
 
getchar() : is also used to accept a single character ,the only difference between getc() and getchar() is about parameters, parameters are the additional information we pass between (...) .
 
putchar() : is also used to display a single character ,parameters are bit different as compare to
 putc() .
 
Illustration :
 
#include<stdio.h>
main()
{
 char c;
 c=getchar();
 putchar(c);
 printf("\n");
}


 
 
you can see here that there is no need to specify stdin or stdout .so a different way to implement character-based I/O operations .

String-Based I/O includes :-

#gets()
#puts()

gets() : is used to accept a string (sequence of characters ) ,example : "Programming" .
puts() : is used to display the string

Illustration :
#include<stdio.h>
main()
{
 char c[20];
 puts("Enter Something :\n");
 gets(c);
 puts("You Entered: ");
 puts(c);
 printf("\n");
}


so here we are declaring a character type variable c which holds sequence of  20 character .
you can see here gets and puts used to accept and display strings .



check it out!

#include<stdio.h>
main()
{
printf("Hi, ");
disp_msg();
printf(" Enjoy Learning ");
}
disp_msg()
{
printf("Welcome to C Programming,");
}

What will be the output?
-Hi, Enjoy Learning Welcome to C Programming,

No, the above generated output is not correct ,when you execute this ,you'll see :
-Hi, Welcome to C Programming, Enjoy Learning

because compiler executes the statements step-by-step ,so it goes the printf("Hi, "), then disp_msg(),
disp_msg() having some information which is in disp_msg(){...} block, so compiler executes it first ,
disp_msg()
{
printf("Welcome to C Programming,");
}
then printf(" Enjoy Learning "), so the output will be :
Hi, Welcome to C Programming, Enjoy Learning

Functions

Functions as u have seen earlier ,used to transfer messages and data in our C Program. Functions contain all the information/ data. it can be single level function as well as multi-level function.
Single Level Function
Consider the following single-level function :

main()
{
printf("single-level function\n");
}

So, in the above illustration main is the only function available .

Multi-level Function
you can create multiple functions in your C Program ,or you can say whenever a user create a function by itself (user-created function) .
Consider this :

main()
{
-
-
-
}
cprog()
{
-
-
-
}
...

so, here you can see cprog() ,is a user0defined function ,added to the C code ,that's multi-level function's example .

There are some predefined Functions available in library :-

strlen() is used to find length of a given string
strcpy() is used to copy strings
strcmp() is used to compare strings
strcat() is used to concantinate strings

strlen() example: 

#include <stdio.h>
#include <string.h>

int main ()
{
  char szInput[256];
  printf ("Enter a sentence: ");
  gets (szInput);
  printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
  return 0;
}

 strcpy() example


#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

strcat() example


#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}




Friday 11 January 2013

A Simple Program to add two numbers

There are many compilers available to run C programs, some compilers come with some IDE (Integrated Development Enviroment ).
I'm using here is C-Free(Software for creating and Compiling C programs), you can use any of these like: Turbo C++, Turbo C, DJGPP ,Dev C, Dev C++, etc..
 
 
#include<stdio.h>
main()
{
int a, b, c;
printf("Enter your First Numer:\n");
scanf("%d",&a);
printf("Enter your Second Number:\n");
scanf("%d",&b);
c = a + b;
printf("Result/Addition is:\%d n",c);
}

scanf("%d",&a):- is used here to store and to refer the integer value to a variable, fro example here whatever the value is entered by the user ,stored in the variable name : a, %d is defining which type of data type is going to store in the variable 'a'. &a is used to refer that particular value, tells the compiler to store the value into the variable 'a'.
 
So what is happening here is :
 

So when you run this program it will show an output like above

How to Define Data Types?

The Syntax for Defining a Data type is:
<Data Type> <Variable Name> , . . . . . . ;
Declaration is done at the beginning of function :


Data Definition

Data type

Memory Defined

Size (bytes)

Value Assigned

char a, c;

char

a

c

1

1

-

-

char a=’Z’

char

a

1

Z

int a;

int

a

4

-

int a = 2;

int

a

4

2

float a;

float

a

4

-

float a = 2.3;

float

a

4

2.3

 So, this is how you declare & define data types in your C program.

 

 
 

Data Types!

Data Type defines which type of data we are actually using in our C program, for example if you want to make a program, which simply add two numbers ,so the number should contain an integer value or for example: 2,3,4,5,....
The 2,3... like Digits called Integers.
 
We have generally two types of data types:
 
1.Fundamentals
2.Derived
 
Fundamental Data Types are the base of other data types ,most commonly used data types in any C program.They are:
int, float, char
"int" stores only integer values (2,3,4,.....)
"float" stores Decimal values (2.3,3.4,....)
"char" stores character type values (a, b, A, B, ....)

 Storage Requirement for Fundamental Data types in memory:

 

Data

Bytes Consume

Range Min

Range Max

Char

1

-128

127

Int

4

-2^31

(2^31)-1

float

4

6 digit of precision

6 digit of precision


Derived Data Types are the type of data types which are actually modification of Fundamental Data Types:
short int, long int, double float
 Storage Requirements for Derived Data types in memory:

Data

Bytes Consume

Range Min

Range Max

short int

2

-2^15

(2^15)-1

long Int

4

-2^31

(2^31)-1

double float

8

6 digit of precision

12 digit of precision

Variables?

So, you have learnt the structure for creating the C program, no here a term that stuck in mind -"Variables in C Language"
    Variable basically is a named location in system memory which contain some value or data.


 
     
              5
                      A
 



So here, you can see a variable named 'A' ,occupied a space in the memory with a value '5'. variable can changeable ,because as its name specify "vari-able" .So you can change the value anytime ,if you wish to change it.
 

Thursday 10 January 2013

Let's get Started!

A Simple Program in C Language

#include<stdio.h>
main()
{
printf("Hello ,you are @www.seekhle.blogspot.com");
}
 
So this is how you create your programs in C Programming language, this is the way of writing some instructions .Explanation :
 
#include<stdio.h> is a Preprocessor Directive, It tells the compiler to include this inbuilt system files /libraries (Input Output) into our little program, we are actually telling are compiler what we are doing in our program ,is also known as Header Files .
 
main() is a default function, Function/ Member Functions used to transfer data or passes messages in our program. This is a default function because main is a Keyword(Inbuilt). The compiler automatically executes the main() first of all, then move to next statement in the program.
 
{...} is known as local block, contains everything and is responsible only for the specific block, for example here main() .
 
printf("...") is also an inbuilt keyword used for output, or to display the content contain under " " .
for example here, if executes this program ,this will generate an output:
 
                                                                     Hello, you are @www.seekhle.blogspot.com
 
 -remember one thing after any statement you have to write a ; .It tells the compiler to terminate the statement and move further.

About C

is a general-purpose programming language ,developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs . C is one of the most widely used programming languages of all time .

C Programming Language is based on Structured Programming Paradigms(Concept). In Structural Concept Execution of a program is done in a sequential manner. (Execution means ,when a program comes in a mode where we actually see its real working).
Structured programs are often composed of simple, hierarchical program flow structures.

Example:

Start                       

---------                                 Code
---------                                 Code

Exit

So Structural Programs Contains a Single Entry and a Single Exit Point, Step-by-Step Execution of a Program is done.
 
Features of C Programming Language
 
Conciseness : It is easy to use
Maintainability : It is easy to modify
Portable : It is easy to Transfer/Share
 
C Language is Compiler Based Language, A Compiler is a tool that translates English-Like words to Machine Language (0s & 1s), because Machine can understands only Binary Language(0s &1s).
 

What is a Programming Language?

A Programming Language is an Artificial Language (example: C/C++ ), we used to create or to write programs.
C/C++ is a Programming Language through which we create our programs.

What is Programming?

Programming is a process of writing some instruction in any programming language, to achieve a desired task, or simply to create programs (package of combined instructions).