Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

Friday, March 1, 2019

Structure of C Program

In this post i am sharing structure of C Program. Actually C Program structure divided in to several sections. They are


Structure of C program is as follow:

1)Documentation:

Consist of comment that gives description of  the program or programmer,problem
   Ex:
   /*Prog for addition of two no */

2)Link Section:
 
For  a compiler to link function from library function.
  Ex:
  #include<stdio.h>
  #include<conio.h>

3)Definition Section:
 
Define all symbolic constant.
   Ex:
   void()

4)Global Declaration Section:
   
For declaration of variable as global variable which can accessed by all function of a program.
     Ex:
     int x=10;

5)Main Function Section:
 
This the most important part of the program.It contain declaration part and execution part of program.
    Ex:
    main()
    {
     Printf("Hello World!");
    }

6)Sub Program Section:
 
This include user define function.This function are placed after the main function.
    Ex:
    void()
    {
      printf("value inside fun() %d",a);
     }

Example of C program:


  1. /*Documentation Section: program to find the area of circle*/
  2. #include<stdio.h>   /*link section*/
  3. #include<conio.h>  /*link section*/
  4. #define PI 3.14     /*definition section*/
  5. float area;          /*global declaration  section*/
  6. void main()
  7. {
  8. float r;                /*declaration part*/
  9. printf("Enter the radius of the circle\n");   /*executable part starts here*/
  10. scanf("%f",&r);
  11. area=PI*r*r;
  12. printf("Area of the circle=%f",area);
  13. getch();
  14. }



Sunday, December 2, 2018

Write a C Program to swap two numbers without using third variable

In this post you will learn simple C Example to swap two numbers without using third variable. In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables. Usually, this is done with the data in memory.

Program 1:

#include<stdio.h>
#include<conio.h>
main()
{
int a=10, b=20;
clrscr();
printf("Before swap a=%d b=%d",a,b);
a=a+b; //a=30 (10+20)
b=a-b; //b=10 (30-20)
a=a-b; //a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
getch();
}

Output:

Before swap a=10 b=20
After swap a=20 b=10
Program 2: Using * and /

Let's see another example to swap two numbers using * and /.

Program 2:

#include<stdio.h>
#include<conio.h>
main()
{
int a=10, b=20;
clrscr();
printf("Before swap a=%d b=%d",a,b);
a=a*b; //a=200 (10*20)
b=a/b; //b=10 (200/20)
a=a/b; //a=20 (200/10)
printf("\nAfter swap a=%d b=%d",a,b);
getch();
}


Output:

Before swap a=10 b=20
After swap a=20 b=10


Program 3: Making use of bitwise XOR operator:

The bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ. For example XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010).

#include <stdio.h>
int main()
{
 int x = 10, y = 5;
 // Code to swap 'x' (1010) and 'y' (0101)
 x = x ^ y;  // x now becomes 15 (1111)
 y = x ^ y;  // y becomes 10 (1010)
 x = x ^ y;  // x becomes 5 (0101)
 printf("After Swapping: x = %d, y = %d", x, y);
 return 0;
}

Output:

After Swapping: x = 5, y = 10

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...