Click below image for more tips

Wednesday, July 1, 2015

Write a c program to print Pascal triangle

0 comments
#include<stdio.h>

int main(){
  int length,i,j,k;

 //Accepting length from user
  printf("Enter the length of pascal's triangle : ");
  scanf("%d",&length);

 //Printing the pascal's triangle
  for(i=1;i<=length;i++) {
            for(j=1;j<=length-i;j++)
                        printf(" ");
            for(k=1;k<i;k++)
                        printf("%d",k);
            for(k=i;k>=1;k--)
                        printf("%d",k);
            printf("\n");
  }

  return 0;
}

Write a c program to reverse a number

0 comments
Code 1:
1. Write a c program to reverse a given number
2. C program to find reverse of a number
3. C program to reverse the digits of a number
4. Reverse of a number in c using while loop

#include<stdio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    while(num){
         r=num%10;
         reverse=reverse*10+r;
         num=num/10;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

Sample output:
Enter any number: 12
Reversed of number: 21

Code 2:
1. Reverse very large or big numbers beyond the range of long int
2. Reverse five digit number c program

Logic is we accept the number as string

#include<stdio.h>
#define MAX 1000

int main(){

    char num[MAX];
    int i=0,j,flag=0;

    printf("Enter any positive integer: ");
    scanf("%s",num);

    while(num[i]){
         if(num[i] < 48 || num[i] > 57){
             printf("Invalid integer number");
             return 0;
         }
         i++;
    }

    printf("Reverse: ");
    for(j=i-1;j>=0;j--)
         if(flag==0 &&  num[j] ==48){
         }
         else{
             printf("%c",num[j]);
             flag =1;
         }

    return 0;

Sample output:

Enter any positive integer: 234561000045645679001237800000000000
Reverse: 8732100976546540000165432

Code 3:
1. C program to reverse a number using for loop
2. How to find reverse of a number in c
3. Wap to reverse a number in c

#include<stdio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    for(;num!=0;num=num/10){
         r=num%10;
         reverse=reverse*10+r;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

Sample output:
Enter any number: 123
Reversed of number: 321

Code 4:
1. C program to reverse a number using recursion

#include<stdio.h>
int main(){
    int num,reverse;

    printf("Enter any number: ");
    scanf("%d",&num);

    reverse=rev(num);
    printf("Reverse of number: %d",reverse);
    return 0;
}

int rev(int num){
    static sum,r;

    if(num){
         r=num%10;
         sum=sum*10+r;
         rev(num/10);
    }
    else
         return 0;

    return sum;
}

Sample output:
Enter any number: 456
Reverse of number: 654

Write a c program to reverse a string

0 comments
Reverse a string in c without using temp
String reverse using strrev in c programming language

#include<stdio.h>
#include<string.h>
int main(){
    char str[50];
    char *rev;
    printf("Enter any string : ");
    scanf("%s",str);
    rev = strrev(str);
 
    printf("Reverse string is : %s",rev);
 
    return 0;
}

String reverse in c without using strrev
String reverse in c without using string function
How to reverse a string in c without using reverse function

#include<stdio.h>
int main(){
    char str[50];
    char rev[50];
    int i=-1,j=0;

    printf("Enter any string : ");
    scanf("%s",str);
 
    while(str[++i]!='\0');

    while(i>=0)
     rev[j++] = str[--i];

    rev[j]='\0';
 
    printf("Reverse of string is : %s",rev);
 
    return 0;
}

Sample output:
Enter any string : cquestionbank.blogspot.com
Reverse of string is : moc.topsgolb.knabnoitseuqc

Reverse a string in c using pointers
C program to reverse a string using pointers

#include<stdio.h>
int main(){
    char str[50];
    char rev[50];
    char *sptr = str;
    char *rptr = rev;
    int i=-1;

    printf("Enter any string : ");
    scanf("%s",str);
 
    while(*sptr){
     sptr++;
     i++;
    }

    while(i>=0){
     sptr--;
     *rptr = *sptr;
     rptr++;
     --i;
    }

    *rptr='\0';
 
    printf("Reverse of string is : %s",rev);
 
    return 0;
}


Sample output:
Enter any string : Pointer
Reverse of string is : retnioP

Write a c program to find the roots of a quadratic equation

0 comments
#include<stdio.h>
#include<math.h>
int main(){
    int a,b,c;
    double x,y,d;

    printf("Enter a , b , c of quadratic equation: ");
    scanf("%d%d%d",&a,&b,&c);

    //calculating the value of d
    d = sqrt(b * b - 4 * a * c);

    //Checking real solution is possible or not
    if(d<0){
         printf("Real number root is not possible");
         exit(1);
    }
    //finding the root of quadractic equation
    x = (-b + d) / 2 * a;
    y = (-b - d) / 2 * a;

    //printing the root of the quadractic equation
    printf("Solution of quadratic equation are %lf , %lf",x,y);

 
    return 0;
}

How to write a code in c

0 comments
Write any code in c language you must have two basic things:
1. Knowledge of syntax of c language.
2. Logic

To learn syntax of c language is very easy task. For a good programmer you must have own logic "how to do any task". C language is only used to convert you logic in text. This site is only helpful for to teach you syntax of c language that is easy part of any programming language also basic logic or approach how to write a c code. Most important point is logic of any code doesn’t come under c language. Now I am going to tell about c language.

1. Every c program start from main and ends with any null statement.
2. We can write preprocessor code in the c language to make it less platform dependent.
Basic syntax of c language is:
a. Include the header file which contains predefined function, data type etc.
b. One and only one main function
c. Any other function

All above three statements are optional.
Example of simple c program:  

#include<stdio.h>  //header file
int main(){
    printf("My first c program"); //to print any message.
    return 0;
}

Definition of variable in c

0 comments
Definition of variable in C programming language:

A variable is named location of data. In other word we can variable is container of data.

    In real world you have used various type containers for specific purpose. For example you have used suitcase to store clothes, match box to store match sticks etc. In the same way variables of different data type is used to store different types of data. For example integer variables are used to store integers char variables is used to store characters etc. On the basis of how many data a variable will store, we can categorize the all c variable in three groups.

(a)Variables which can store only one data at time. Example: integer variables, char variables, pointer variables etc.

(b)Variables which can store more than one data of similar type at a time. Example: array variables

(c) Variables, which can store more than one value of dissimilar type at a time. Example: structure or union variables.

Properties of variable in c:

    Every variable in c have three most fundamental attributes. They are:

1. Name

2. Value

3. Address

Name of a variable:

 Every variable in c has its own name. A variable without any name is name is not possible in c. Most important properties of variables name are its unique names. Not two variables in c can have same name with same visibility. For example:

#include<stdio.h>
int main(){
    auto int a=5;   //Visibility is within main block
    static int a=10; //Visibility is within main block

/* Two variables of same name */
    printf("%d",a);
    return 0;
}

Output: compilation error

But it is possible that two variable with same name but different visibility. In this case variable name can access only that variable which is more local. In c there is not any way to access global variable if any local variable is present of same name. For example:

(a)

#include<stdio.h>
int a=50; //Visibility is whole the program

int main(){
    int a=10; //Visibility within main block
    printf("%d",a);
    return 0;
}

Output: 10

(b)

#include<stdio.h>

int main(){
    int a=10; //Visibility within main block.
    {
        a+=5; //Accessing outer local variable a.
        int a=20; //Visibility within inner block.
        a+=10; //Accessing inner local variable a.  
        printf(“%d”,a);//Accessing inner local variable a.
    }

    printf(“%d”,a); //Accessing outer local variable a.
    return 0;
}

Output: 30 15

Note: In c any name is called identifier. This name can be variable name, function name, enum constant name, micro constant name, goto label name, any other data type name like structure, union, enum names or typedef name.


Identifier naming rule in c
Value of variable in c language
Address of a variable in c
Declaration of a variable in c

Monday, June 8, 2015

Get Started Of C language

0 comments
What is Compiler? see the following images:
compiler-exaple
conversation by an interpreter
There are three persons. First one knows only English and third person knows only chines. The second person knows both (English and chines). To continue the conversation middle person act as an interpreter. For example, when first person speak interpreter translates the English  into chines and then convey to the third person.
A compiler does the same as the interpreter did. C programming is readable for human. It is English like language but Computer does not understand the C programming. Compiler convert the C programming into machine code and command the computer to do what programmer wants. Thus compiler is as like as an interpreter between programmer and computer.

Prerequisite software (code block compiler):
You have to install a compiler to write C programming code. There are a lot of compiler to write C programming code. I would like to suggest you to install code block compiler. It is a nice compiler.  You can download it from the following link
Now install it as other software. If you are facing any problem to install just click the following link and see the video
Now you know how to write code in code block compiler. Well, let me explain a simple C programing code line by line.
1
2
3
4
5
6
#include
int main()
{
printf("Hello world");
return 0;
}
 line 1: we typed #include. By this line we include stdio.h file to our program. This file has been created while you installed code block. You should not worry about the commands written in  stdio.h file.
line2: Here is int main(). this is called main() function. Every C program has this function. Execution will start from the opening brace ‘{‘ followed by int main()
Line3: ‘{‘ it represents the starting point of main() function.
Line4: We typed here printf(“Hello world”); .  printf(); is a library function of C programming. inside the ‘()’ we provide our string to be printed. Here our string is Hello world. All commands to print a message by printf() function have been written insidestdio.h file.
line5: return 0; line tells the compiler to return ZERO where this main() function is called. Are you still confused about this explanation? don’t worry just skip and continue. We will discuss it broadly in FUNCTION topic.
Line6: ‘}’ is called closing brace which represents the end of opening brace, ‘{‘.
Now, you know how this 6 lines code works.