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

Wednesday, 24 February 2016

C Program to Implement Bubble Sort in C Programming

C Program to Implement Bubble Sort in C Programming

 
#include<stdio.h>
#include<conio.h>
 
void bubble_sort(int[], int);
 
void main() {
   int arr[30], num, i;
 
   printf("\nEnter no of elements :");
   scanf("%d", &num);
 
   printf("\nEnter array elements :");
   for (i = 0; i < num; i++)
      scanf("%d", &arr[i]);
 
   bubble_sort(arr, num);
   getch();
}
 
void bubble_sort(int iarr[], int num) {
   int i, j, k, temp;
 
   printf("\nUnsorted Data:");
   for (k = 0; k < num; k++) {
      printf("%5d", iarr[k]);
   }
 
   for (i = 1; i < num; i++) {
      for (j = 0; j < num - 1; j++) {
         if (iarr[j] > iarr[j + 1]) {
            temp = iarr[j];
            iarr[j] = iarr[j + 1];
            iarr[j + 1] = temp;
         }
      }
 
      printf("\nAfter pass %d : ", i);
      for (k = 0; k < num; k++) {
         printf("%5d", iarr[k]);
      }
   }
}

Monday, 15 February 2016

C Program to check Armstrong Number

#include <stdio.h>
#include<stdlib.h>
int main()
{
    int n, sum, temp, reminder;
    sum = 0;

    printf("Enter a number to check Armstrong Number : ");
    scanf("%d",&n);

    temp = n;
    while(temp!=0) {
        reminder = temp%10;
        sum = sum + reminder*reminder*reminder;
        temp = temp / 10;
    }
    if(n==sum) printf("%d is an Armstrong Number.",n);
    else printf("%d is not an Armstrong Number.",n);
    system("pause");
    return 0;
}



Checking a number if it is Even or Odd using Bitwise Operator in C









#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if(num&1)
        printf("%d is ODD\n", num);
    else
        printf("%d is EVEN\n", num);
        system("pause");
    return 0;
}

Output"





 
 

Reverse String using Pointer in C

#include <stdio.h>

int main()
{
    char str[1000], *ptr;
    int i, len;
    printf("Enter a string: ");
    gets(str);
    ptr = str;
    for(i=0;i<1000;i++){
        if(*ptr == '\0')  break;
        ptr++;
    }
    len = i;
    ptr--;
    printf("Reversed String: ");
    for(i=len; i>0; i--){
        printf("%c",*ptr--);
    }
    return 0;
}

Sunday, 14 February 2016

C program to find sum of series 1+x+x^2+……+x^n

C program to find sum of series 1+x+x^2+……+x^n.

Where X and Y are variables whose value is entered by user 


#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
int main()
{
int x,n,power;
int sol=0;
cout<<"Enter the values of x and n:";
cin>>x>>n;
for(int i=1;i<=n;i++)
{
    sol=sol+pow(x,i);
   
}
cout<<"Sum="<<sol<<endl;
system("pause");
return 0;
}

---------------------------------------------------------------------------
Output:




For help and questions leave comments....


C Program to Swap two numbers without third variable.

A Simple C Program to Swap two numbers without using another variable.


 #include<stdio.h>

int main() {
   int x,y;

   printf("Enter the value of X:");
   scanf("%d",&x);
    printf("\nEnter the value of Y:");
    scanf("%d",&y);
   x = x + y;
   y = x - y;
   x = x - y;

   printf("\nAfter swap value of X : %d", x);
   printf("\nAfter swapping value of Y : %d", y);

   return 0;
}

-----------------------------------------------------------------------------
Output:


 
For any question and help leave comments.......................

Writting text to a file using C language

A Simple program to write and read data form or to a file using C language.



#include<stdio.h>

int main()
{
  FILE *fp = fopen("fileName.txt", "a+");
  int ch = getc(fp);
  while (ch != EOF)
  {
    /* To display the contents of the file on the screen */
    putchar(ch);

    ch = getc(fp);
  }
  
  if (feof(fp))
     printf("\n Reached the end of file.");
  else
     printf("\n Something gone wrong.");
  fclose(fp);
    
  getchar();
  return 0;
}

================================================================

 
For furthur details and help leave comment........................

Adding Two Numbers In C Using Functions And Pointers

A Simple Program To Add Two Numbers Using Functions and Pointers, Entered By User.

 

#include<stdio.h>
int point(int *k,int *j);
int main(){

    int a,b,*p,*q,sum;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
point(&a,&b);
return 0;
}
int point(int *k,int *j)
{
    int u,o,sum;
    sum=*k+*j;
    printf("Sum is %d",sum);
    }

--------------------------------------------------------------------
For any question or help leave a comment....  

C/C++ program to Reverse six digit number

C/C++ program to reverse  six digits number entered by the user


#include<iostream>

using namespace std;

int main() {
    int num[7],rev_num = 0;;
    cout<<"Enter a number:";
    cin>>num;

    while(num>0) {
        rev_num += num-((num/10)*10);
        num /= 10;
        if (num>0) {
            rev_num *= 10;
    }}
    cout<<"\nNumber in reverse is="<<rev_num<<endl;
    }
------------------------------------------------------------------------
For any question and help leave a comment.......


Saturday, 13 February 2016

C program to move a car

Program in c using graphics move a car.

#include <graphics.h>
#include <dos.h>
#include <conio.h> 
 
main()
{
   int i, j = 0, gd = DETECT, gm;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
   outtextxy(25,240,"Press any key to view the moving car");
 
   getch();
   setviewport(0,0,639,440,1);
 
   for( i = 0 ; i <= 420 ; i = i + 10, j++ )
   {
      rectangle(50+i,275,150+i,400);
      rectangle(150+i,350,200+i,400);
      circle(75+i,410,10);
      circle(175+i,410,10);
      setcolor(j);
      delay(100);
 
      if( i == 420 )
         break;
 
      clearviewport();
   }
 
   getch();
   closegraph();
   return 0;
}

C Program to check if mouse support is available or not

C code

#include <dos.h>
#include <conio.h>
 
int initmouse();
 
union REGS i, o;
 
main()
{
   int status;
 
   status = initmouse();
 
   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
      printf("Mouse support available.\n");
 
   getch();
   return 0;
}
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}

Syntax of sin function in c

#include <stdio.h>
#include <math.h>
 
int main()
{
  double result, x = M_PI/6;
 
  result = sin(x);
 
  printf("The sin(%lf) = %.2lf\n", x, result);
 
  return 0;
}

C progarm to shutdown computer

C programming code for Windows XP

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   char ch;
 
   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c", &ch);
 
   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");
 
   return 0;
}
---------------------------------------------------------------------------

C programming code for Windows 7

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   system("C:\\WINDOWS\\System32\\shutdown /s");
 
   return 0;
}
 

C program to find IP

C program to get ip address: 

 

#include<stdlib.h>
 
int main()
{
   system("C:\\Windows\\System32\\ipconfig");
 
   return 0;
}

 

Begnning with C/C++

Simple c program

 Here is a simple program in C language. This programs display "My first program"
 on the screen.

 
  #include <stdio.h>                     // use to include header file
 
int main()                             // main function
{                                     //stating brace
  printf("My first program");         
  return 0;         //   returns 0 on the screen at the end of program
  
 

C/C++ Programming tips

What is C

C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. It wasn’t made the ‘official’ Bell Labs language. Thus, without any advertisement C’s reputation spread and its pool of users grew. Ritchie seems to have been rather surprised that so many programmers preferred C to older languages like FORTRAN or PL/I, or the newer ones like Pascal and APL. But, that's what happened.
Possibly why C seems so popular is because it is reliable, simple and easy to use. Moreover, in an industry where newer languages, tools and technologies emerge and vanish day in and day out, a language that has survived for more than 3 decades has to be really good. 
An opinion that is often heard today is – “C has been already superseded by languages like C++, C# and Java, so why bother to learn C today”.

 For more details leave comments..........................................................