Posts

Showing posts from September, 2012

2d array in c language

Image
                                          2D array in C# language The array which is used to represent and store data in a tabular form is called as 'two dimensional array.' Such type of array specially used to represent data in a matrix form. Syntax:-  <data type><array name>[No of rows][No of columns] int a[3][3]; Initialization of 2D Array:- <data type><array name>[No of rows][No of columns] int list[3][5]={{1,2,3,4,5}{6,7,8,9,10}{11,12,13,14,15}}; Memory Allocation :                                                       Column              Rows        Example:-  C# program which takes values for 2D-Array and print all in matrix form. #include<stdio.h> void main(){ int list[3][5],i,j; printf("Enter value in 2D - array:\n"); for(i=0;i<3;i++) { for(j=0;j<5;i++) { printf("value of list[%d][%d]=",i,j); scanf("%d",&list[i][j]); } } printf(&quo

C# program to sum members of array

           C# program to sum members of Array /*website: http://ankitcguru.blogspot.com/ *programmer:ankit gehlot*/ #include<stdio.h> #include<conio.h> void main() { int sum=0,i; int num[10]; for(i=0;i<10;i++){ printf("enter value in array:"); scanf("%d",&num[i]); sum=sum+num[i]; } printf("sum of array:%d",sum); getch(); } Output:-   enter value in array:1 enter value in array:2 enter value in array:3 enter value in array:4 enter value in array:5 enter value in array:6 enter value in array:7 enter value in array:8 enter value in array:9 enter value in array:10 sum of array:55

Array

                                                                        Array        Array is a collection of similar data type element which have save name but distinguish from index number. C# arrays are zero indexed; that is, the array indexes start at zero. Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences that you should be aware of.( reference ) Syntax:-   int salary [100]; data array name size Initialization:-  int num [5]={1,5};//or int num2[5]; num2[0]=13; num2[1]=14; num2[2]=15; num2[3]=16; With index address it looks like shown below:- address array value 65520 num2[0] 13 65522 num2[1] 14 65524 num2[2] 15 65526 num2[3] 16 Scaning of array:-  int arr[10],i; for(i=0;i<5;i++) {scanf("%d",&arr[i]); } Printing of array:-   int arr[10]={1,2,3},i; for(i=0;i<5;i++){ printf("%d",arr[i]); } Example:-program to print sum of array:- #i

Jumping Statement(break,continue,goto,exit() , return)

                                                      Jumping Statement break continue goto exit() return() Break:- The break statement breaks the execution of the current statement block and executes the first line that follow the block. We use break statement to terminate the loop once the condition gets satisfied.It can be used to end an infinite loop or force to end before its natural end. Syntax:- if(boolean condition) { ;//statements; break; } Example: #include <stdio.h> int main() { char c; for(;;) { printf( "\nPress any key, q to quit: " ); // Convert to character value scanf("%c", &c); if (c == 'q') break; } } // Loop exits only when 'Q' is pressed Continue:- The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block has reached, causing to jump to the start of the following iteration .

Looping Statements ( For loop ,while loop ,do while)

Image
                     Looping Statements  Executing a statement (or a group of statements) a number of times is called looping, and there are four statements for looping and iteration in C#. loops are used to repeat a lick of code being able to have your program repeatedly execute a block of code is one of the most basic but useful talks in programming.Al loops can do nesting(loops in loops )there are 3 loops in c#.  For loop while loop do while For loop:- The for loop is used when the number of loops is known.    Syntax:- for(i=1;i>1;i++) { printf("ankit c guru"); }  for(<variable> = <equals to example 1,x,a> ; <identifier> > <comparison with>;<identifier<increment/decrement>>) { \\statements; } Example:-print 1 to 100 numbers by for loop. #include<stdio.h> void main() {int i; for(i=0;i<=100;i++) { printf("\n%d",i); }  getch(); } while Loop:- With th

Branching(simple if,if else,nested if,else if,switch case )

                                                    Branching                                        (simple if,if else,nested if,else if,switch case ) In c language decision making is so important .Program should be able to do logical(bool or True \False) decisions. “IF statement” and “switch statement” are the most popular conditional statements used in C.These statements are used in most of program. Branch is the term given to the code executed in sequence as a result of change in the program’s flow; the program’s flow can be changed by conditional statements in that program.                                                       IF Statement IF Statement:- “If statement” is the selection statement used to select set of action depending on the given conditions . Therefore programmers can use this statement to control the flow of their program. If the program’s condition matches “if statement” condition then the code will be executed otherwise it will be

Basic structure of c program

                         Program structure I am going to explain basic structure of  basic structure of c# program.By this simple  you  can definitely understand how to write program? /* documentation ankit c guru http://ankitcguru.blospot.com/ *\ #include<stdio.h>//including header files which contains user defined functions #include<conio.h>//including header files which contains user defined functions #define pi 3.142 //This section is used to define symbolic constants #define maxmarks 100//This section is used to define symbolic constants #define minmarks 35//This section is used to define symbolic constants int a /*Global variables are declared out side the main function this can be used anywhere in program*/ void main()// main function { /*local variables*/ int d=10,e; float b, c;char name; //scanf(“format specifier”,&var1,&var2-----,&varn); scanf(“%d”,&d)//scanf() function is used to read the data from standard input device // for int

Printf and scanf functions

                   Printf and scanf functions printf and scanf are the two standard C programming language functions for console input and output.  A variation of these commands ( fprintf and fscanf ) also allows I/O to files.  Another ( sprintf and sscanf ) allows I/O to strings.  ( sscanf is especially useful.)  All these as well as many other I/O functions are declared in the standard header file <stdio.h> .  Using scanf is harder than printf .  This is because of promotion, so that any type smaller than an int is promoted to an int when passed to printf , and float s are promoted to double .  This means there is a single code for most integers and one for most floating point types.  There is no such promotion with scanf arguments, which are actually pointers.  So all types must be exactly specified.  In addition there are security concerns with input that don't apply to output.  (These will be discussed below.  In particular, never use the gets() function

Comma and Sizeof() operator

                                       Comma and Sizeof() operator      Comma Operator:- Comma operator( , ) is to separate declaration of two variables ,two data types.It is also helpful in scanning and printing,In built in functions. I can explain you with this below example:-   int a ,b ,c ,d; //<datatype><var.>,<var>;   float e; //<datatype><var.>,<var>;   printf("%d",a); scanf("%d",a);   Sizeof() :-  this operator is used get size of variable.I can explain you with this below example:-     int a,i; i=size of (int a) ; //out will be 2 because integer use 2 bytes in memory int =sizeof(datatype/variablr no.)   Operator Description Example sizeof() Returns the size of an variable. sizeof(char  a), where a is interger, will return 1. sizeof() Returns the size of an variable. sizeof(int  a), where a is interger, will return 2. sizeof() Returns the size of an variable. sizeof(float a),

Bitwise operator

                           Bitwise operator The c# bitwise operators I shall be covering here and are of particular interest to me in my followup post are: Bitwise AND( & ) Bitwise OR( | ) Bitwise NOT ( ! ) Bitwise XOR (^) Bitwise Shift operator Bitwise AND( & ):- And operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true. A B A & B True True True True False True False True True False False False   Bitwise OR( | ):- OR operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false. A B A | B True True True True False F

Conditional operator

                                                                                           Condition operator The Conditional operator (?:) is a ternary operator (it takes three operands)the condition operator work as follows:- The first operand is implicity converted to bool(true or false) It is evaluated and all side effects are completed before continuing. The conditional operator allows you to define a condition that returns a Boolean value. In addition to the condition, two expressions are provided. Only one of these expressions will be evaluated when the program is running, being selected according to the result of the condition. If the first operand  evaluates to true ( 1 ) or true, the second operand is evaluated. If the first operand evaluates to false ( 0 ) or false, the third operand is evaluated.   Examples:-   Z=(a>b)?a:b; // condition ? true-expression : false-expression

Logical operators

                            Logical operators Operator Name && logical and || logical or ! logical not Not (!) T=F F=T Operator   And (&&) Or ( || )  T T T T F T F F T F F F Example:-   int a=10, b=20 a>5 && b<15                               (T)             (T)         (T) a>5||b<15                                    (F)            (T)      (F) a>5||b<15                                    (T)           (T)      (F) !(b<15)                                        (T)              (F) !(a>5)                                          (F)             (T)   Operator Description Example == Checks if the value of two operands is equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greate

Increment and decrement operator

                                Increment and decrement operator C# provides two operators that can be used to increase or decrease a variable's value by one. These are the increment operator (++) and decrement operator (--). These operators are known as unary operators because they are applied to a single value. The operators can be placed before the variable to be adjusted (prefix) or after it (postfix). postfix=i++;( In post i=i+i) prefix=++i;   (In pre i=i+i)                                                     Increment operator Example:- int a; int b; // Prefix.  a is incremented before its value is assigned to b a = 10; b = ++a;        //a = 11, b = 11; // Postfix.  a is incremented after its value is assigned to b a = 10; b = a++;        //a = 11, b = 10; One more example to explain you better int i=5,j=9,a,b; a=i++;  // output  5  b=++j; // output  10                                                Decrement operator

Assignment operator

                                                                                   Assignment operator A compound assignment operator is used to simplify the coding of some expressions. For example, using the operators described earlier we can increase a variable's value by ten using the following code: Using normal way:- a=a+2; a=a+b+2; a=a-2; a=a*4; a=a/5; a=a%2; The following code gives examples for addition (+=), subtraction (-=), multiplication (*=), division (/=) and modulus (%=): Using assignment operator:- a+=2; a+=b+2; a-=2; a*=4; a/=5; a%=2;    You may observe that by using assignment operator .It can save time,simplify the some expressions(code)  and expressions become sorter. Explaination by table :- Operator Description Example = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne value of A + B into C += Add AND assignment operator, It adds right operand to the le

Relational Operator

                                              Relational Operator These useful operators permit the developer to compare two values and make decisions based upon the result of the comparison. > Greater than >= Grater than equals to < Less than <= Less than equals to == Equals to != Not equals to Example:     int a=20;                  a>5 True a>25 False a>20 False a<=20 True a<25 True a<20 False a<=20 True a==20 True a==10 False a!=20 False a!=10 True

Arithmetic operators

                                                 Arithmetic operators There are five basic arithmetic operators available in C#. These are used for addition (+), subtraction (-), multiplication (*), division (/) and modulus (%). Of these, the first four are available to almost every programming language.     Operator Name + add - sub * mul / div % modulus (used for reminder)  Syntax:- int k=10,a,b,c,d,e;                                   a=k%2;     //output is 0 b=k+2;     //output is 12 c=k-2;     //output is 8 d=k*2;     //output is 20 e=k/2;    //output is 5                                                     

Operators

                                                                                                            Operators      Operators are special symbol which are used for arithmetic & logical calculations.C# provides a large set of operators, which are symbols that specify which operations to perform in an expression. Operations on integral types such as == , != , < , > , <= , >= , binary + , binary - , ^ , & , | , ~ , ++ , -- , and sizeof() are generally allowed on enumerations. In addition, many operators can be overloaded by the user, thus changing their meaning when applied to a user-defined type. Types of Operators in C# are as follows:- Parentheses operator Arithmetic operators Assignment operators Relational operators Logical operators Conditional operators / ternary operator Increment / Decrement operator Bitwize operators Sizeof(),comma operator Category  Operator  Associativity  Postfix  () [] -> . ++ - -   Left

Double data type

                                                        Double data type Double is used to define big floating point numbers,it reserves twice the storage for the number .On PCs this likely to be 8 bytes. Initialization:- Syntax:- double<value name>=<value>; Example:- double k=158.368; printf("double:%lf",k); Output: double:158.368 Runtime  initialization of double type variable:- scanf("%lf",&k);