Posts

Showing posts from 2012

What are Header files?

        Header file contains different predefined functions, which are required to run the program. All header files should be included explicitly before main ( ) function.Header files are also called as Pre-compiled header. Here is some C programming header files ... <assert.h> <complex.h>  <ctype.h> <errno.h>   <fenv.h>   <float.h>   <graphic.h> <inttypes.h> <iso646> <limits.h> <locale.h> <math.h>   <process.h> <setjmp.h>   <signal.h> <stdalign.h> <stdatomic.h> <stdbool.h>  <stdarg.h> <stddef.h> <stdint.h>  <stdio.h>   <stdlib.h> <stdnoreturn.h>  <string.h> <threads.h>  <tgmath.h>   <time.h> <uchar.h> <wchar.h> <wctype.h> We can input using two methods 1. #include<Header file name >      example: #include<stdio.h> 2. #include &qu

C Program to convert string into ASCII values in c programming language

#include<stdio.h> int main(){ char str[100]; int i=0; printf("Enter any string: "); scanf("%s",str); printf("ASCII values of every characters of string: "); while(str[i]) printf("%d ",str[i++]); return 0; } Enter any string:ankit c guru ASCII values of every characters of string: 97 110 107 105 116

C program to change Upper case to lower case

/*by ankit gehlot website:ankitcguru.blogspot.com/ */ #include<stdio.h> int main(){ char str[20]; int i=0; printf("Enter any string :"); scanf("%s",str); while(str[i]!='\0'){ if(str[i]>=65&&str[i]<=97){ str[i]=str[i]+32;}i++; } printf("\nThe string in lowercase is :%s ",str); return 0; } Output:- Enter any string :ANKIT The string in lowercase is :ankit

C program to change lower case to upper case

/*by ankit gehlot website:ankitcguru.blogspot.com/ */ #include<stdio.h> int main(){ char str[20]; int i=0; printf("Enter any string :"); scanf("%s",str); while(str[i]!='\0'){ if(str[i]>=97&&str[i]<=122){ str[i]=str[i]-32;}i++; } printf("\nThe string in Upper case is :%s",str); return 0; } Output:- Enter any string :ankit The string in lowercase is :ANKIT

C program to print length of string

         C program to print length of string include<stdio.h> void main(){ char str[15]; int=0; printf("Enter any string :\t");  scanf("%s",&str); while(str[i]!='\0') { i++; printf("\nLength of string :\t%d",i); getch(); } Output:- Enter any string :      ankit c guru Length of string :      12 Note:- Compiler will also count blank spaces between words while compiling.

String

Image
                                                                    String String is a set(array) of character which is terminated by '\0'(null character). When compiler assigns string to character array then it automatically supplies null character ('\0') at the end of string. Thus, size of string = original length of string + 1. Declaration of string:-     char <string name>[roof character]; Example:-   char str[15]; Initialization of strings:-    char str[15]="ankit"; //or char str[15]={'a','n','k','i','t','\0'};//you must put '\o'(null)in this method //printing printf("%s",str);// string printing printf("%c",str[i]);// character printing //scanning scanf("%s",&str);// string scanning scanf("%c",&str[i]);// character scanning

Multiplication of Matrix

Image
                                                                                 Multiplication of Matrix C program to multiplication of matrix :-  #include <stdio.h> #include <conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j,k; clrscr(); printf("\n\t Enter First Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("\n\t Enter Second Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf("\n\t Matrix Multiplication is : \n\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\t %d",c[i][j]); } printf("\n"); } getch(); } Ouput:-  Enter First Matrix : 1 2 1 3 2 1 1 2 1 Enter Second Matrix : 3 3 3 1 2 1 1 1 1 Ma

Subtraction of matrix

Image
                                             Subtraction of matrix Subtraction of matrix means that one matrix is subtracted for another and result is putted into third matrix. C program to subtract matrix:-   #include <stdio.h> #include <conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j,k; clrscr(); printf("\n\t Enter First Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("\n\t Enter Second Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf("\n\t Matrix Multiplication is : \n\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\t %d",c[i][j]); } printf("\n"); } getch(); } Output:- Enter First Matrix : 2 1 2 3

Addition of matrix

Image
                                        Addition of matrix   Addition of matrix means that two matrix are added and addition of both matrix is putted in into third matrix. C program to add matrix:-   #include <stdio.h> #include <conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf("\n\t Enter First Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("\n\t Enter Second Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; } } printf("\n\t Matrix Addition is : \n\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\t %d",c[i][j]); } printf("\n"); } getch(); } Output :-  Enter First Matrix : 2 1 2 3 2 1 2 3 3 Enter Second Matrix : 2 1 0 2 3 0 1 1 2 Matrix Additio

Magic square matrix

Image
                                                                           Magic square matrix A magic square is a square array of  numbers consisting  of  the distinct positive integer ,arranged such that the sum of the numbers in any horizontal,vertical or main diagonal line is always the same number C program of Magic square matrix :-  void main() { int n, i, j, c, a[9][9] ; clrscr() ; printf("Enter the size of the magic square : ") ; scanf("%d", &n) ; if (n % 2 == 0) { printf("\nMagic square is not possible") ; goto end ; } printf("\nThe magic square for %d x %d is :\n\n", n, n) ; j = (n + 1) / 2 ; i = 1 ; for(c = 1 ; c <= n * n ; c++) { a[i][j] = c ; if(c % n == 0) { i = (i + 1); goto loop ; } if(i == 1) i = n ; else i = i - 1 ; if(j == n) j = 1; else j = j + 1 ; loop : ; } for (i = 1 ; i <= n ; i++) { for (j = 1 ; j <= n ; j++) { printf("%d\t", a[i][j]) ; } printf("\n\n") ; } end : ; getch(

Transpose of matrix

Image
                                                 Transpose of matrix The transpose of a matrix A is a matrix formed formed from A by interchanging the rows  and columns such that row i of a matrix  A becomes column i of the transposed matrix.The transpose of A is denoted by AT.Each element a a q in A becomes element a p in AT.If A is a m by n matrix ,then AT is an n and m matrix .The  following represents a matrix and its transpose. C program to transpose matix:-  #include <stdio.h> #include <conio.h> void main() { int a[3][3],i,j; clrscr(); printf("\n\t Enter Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("\n\t Transpose of Matrix is : \n\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\t%d",a[j][i]); } printf("\n"); } getch(); } Output:-   Enter Matrix : 2 1 2 3 2 1 2 3 3 Transpose of Matrix is : 2 3 2

Square matrix

Image
                                                             Square matrix     The square matrix is 2D-Array or matrix in which both number of  rows and columns are equal. Example 1:-     int list[5][5]; X = main diagonal -  = upper triangular + = lower triangular Example 2 :- int list[5][5]; main diagonal     = 2,5,5,8,7 upper triangular  = 3,5,8,4,7,9,8,2,3,2 lower triangular  = 2,8,3,3,5,3,7,1,2,5

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