Introduction

The main purpose of this blog is to share our experience with the world and get experience from the world. This will be a new venture for us and we are sure, we shall learn a lot with it. we will try to Explore our mind which will be very helpful for all us. As from its name, its a blog covering all the topics. we will try to post all the interesting topics and give our opinion. Do give your point of view in comments.


"NAVIGATE VIA BLOG ARCHIVE ON RIGHT TO GET WHAT YOU ARE LOOKING FOR"


Friday, October 14, 2011

C Code For System Of Linear Equations Solver

//this is the program  for gaussian elimination method for solving system of equations

#include<stdio.h>         //preprocessor library used in the program

      //prototypes
void starter();           //this function displays the start of the program
void getvalues(int R,int C,float A[][C+1]);//this function get values of augmented values
void printmatrix(int R,int C,float A[][C+1]);           //function  will print the matrix
void Swap(int R,int C,float A[][C+1]);        //function will swap the rows of the matrix
float Echelon(int R1,int C1,float A1[][C1+1]);//function will convert matrix in echelon form
float Infinite(int R2,int C2,float A2[][C2+1],float X1[]); //function for infinite sol. case
float Unique(int R3,int C3, float A3[][C3+1],float X2[]);        //function for no sol. case
void solution(int R,int C,float A[][C+1],float X[]);   //function  shows the solution result
void endresult();                                          //function which ends the program

int main()                                                     //main function starts here
{
    starter();                                                   //call to starter function
    int i,j,R,C;                                    //declaration of variables used in main
              //now it will take user input for how many unknown variables of each equation
    printf("\n\tEnter Number of unknown variables.\n\t");                    //ask the user
    scanf("   %d",&R);                                            //get the value from user
    C=R;                      //no.of unknown vaariables is always equal to no of equations
    float A[R][C+1];          //declaration of array which contains the augmented matrix
    float X[10]={0};          //declaring array which contains row to swap
    getvalues(R,C,A);         //call for getting the input for augmented matrix
    printf("\tThe augmented matrix of the system of equations is\n");
    printmatrix(R,C,A);        //call to show the matrix before echelon form
    Swap(R,C,A);               //call to check the matrix for any zero value and then swap
    Echelon(R,C,A);            //call for converting in echelon form
    printf("\tAfter reducing the matrix in echelon form\n");
    printmatrix(R,C,A);        //print the matrix after echelon form
    solution(R,C,A,X);         //call to display the final result
    endresult();               //call to end the program
       return 0;               //ensures program ends
}
//main bloack ends

          //function  for starting page starts here
void starter()//definition of the funtion starter of void type
{
     //the very first welcoming screen display
    printf("\n_______________________________________________________________");
    printf("\n!!      ****************** ASSLAM U ALAIKUM ******************  !!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!      \t WELCOME TO THE WORLD OF LINEAR EQUATIONS \t!!");
    printf("\n!!      \t YOU CAN SOLVE ANY KIND OF EQUATION USING \t!!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!      \t \tGAUSSIAN ELIMINATION METHOD \t\t!!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!______________________________________________________________!!\n");
    printf("\n!!      ----------------GENERAL INSTRUCTION-------------------  !!");
    printf("\n!!      1)Number of equations and variables should be equal.    !!");
    printf("\n!!      2)Gaussian method is applicable for square matrices.    !!");
    printf("\n!!      3)Equations  in the form of matrix are as AX=B  \t!!\n!!\t where\t\t\t\t\t\t\t!!\n!!\t  A is the matrix of coefficients.\t\t\t!!");
    printf("\n!!        B is matrix for constants.\t\t\t\t!!");
    printf("\n!!        X is the unknown matrix which contains variables.     !!");
    printf("\n!!______________________________________________________________!!\n");
   //general instructions for the user are also included so that he could understand
   // what he is doing
}
         //starter function ends here
         //definition of getvalues function starts here
    //it will get the co efficient of the variables and adjust them in an array
void getvalues(int R,int C,float A[][C+1])
{
     int i,j;   //declare i for row check and j for colomn check
     printf("\n\tEnter coefficients which are the elements of matrix A\n");
     int equno=0;  //asking user for input and equno is used to checkfor no of equation
     for (i=0; i<R; i++)     //this for loop will move through each row
     {
         equno++;        //increment in variable
         printf("\tequation %d \n",equno); //show equation no.whose values are entered
         for (j=0;j<C; j++)  //for loop will move through each coloumn
         {
             printf("\t  A[%d][%d]   ",i+1,j+1); 
             scanf("%f",&A[i][j]);    //takes user input and adjust in the array
          }
          printf("\n");    //switch lines
      }
      printf("\tEnter Elements Of Matix B\n");  //asks for values in constant matrix
      for (i=0; i<R; i++)      //loop for rows
      {
          for (j=C; j<C+1; j++)  //loop for coloumn
          {
              printf("\t  B[%d][%d]   ",i+1,j+1);
              scanf("%f",&A[i][j]);   //takes input and adjust in last colomn of array
          }
          printf("\n"); // switch lines
      }
}//getvalues function ends here

    //function for printing matrix
   //printing the values of the array in augmented matrix
void printmatrix(int R,int C,float A[][C+1])
{
     int i,j;  //declare i for row check and j for colomn check
     for (i=0; i<R; i++) //this for loop will move through each row
     {
         for (j=0; j<C+1; j++)   //loop for coloumn
         {
             if(j!=C)  //print the values of matrix A
             {printf("\t%.1f  ",A[i][j]);}
             if(j==C)  //prints the vaalues of matrix B
             {printf("|\t%.1f  ",A[i][j]);}
         }
             printf("\n");//switch lines
     }
     printf("\n\n");//switch lines
}//printmatrix funcion ends here

   //function for swapping rows
  //this swap rows if there is zero in the diagonal
void Swap(int R,int C,float A[][C+1])
{
      int i,j,Counter1,Counter2,Counter3,Temp2,red=0;  //declaration variables
      float Temp[C+1];      //declare an array for swaping assistance
      for (i=0; i<R; i++)      //loop for rows
      {
           for (j=0; j<C+1; j++)      //loop for coloumn
           {    if (i==j)    //checks only for element in diagonal
                {   if  (A[i][i]==0)   //check for 0 in diagonal
                    {   for (Counter3=0; Counter3<R; Counter3++)
                        {    if (A[Counter3][j]!=0)    
                             {
                                 Temp2=Counter3;
                              }
                         }
                         {
                                 for (Counter2=0; Counter2<C+1; Counter2++)
                                  {   //this will swap the rows
                                       Temp[Counter2]=A[i][Counter2];
                                       A[i][Counter2]=A[Temp2][Counter2];
                                       A[Temp2][Counter2]=Temp[Counter2];
                                   }  
                         }
                      }
                  }
             }
        }
}//swap function will end here

  //echelon funcion starts here
  //it will convert matrix in upper trangular form
float Echelon(int R1,int C1,float A1[][C1+1])
{    
      int i,j,Pass1,Pass2,Pass3,K3;  //declaration variables
      //K3 for storing divisor  //all pass1,2,3 for sorting assistance
      Pass2=0;  // initialization of variables

      for (j=0; j<C1; j++)  //loop for rows
      {
          Pass2++;  //increment
          K3=1;     //initalize divisor at 1

          for (i=Pass2; i<R1+1; i++)  //loop for coloumn
          {
              if (j==i-1)      //
              {
                         float K1=A1[i-1][j];  //stores  value for division
                         for (Pass1=0; Pass1<C1+1; Pass1++) //check for rows
                         {
                             if (i==R1 && A1[R1-1][C1-1]==0 && A1[R1-1][C1]!=0)
                             { 
                                                   break;
                             }
                             if (i==R1 && A1[R1-1][C1-1]==0 && A1[R1-1][C1]==0)
                             {
                                                   break;
                             }
                             A1[j][Pass1]=A1[j][Pass1]/K1;
                         }
              }
              if (i==R1)
              {
                    break;
              }
              float K2=A1[i][j];

              for (Pass3=0; Pass3<C1+1; Pass3++)
              {
                  A1[i][Pass3]=A1[i][Pass3]-(K2*A1[i-K3][Pass3]);
              }
              K3++;
          }
       }
}  //echelon function ends

// infinite function starts
//function willoperate for infinte solutions
float Infinite(int R2,int C2,float A2[][C2+1],float X1[])
{
      int i,j,Pass6,Pass7;
      Pass6=0;
      for (i=0; i<(R2-1); i++)
      {
                 Pass6++;
                 if (Pass6>1)
                 {
                             for (j=1; j<Pass6; j++)
                             {
                                 int Pass7;
                                 Pass7=A2[R2-(i+2)][C2-1];
                                 X1[i]=X1[i]-Pass7-X1[i-1]*A2[R2-(i+2)][C2-(j+1)];
                                 if(j==1)
                                 {
                                         Pass7=0;
                                 }
                             }
                 }
                  else
                  {
                      X1[i]=-A2[R2-2][C2-1];
                   }
        }
}//infinite  function ends here

    //function for unique solution
    //back substitution of unique answers held here
float Unique(int R3,int C3, float A3[][C3+1],float X2[])
{
     int i,j,Pass4,Pass5;  //declaration variables
     Pass4=0;

     for (i=0; i<R3; i++)   //loop for rows
     {
         Pass4++;
         if (Pass4>1)
         {
                     for (j=1; j<Pass4; j++)  //loop for coloumn
                     {
                         X2[i]=X2[i]+A3[R3-(i+1)][C3] -X2[j-1]*A3[R3-(i+1)][C3-j];
                         if (j==1)
                         {
                                  A3[R3-(i+1)][C3]=0;
                          }
                     }
          }
         else
         {
             X2[i]=A3[R3-1][C3];
         }
     }
} //unique function ends here

  //function for displaying answrs starts
  //will show the respective solution of the matrices
void solution(int R,int C,float A[][C+1],float X[])
{
     int i,j;      //declare i for row check and j for colomn check
     printf("\n\t******HERE IS THE ANSWER******\t\n");
     if (A[R-1][C-1]==0 && A[R-1][C]!=0)  //condition for no solution
     {
                        printf("\n\n\t The given system of equation has no solution\n");
     }
     if (A[R-1][C-1]==0 && A[R-1][C]==0)  //condition for infinite solution
      {
                         Infinite(C,R,A,X); //calls infinite function
                         printf("\n\tThe given system of equation has Infinite Solutions\n\t");
                         for (i=0; i<(R-1); i++)  //loop for rows
                         {
                               printf("\tX[%d] = %.2ft\n",i+1,X[R-(i+2)]);
                          }  //displays the result of infinite answer
                               printf("\tX[%d] = t\n",R);
                               printf("\tWhere   -Infinity < t < +Infinity\n");
       }
       if (A[R-1][C-1]!=0)// condition for unique solution
       {
                          Unique(R,C,A,X);//calls for unique function
                          printf("\tEach unknown variable has unique solution\n");
                          for (i=0; i<R; i++) //loop for rows
                          {
                              printf("\t\tX[%d] = %.2f \n",1+i,X[R-i-1]);
                           } //display result for unique solutions
       }      
}   // solution function ends here

  //this will display the good bye screen
  //and will show the group members
void endresult() 
{
     printf("\n\n\t**************THANK YOU FOR USING THIS PROGRAM*****************");
     }//function end result will end here
//the program will end

Thursday, October 13, 2011

C Code For Rat In a Maze Game

//In this code the rat moves by itself

#include <math.h>
#include <stdio.h>
#include <time.h>
int x,y;
int main ()
{
int i,j,z,v,s,b,c,q=0,m,n,o,u;
char a[x][y];
for (i=0;i<x;i++)
{for (j=0;j<y;j++)
{a[i][j]='X';}}
a[x-1][0]='C';
a[x-1][y-1]='R';
z=x/2;
v=x;
for (i=0;i<=z;i++)
{
a[v-1][1]='T';
v--; }
s=y/2;
b=2;
for (i=0;i<(y-3);i++)
{
a[x/2][b]='T';
b++;
}
c=x/2;
for(i=0;i<=z;i++)
{
a[c][y-2]='T';
c++;}
for (i=0;i<(x-1);i++)
{for (j=0;j<y;j++)
    {q=rand()%2;
    if (a[i][j]!='T')
    {{if (q==0)
     {a[i][j]='o';}
    if (q==1)
     {a[i][j]='X';}}       
         }}}
for (i=0;i<x;i++)
{for(j=0;j<y;j++)
{if (a[i][j]=='T')
    a[i][j]='o';}}
for(i=0;i<x;i++)
{for (j=0;j<y;j++)
{printf("%c ",a[i][j]);} printf("\n");  }
u=a[x-1][y-1];
n=x-1;
o=y-1;
for(i=0;i>-1;i++)
{  
sleep (1);
system("clear");

//left
if ((a[n][o-1]!='X') && o>0 && a[n][o-1]!='1')
{ a[n][o-1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
    {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
    o=o-1;if (a[n][o]==a[x-1][0])
    {printf("\n\t\t\t\tYOU WON\n"); break;}
  // left (force right)
  if (o==0 && a[n+1][o]=='X' && a[n-1][o]!='X')
   {a[n][o+1]='R'; a[n][o]='1';
    sleep(1);
    system("clear");
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
    o=o+1;}
continue;
}

//down
if((a[n+1][o]!='X') && n<(x-1) && a[n+1][o]!='1')
{  a[n+1][o]='R';
   a[n][o]='1';
   for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
   printf("%c ",a[i][j]); printf("\n");}
n=n+1;if (a[n][o]==a[x-1][0])
{printf("\n\t\t\t\tYOU WON\n"); break;}
   // down (force right)
if (a[n+1][o]=='X') {a[n][o+1]='R'; a[n][o]='1';
    sleep(1);
    system("clear");
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;} continue; }

//up
{if ((a[n-1][o]!='X') && n>0)
{ a[n-1][o]='R';
  a[n][o]='1';
  for(i=0;i<x;i++)
  {for (j=0;j<y;j++)
  printf("%c ",a[i][j]); printf("\n");}
n=n-1;
   if (a[n][o]==a[x-1][0])
      {printf("\n\t\t\t\tYOU WON\n"); break;}
  // up (force down)
  if (a[n-1][o]=='X' && a[n][o-1]=='X' && a[n][o+1]=='X')
      {a[n+1][o]='R';
      a[n][o]='1';
      for(i=0;i<x;i++)
      {for (j=0;j<y;j++)
      printf("%c ",a[i][j]); printf("\n");}
      n=n+1; if (a[n][o+1]!='X') {a[n][o+1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;} }

continue;}}

//right
if((a[n][o+1]!='X') && (o<(y-1)) )
{a[n][o+1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;
if (a[n][o]==a[x-1][0])
{printf("\n\t\t\t\tYOU WON\n"); break;} continue;
}

// force down

if (a[n+1][o]=='1')

      {a[n+1][o]=='R'; a[n][o]=='S';}

  }

}// end loop
}//end main       

Wednesday, October 12, 2011

Engineering Electromagnetics Download

Follow this link to donwload.

Modern Control Systems Download

Follow this link to download.

Data Structures and Algorithm Analysis in C 2nd Edition Download

Follow this link to download the manual.

Signal And System Manual and Lectures Download

Follow this link to download the manual

Follow this link to get the lecture.

Network Analysis

Follow this link to download.

Data Structures and Algorithm Analysis in C 2nd Edition

Follow this link to download the manual.

Manual for Digital Logic Design by Morris Mano 2nd Edition Download

Follow this link to download the manual.

Manual Electronic Devices and Circuits by Boyelstead 11th Edition Download

Follow this link to download the manual.

Manual For Digital System BY TOCCI 10 Edition Download

Follow this link to download the manual part1.

Follow this link to download the manual part2.

Follow this link to download the manual part3.

Manual for University Physics Download

Follow this link to download manual for  chapter 19-24.

Follow this link to download manual for  chapter 25_30.

Monday, October 10, 2011

Manual of Engineering Circuit Analysis 6th Edition by Willian Hayt Download

Follow this link to download the manual

Fundamentals of Electric Circuit 3rd Edition by Alexander, Sadiku

 

Follow this link to download this ebook.

Manual for Electric Machinery Fundamentals 4 Edition by Stephen J. Chapman Download

Follow this link to download the manual for the Chapman 4 edition

Instructor Manual For Advanced Engineering Mathematics 9 edition By Erwin Kreyszig Download

Follow this link to download the manual.

Fundamentals of Physics By Resick , Halliday 7th Edition Download

Follow this link to download the book.

AutoCad Lecture Slides

Based on some requests i have uploaded the lectures of AutoCad .

Their are some slides for ENGINEERING DRAWING by Dhananjay A Jolhe

Follow this link to download the slides.

Instructor Manual for Calculus By Thomas and Finney 11 Edition

Here you go

Follow this link to download

the instructor manual for Thomas and Finney 11 Edition

Instructor Manual for C how to program by dietel and dietel

Follow this link to download the instructor manual for the C how to program by dietel and dietel

C How to program 5th edition by Dietel and Dietel

Follow this link to download the 5th edition of the book C how to program by Dietel and Dietel

C PROGRAMMING LECTURE SLIDES

Encouraged by the feedback, and the interest of you guys

Lecture slides of C Programming by Dietel and Dietel are uploaded

I have put some lectures for the sake of better understanding of  C

so just get them through this link :).

Sunday, February 6, 2011

How to use sscanf

#include<stdio.h>
int main()
{
char s[]="123 45.32 4";
int x;
double y;
int c;
puts(s);
sscanf(s,"%d%lf%d",&x,&y,&c);
printf("%d\n%lf\n%d\n",x,y,c);
}

How to use sprintf

#include<stdio.h>
int main ()
{ int x;
  char s[80];
  double y;
  printf("ENTER\n");
  scanf("%d%lf",&x,&y);
  sprintf(s,"integer:%6d\ndouble:%.2f\n",x,y);
  puts(s);
}

How to get string input way2

#include<stdio.h>
int main ()
{ int i=0;
  char c;   
  char a[80];
  printf("ENTER\n");
  while ((c=getchar())!='\n') {a[i++]=c;}
  a[i]='\0';
  puts(a); 
}

How to get a string input way1

#include<stdio.h>
int main()
{
char s[50];
printf("enter\n");
fgets(s,50,stdin);
printf("%s",s);
}

How to use strtol

#include<stdio.h>
#include<stdlib.h>
int main () {
const char *string="21.3% are good" ;
long d;
char *stringptr;
d=strtol(string,&stringptr,0);
printf("%s\n%ld\n%s\n",string,d,stringptr);
}

How to use strod

#include<stdio.h>
#include<stdlib.h>
int main () {
const char *string="21.3% are good" ;
double d;
char *stringptr;
d=strtod(string,&stringptr);
printf("%s\n%.2f\n%s\n",string,d,stringptr);
}

How to use atol

#include<stdio.h>
#include<stdlib.h>
int main() {
long i;
char s[20]={"23.56 this is"};
i=atol(s);
printf("%ld\n",i);
}

How to use toupper and tolower

#include<stdio.h>
#include<ctype.h>
int main() { char c,a;
printf("ENTER\n");
scanf("%c",&c);
a=toupper(c);
printf("%c\n",a);
a=tolower(c);
printf("%c\n",a);
}

How to use ctype.h functions

The following codes shows how to use isdigit(), isalpha(), isalnum(), isxdigit(), isspace(), iscntrl(), ispunct(), isprint(), isgraph(),
include<stdio.h>
#include<ctype.h>
int main()
{int i;
char f;
printf("enter character\n");
scanf("%c",&f);
i=isdigit(f);
if(i==0)printf("not digit\n");else printf("is digit\n");
i=isalpha(f);
if(i==0)printf("not alpha\n");else printf("is alpha\n");
i=isalnum(f);
if(i==0)printf("not digit or aplha\n");else printf("is digit or alpha\n");
i=isxdigit(f);
if(i==0)printf("not xdigit\n");else printf("is xdigit\n");
i=isspace(f);
if(i==0)printf("not space\n");else printf("is space\n");
i=iscntrl(f);
if(i==0)printf("not cntrl\n");else printf("is cntrl\n");
i=ispunct(f);
if(i==0)printf("not punct\n");else printf("is punct\n");
i=isprint(f);
if(i==0)printf("not print\n");else printf("is print\n");
i=isgraph(f);
if(i==0)printf("not graph\n");else printf("is graph\n");
}

How to use strlen

#include<stdio.h>
#include<string.h>
int main ()
{
  const char *s="abcdefghijklmnopqrstuvwxyz";
   int y;
   y=strlen(s);
   printf("%d\n",y);
}

How to use memset

#include<stdio.h>
#include<string.h>
int main()
{ char s[]="ggggggggggg";
  memset(s,'G',5);
puts(s);
}

How to use memmove

#include<stdio.h>
#include<string.h>
int main ()
{
char x[]="home sweet home";
memmove(x,&x[5],10);
puts(x);
}

How to use memcpy

#include<stdio.h>
#include<string.h>
int main()
{char s[17];
char x[]="Thats how it is done";
memcpy(s,x,17);
puts(s);
}

How to use strrchr

#include<stdio.h>
#include<string.h>
int main()
{
const char *s="the camera is zooming";
int c='a';
printf("%s\n",strrchr(s,c));
}

How to use strstr

#include<stdio.h>
#include<string.h>
int main()
{
const char *s="scorecard";
const char *y="e";
printf("%s\n",strstr(s,y));
}

How to use strpbrk

#include<stdio.h>
#include<string.h>
int main()
{
const char *s="best of luck";
const char *x="o";
printf("%c\n",*strpbrk(s,x));
}

How to use strchr

#include<stdio.h>
#include<string.h>
int main()
{
const char *s="Thats how it is done";
char c='u';
if (strchr(s,c)!=NULL)
{printf ("found\n");}
else
{printf("not found\n");}
}

How to use strcmp

#include<stdio.h>
#include<string.h>
int main()
{
const char *x="memm";
const char *y="mmea";
int c;
c=strcmp(x,y);
if(c==0) {printf("0\n");}
if(c<0)  {printf("-1\n");}
if (c>0) {printf("1\n");}
}

How to use strcpy & strncpy

#include<stdio.h>
#include<string.h>
int main()
{
char x[]="HOW TO USE THIS";
  char y[25];
  char z[15];
strcpy(y,x);
puts(y);
strncpy(z,x,6);
puts(z);
}

C Code for Finding maximum entry in data

#include<stdio.h>
void array(int a[4][3]);
int locater(int b[4][3],int c);
int main()
{
    int marks,i;
    int record[4][3]={{12,25,67},{32,30,65},{15,34,70},{20,40,79}};
    printf("\n\n\n");
    array(record);
    for(i=0;i<3;i++)
        {
            marks=locater(record,i);
            if(i==0)
            {
            printf("\nThe Maximum lowest marks in all sections are %d\n",marks);
            }
            if(i==1)
            {
            printf("The Maximum Average in all sections are %d\n",marks);
            }
            if(i==2)
            {
            printf("The Maximum Highest marks in all sections are %d\n\n\n\n",marks);
            }
        }
    return 0;
}
void array(int a[4][3])
{
            int i,j;
            printf("Sections    Lowest Average Highest\n");
            printf("        marks  marks    marks\n"   );
                for(i=0;i<4;i++)
                    {   
                    printf("Section %d\t",i+1);
                    for(j=0;j<3;j++)
                        {
                            printf("%d\t",a[i][j]);
                        }   
                    printf("\n");
                    }
}
int locater(int a[4][3],int c)
{
            int x;
            for(x=0;x<4;x++)
            {
                if(a[x][c]>=a[1][c] && a[x][c]>=a[2][c] && a[x][c]>=a[3][c])
                               {   
                return a[x][c];       
                            }   
            }
}

C Code for Searching and Sorting Data

#include<stdio.h>
#include<stdlib.h>
void sorter(int b[],int size);
void searcher(int b[],int size);
int main()
{
    int a[40],i;
    printf("\n\n\nTHE DATA SET CONTAINS 40 VALUES WHICH ARE AS FOLLOW:");
    printf("\n");
    for(i=0;i<40;i++)
    {a[i]=rand()%40;
    printf("%d ",a[i]);
    }
    sorter(a,40);
    searcher(a,40);
    return 0;
}
void sorter(int b[],int size)
{
    int i,j,k,temp;
    for(i=0;i<size;i++)
        {
        for(j=0;j<size-1;j++)
        {
            if(b[j]>b[j+1])
            {temp=b[j];
                 b[j]=b[j+1];
                 b[j+1]=temp;
            }
         }
        }
    printf("\n\n\nTHE DATA IS BEING SORTED AS FOLLOW:\n");
    for(k=0;k<40;k++)
    {printf("%d ",b[k]);}
    printf("\n\n");
    }
void searcher(int b[],int size)
{
    int key,found,low,mid,high;
    printf("\nWhat value you want to locate=");
    scanf("%d",&key);
    low=0;
    high=size-1;
    while(low<=high)
    {   
    mid=(low+high)/2;
    if(b[mid]==key)
    {
    found=1;
    break;
    }
    else if(b[mid]>key)
    {
    high=mid-1;
    }
    else
    {
    low=mid+1;
    }
    }
    if(found==1)
    printf("%d is in the data set\n",key);
    else printf("%d is not in the data set\n",key);
}

C Code for int type function

#include<stdio.h>
int function(int);
int main ()
{
int x=10,y;
y=function(x);
printf("%d\n",y);
return 0;
}
int function(int a)
{return a*a;}

C Code for void type fuction

#include<stdio.h>
void function(int);
int main()
{
int x=10;
function(x);
return 0;
}
void function(int a)
{printf("%d\n",a);}

C Code for array of characters/string

#include<stdio.h>
int main ()
{
char a[]="Hello";
int x;
for (x=0;x<6;x++)
    {printf("%c",a[x]);} //printing character wise
printf("\n");
//printing string directly
printf("%s\n",a);
return 0;
}

C Code for initializing 2d array

#include<stdio.h>
int main ()
{
int a[10][10]={0};
int x,y;
for (x=0;x<10;x++)
    for (y=0;y<10;y++)
        {printf("%d\n",a[x][y]);}
return 0;
}

C Code for initializing and printing array

#include<stdio.h>
int main()
{
int a[10]={0};
int x;
for (x=0;x<10;x++)
  {
    printf("%d\n",a[x]);
  }
return 0;
}

Sunday, January 30, 2011

C CODE FOR A BAKERY

The following represents a simple bakery program that keeps the update of sales made.Run it first then you will have a better understanding of whats going on  in the code.

#include<stdio.h>//standard input
int main()//entry to main fuction
{
int bread=15; // initial bread available
int breadsold,remainingbread=15,breadprofit=0; // variables for bread
int egg=72; // initial egg available
int eggsold,remainingegg=72,eggprofit=0; // variables for egg
int butter=25;//initial butter available
int buttersold,remainingbutter=25,butterprofit=0;//variables for bread
int milk=50; // initial milk
int milksold,remainingmilk=50,milkprofit=0;//variables for milk
int item,x,profit1=0,profit2=0,profit3=0,profit4=0,megaprofit=0;//variables for profit and items selected
printf("\n\n*******************************************************************************\n");
printf("\t\t\t      WELCOME TO BAKERY\n ");
printf("\t     CURRENTLY WE HAVE THE FOLLOWING ITEMS IN QUANTITY\n");
printf("\t        BREAD         EGG        BUTTER        MILK\n");
printf("\t         %d            %d         %dkg          %dl\n\n\n",bread,egg,butter,milk);
printf("Current Market Rates for Respected Items are as Follow:\nBread=40Rs\nEgg=4Rs\nButter=50Rs/kg\nMilk=50Rs/ltr\n\n\n\n");
printf("What would you like to sell\n");  // Here user selects what to sell
printf("1 for Bread\n");
printf("2 for Egg\n");
printf("3 for Butter\n");
printf("4 for Milk\n");
printf("Your choice:");
scanf("%d",&item);
if(item>4 || item<=0)
   {printf("Not a valid entry"); }
else{
for(x=0;x!=6;x)  // Only loop so that user can sell more
{
   if (item==1) // 'Bread if'
      {
         printf("\nhow much bread u want to sell\n");
         scanf("%d",&breadsold);
            if (breadsold>remainingbread || breadsold<0)
            {printf("We dont have this much Bread to sell kindly keep it less or equal to quanitiy available\n");}
            else
            {remainingbread=bread-breadsold; //tracker of bread available for next entry in the loop
            bread=remainingbread;
            profit1= breadsold*40;//calculates profit of this sale
            breadprofit=profit1 + breadprofit;//for total megaprofit
            printf("\nThe quantity of Bread u have sold is %d\nThe profit for this sale is %dRs\nNow you have %d bread remaining to sell\n\n",breadsold,profit1,remainingbread);
             megaprofit=breadprofit+eggprofit+butterprofit+milkprofit ;}
      }// 'End Bread if'
  if (item==2) //"Egg if"
     {
       printf("how much Egg u want to sell\n");
       scanf("%d",&eggsold);
        if(eggsold>remainingegg || eggsold<0)
           {printf("We dont have this much Egg to sell kindly keep it less or equal to quantity available\n");}
         else{
         remainingegg=egg-eggsold;//traker for egg available
         egg=remainingegg;
         profit2=eggsold*4;//profit of this sale
         eggprofit=profit2+eggprofit;//total eggprofit for megaprofit
         printf("\nThe quantity of Egg u have sold is %d\nThe profit for this sale is %dRs\nNow you have %d Egg remaining to sell\n\n",eggsold,profit2,remainingegg);
         megaprofit=breadprofit+eggprofit+butterprofit+milkprofit ;}
    } // End " Egg if"
  if (item==3)//"Butter if"
  {
       printf("how much Butter u want to sell\n");
       scanf("%d",&buttersold);
          if(buttersold>remainingbutter || buttersold<0 )
           {printf("We dont have this much Butter to sell kindly keep it less or equal to quantity available\n");}
          else {
           remainingbutter=butter-buttersold;//tracker of butter available
           butter=remainingbutter;
           profit3=buttersold*50;//profit for this sale
           butterprofit=profit3+butterprofit;//total profit ofbutter for megaprofit
           printf("\nThe quantity of Butter u have sold is %dkg\nThe profit for this sale is %dRs\nNow you have %dkg Butter remaining to sell\n\n",buttersold,profit3,remainingbutter);
           megaprofit=breadprofit+eggprofit+butterprofit+milkprofit ;}
  } //End "butter if"
  if (item==4) // " Milk if"
  {
  printf("how much Milk u want to sell\n");
  scanf("%d",&milksold);
     if(milksold>remainingmilk || milksold<0)
     {printf("We dont have this much Milk to sell kindly keep it less or equal to quantity available\n");}
     else {
      remainingmilk=milk-milksold;//tracker of milk availabe
      milk=remainingmilk;
      profit4=milksold*50;//this sale profit
      milkprofit=profit4+milkprofit;//for megaprofit
      printf("\nThe quantity of Milk u have sold is %dltr\nThe profit for this sale is %dRs\nNow you have %dltr Milk remaining to sell\n\n",milksold,profit4,remainingmilk);
      megaprofit=breadprofit+eggprofit+butterprofit+milkprofit ;}
  } //End "Milk if"
if (item==5)
{printf("Profit earned uptill now is %dRs",megaprofit);}
printf("\n\n\nWhat would you like to sell\n"); // portion for sale after another or end
printf(" 1 for Bread\n");
printf(" 2 for Egg\n");
printf(" 3 for Butter\n");
printf(" 4 for Milk\n");
printf("OR\n");
printf(" 5 to view Profit earned uptill now\n");
printf(" 6 if you are willing to Exit sale\n");
printf("Your choice:");
scanf("%d",&item); // end portion
if( item<=0 || item>6)
{printf("\nNOTE : There is no such choice\nPlease select again\n");}
if(item==6) // if 6 is entered the loop ends
{ x=6;
megaprofit=breadprofit+eggprofit+butterprofit+milkprofit ; //calculates the profits of all sales made
printf("\n\n\nThe Profit you earned for today's sale is %dRs\n You have\n %d BREAD\n %d EGG\n %dkg Butter \n %dltr MIlk left to sale\n\n\n \t\t\tHAVE A GOOD DAY\n\n\n",megaprofit,remainingbread,remainingegg,remainingbutter,remainingmilk); }
//displays the total profit
}//end For loop
}
printf("\n\n*******************************************************************************\n");
return 0;} //end main funtion

Saturday, January 29, 2011

C CODE FOR SQUARE/RECTANGLE

#include<stdio.h>
int main()
{
    int x,y,h,w;
    printf("ENTER THE HEIGHT AND WIDTH OF SQUARE/RECTANGLE\n\n");
    scanf("%d%d",&h,&w);
    for(y=1;y<=h;y++)
    {
        for(x=1;x<=w;x++)
        {
            printf("^");
        }
        printf("\n");
    }
return 0;
}

C CODE FOR DIAMOND

#include<stdio.h>
int main()
{
    int counter,x,y,z;
    int count,a,b,c,input;
    printf("PLEASE ENTER the width");   
    scanf("%d",&input);
    counter=0;
    for(x=input;x>=1;x--)
    {
        for(y=1;y<=x;y++)
        {
            printf(" ");
        }
                counter=counter+1;
            for(z=1;z<=counter;z++)
                {printf("* ");}
                printf("\n");}
    count=input;
    for(a=1;a<=input;a++)
    {
        printf(" ");
        for(b=1;b<=a;b++)
        {
            printf(" ");
        }
        count=count-1;
        for(c=1;c<=count;c++)
        {
            printf("* ");
        }
        printf("\n");
    }
return 0;
}

C CODE FOR PYRAMID


#include<stdio.h>
int main()
{
    int counter,x,y,z;
        counter=0;
    for(x=15;x>=1;x--)
    {
        for(y=1;y<=x;y++)
        {
            printf(" ");
        }
                counter=counter+1;
            for(z=1;z<=counter;z++)
                {printf("* ");}
                printf(" \n");}
return 0;
}

C CODE FOR HOLLOW TRIANGLE

#include<stdio.h>
int main()
{
    int x,y,z;
    printf("*\n");   
    for(x=1;x<10;x++){
                     printf("*");
                           for(y=1;y<=x;y++){
                            printf(" ");
                                           }
          printf("*\n");
                         }
        for(z=1;z<=10;z++)
        {printf("*");}
    printf("**");
    printf("\n");

return 0;
}

C CODE FOR TRIANGLE

#include<stdio.h>
int main()
{
int x,y,h;
printf("ENTER HEIGHT OF TRIANGLE:\n");
scanf("%d",&h);
for(x=1;x<=h;x++){
for(y=1;y<=x;y++){
printf("^");
}
printf("\n");
}
return 0;
}

C CODE FOR FACTORIAL OF A NUMBER

#include<stdio.h>
int main () {
int i,num,factorial=1;
printf("ENTER THE NUMBER");
scanf("%d",&num);
if(num<0)
printf("Factorial not possible");
else
{
  for (i=1;i<=num;i++)
  {factorial=factorial*i;}
  printf("\nFACTORIAL=%d\n",factorial);      
}
return 0;
}

C CODE FOR ARMSTRONG NUMBER UPTO LIMIT

#include<stdio.h>
int main () {
int num,n,sum,r,limit;
printf("ENTER THE LIMIT\n");
scanf("%d",&limit);
printf("Armstrong numbers upto given limit are:\n");
for(num=1;num<limit;num++)
{
    n = num;
    sum = 0 ;
    while (n!=0)
        {
            r=n%10;
            sum=sum+(r*r*r);
            n=n/10;
        }
    if(sum==num)
        {printf("%7d\n",num);}
}
return 0;
}

C CODE FOR ARMSTRONG NUMBER

#include<stdio.h>
int main () {
int num,n,sum,r;
printf("ENTER THE NUMBER\n");
scanf("%d",&num);
n=num;
sum=0;
while(n!=0)
  {
    r=n%10;
    sum=sum+(r*r*r);
    n=n/10;
  }
if(sum==num)
   {printf("\n%d is armstrong number\n",num);}
else
   {printf("\n%d is not armstrong number\n",num);}
return 0;
}  

C CODE FOR FIBONACCI SERIES

#include<stdio.h>
int main () {
int a,b,n,next,count;
printf("how many terms are required 2<=n<=24\n");
scanf("%d",&n);
a=0;
b=1;
printf("\n\nFIBNONACCI TERMS ARE:\n");
printf("%8d%8d",a,b);
count=2;
while (count<n)
    {
        next=a+b;
        printf("%8d",next);
        a=b;
        b=next;
        count++;
    }
printf("\n");
return 0;
}

Thursday, January 27, 2011

C CODE FOR SUM OF EVEN AND ODD NUMBERS UPTO LIMIT

#include<stdio.h>
int main ()
{
int n,i,sumeven=0,sumodd=0;
printf("ENTER THE LIMIT\n");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
    if ((i%2)==0)
     {sumeven=sumeven+i;}
    else
      {sumodd=sumodd+i;}
}
printf("\n\nSUM OF EVEN NUMBERS UPTO %d is %d\n",n,sumeven);
printf("\n\nSUM OF ODD NUMBERS UPTO %d is %d\n",n,sumodd);
return 0;
}

C CODE FOR NTH TERM OF FIBONACCI SERIES

#include<stdio.h>
int main()
{
int x=0,y=1,z,nth,i;
printf("\n\nPlease Enter The Term Number:");
scanf("%d",&nth);
for(i=1;i<=nth;i++)
{z=x+y;
x=y;
y=z;
}
printf("\nthe %dth term of Fibonacci Series is is %d\n\n\n",nth,z);
return 0;
}

C CODE FOR COMPLETE SQUARE TRIANGLE

The following code will print upto 20 the triangle whos hypotenuse is a complete square. You can extend the limits by doing some changes in the code to your desire.

#include <stdio.h>  
#include <math.h>    // for sqrt root function
int main ()         
{
int b,p;             // initialization
int h;           // initialization
printf("All the Applicable Triangles are:\n");
printf("Base \t Perp \t   Hyp \n"); // to represent sides of triangle which are satisfied by pythagoras theorem
  for(b=1;b<=19;b++)   // first loop starts with one side from 1-19
     { for (p=1; p<=19;p++) // second loop starts with b=loop state untill p=19
             { for (h=sqrt((p*p)+(b*b));h==sqrt((p*p)+(b*b));h++) // 3rd loop takes the first input only and becomes false for nxt loop so no further execution occurs
               if (h>=20)   // if hyp is more than 20 sides wont be printed
               break;
               else  
               {printf(" %d \t  %d \t   %d \n",b,p,h); // sides printed if h^2=b^2+p^2 ans is integer
             } // end of third loop
      } // end of second loop
    } //end of first loop
return 0;
} //end main

C CODE FOR PRIME FACTORS OF A NUMBER

#include<stdio.h>

int main()
{ int x,q,no,s;
  printf("Enter the Number: "); // the number to be operated
  scanf("%d",&no);
  printf("\nThe Prime Factors of %d Follow:\n" ,no);
  for ( x=2;(no/x)>=1;x++) // entry to only loop which lets only prime    factors to be printed
      { if ((no%x)==0)        // prints the factor if mod is 0
         {printf("%d  ",x);
          s=(no/x);           
          no=s;          // changes the number for entry into loop again
          x=x-1;        // it makes the x same when entered again in the loop
         }
      }// loop end 
  printf("\n");
  return 0;
}

C CODE FOR PRIME NUMBER way2

//This one is a little complicated

#include<stdio.h>
int main()
{ int x,q,no,s,originalno;
  printf("Enter the Number: ");
  scanf("%d",&no);
  originalno=no;
  for ( x=2;(no/x)>=1;x++)
      { if ((no%x)==0)      
         {
      if (x==originalno)
          {printf("%d is a PRIME number\n",originalno);}
      s=(no/x);           
          no=s;         
          x=x-1;       
         }
      }
if(x!=originalno)
  {printf("%d is not a prime number\n",originalno);}
  return 0;
}

C CODE FOR PRIME NUMBER way1

#include <stdio.h>
int main()
{
int x,y,z,a=0;
printf("enter the number: ");
scanf("%d",&x);
for (y=2;y<x;y++)
      { if (x%y==0)
            {a=y;}   
    }// loop end
if(a==0)
{printf("Number is Prime\n");}
else
{printf("Number is not Prime\n");}
return 0;
}

C CODE FOR FACTORS OF A NUMBER

#include <stdio.h>
int main()
{
int x,y,z;
printf("enter the number: ");
scanf("%d",&x);
printf("The factors are as follow:\n");
for (y=1;y<=x;y++)
      { if (x%y==0)
        printf("%d\n",y);}// loop end
return 0;
}

C CODE FOR PRINTING MULTIPLICATION TABLE OF No

#include<stdio.h>

int main() {

int i,num;

printf("ENTER THE NUMBER");

scanf("%d",&num);

for (i=0;i<=10;i++)

{printf("%d * %2d = %d\n",num,i,num*i);}

return 0;

}

C CODE FOR SUM OF N NUMBERS USING while,Do while & for Loop

I guess it is enough with the simple examples of if .So the examples below illustrate the use of while , do while and for loop all are doing the same thing. I have made the same code just to show the differences among these loops.I myself prefer for loop as it is summed up in just a single line.Anyways this is the code.

// WHILE LOOP

#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("ENTER THE VALUE OF N:");
scanf("%d",&n);
printf("First %d numbers are\n",n);
    while (i<=n)
    {
      printf("%7d ",i); //%7 to print the digit within 7 slots
       sum = sum +i;
       i++;
    }
printf("\nSum = %d\n",sum);
return 0;
}

// DO WHILE LOOP

 
#include<stdio.h>
int main ()
{
int n,i=1,sum=0;
printf("ENTER THE VALUE OF N:");
scanf("%d",&n);
printf("First %d numbers are\n",n);
do
{
  printf("%7d,",i);
  sum=sum+i;
  i++;
  }
while(i<=n);
printf("\n\nSum=%d\n",sum);
return 0;
}

//FOR LOOP

 
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("ENTER THE VALUE OF N:");
scanf("%d",&n);
printf("First %d numbers are\n",n);
for(i=1;i<=n;i++)
    {
        printf("%7d",i);
        sum=sum+i;
    }
printf("\n\nSum = %d",sum);
return 0;
}

C CODE FOR SOLVING QUADRATIC EQUATION

#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,r1,r2,img1,img2,disc;
printf("\nENTER THE Co-officients\n");
scanf("%f%f%f",&a,&b,&c);
if (a==0)
    {
      if (b==0)
          printf("Equation is Degenerate\n");
       else
     {
        printf("Linear equation has single root");
        r1=-c/b;
            printf("\nRoot=%.2f",r1);
      }
      }
    else
      {
         disc=(b*b)-(4*a*c);
           if(disc>0)
        {
            printf("Real and disctinct roots\n");
            r1=(-b+sqrt(disc))/(2*a);
            r2=(-b-sqrt(disc))/(2*a);
            img1=img2=0;
        }
          else
        {
              if(disc==0)
            {
               printf("Real and equal roots\n");
               r1=r2=(-b)/(2*a);
               img1=img2=0;
            }
            else
            {
              printf("Imaginary roots\n");
              r1=r2=-b/(2*a);
              img1=sqrt(-disc)/(2*a);
              img2=img1;
            }
              }
        printf("\nFirst root is \n");
        printf("\nReal part=%.2f  Imaginary part=%.2f\n",r1,img1);
        printf("\nSecond root");
        printf("\nReal part=%.2f  Imaginary part=%.2f\n",r2,img2);
    }
return 0;

C CODE FOR TRIANGLE AREA AND ITS TYPE

In the following code i have included the math library as well because the use of square root function was required . There are plenty others functions located in the math library .Dont forget to write –lm while compiling as it is required for math library if ur working on linux environment eg gcc test.c –lm . Anyways here is the code as the topic says

 

#include<stdio.h>
#include<math.h>
int main ()
{
float a,b,c,s,area;
printf("\nENTER THE THREE SIDES OF TRIANGLE\n");
scanf("%f%f%f",&a,&b,&c);
if( ((a+b)>c) && ((a+c)>b) && ((b+c)>a))
  {
     if ((a==b) && (a==c))
    {printf("\nEQUILATERAL TRIANGLE\n");}
     else
       {
     if((a==b) || (a==c) || (b==c) )
       printf("\nISOCELES TRIANGLE\n");
     else
       printf("\nSCALENE TRIANLGE\n");
    }
    s=(a+b+c)/2;
    area=sqrt(s*(s-a)*(s-b)*(s-c));
    printf("AREA IS %.2f sq units\n",area);
    }
else
{printf("TRIANGLE NOT POSSIBLE");}
retur

C CODE FOR SWITCH CONTROL

#include<stdio.h>
int main ()
{
int day;
printf("ENTER THE WEEKDAY\n");
scanf("%d",&day);
switch(day)
{
  case 1: printf("weekday is Sunday\n");
          break;
  case 2: printf("weekday si Monday\n");
      break;
  case 3: printf("weekday is Tuesday\n");
      break;
  case 4: printf("weekday is Wednesday\n");
      break;
  case 5: printf("weekday is Thursday\n");
      break;
  case 6: printf("weekday is Friday\n");
         break;
  case 7: printf("weekday is Saturday\n");
       break;
  default:printf("Wrong choice entered\n");
      break;
}
return 0;
}

C CODE FOR 5 NUMBERS AVERAGE TELLER

#include<stdio.h>
int main ()
{
float a,b,c,d,e,sum,avg;
printf("ENTER THE FIVE NUNBERS\n");
scanf("%f%f%f%f%f",&a,&b,&c,&d,&e);
sum=a+b+c+d+e;
avg=sum/5;
printf("SUM=%.2f\n",sum); // .2 is used to restrict upto 2 decimal digit
printf("AVERAGE=%.2f\n,",avg);
return 0;
}

C CODE FOR CONVERTING NUMBER TO YEARS WEEKS & DAYS

#include<stdio.h>
int main () {
int number,years,weeks,days;
printf("ENTER THE NUMBER\n");
scanf("%d",&number);
years=number/365;
weeks=(number-(years*365))/7;
days=(number-(years*365)-(weeks*7));
printf("\nYEARS=%d\nWEEKS=%d\nDays=%d\n",years,weeks,days);
return 0;
}

C CODE FOR LEAP YEAR

#include<stdio.h>
int main() {
int year;
printf("ENTER THE YEAR\n");
scanf("%d",&year);
if ((year%4==0)&& (year%100!=0))
{printf("%d is a leap year\n",year);}
else
printf("%d is not a leap year\n",year);
return 0;
}

C CODE FOR GRADE CALCULATOR

#include<stdio.h>
int main () {
int sub1,sub2,sub3,sub4,sub5,total;
float average;
printf("ENTER THE NUMBERS IN 5 SUBJECTS OF THE STUDENT\n");
scanf("%d%d%d%d%d",&sub1,&sub2,&sub3,&sub4,&sub5);
   total=sub1+sub2+sub3+sub4+sub5;
   average=total/5;
   if(average>=80)
     {printf("STUDENT HAS GOT A GRADE\n");}
  else if(average>=70)
     {printf("STUDENT HAS GOT B GRADE\n");}
  else if(average >=60)
     {printf("STUDENT HAS GOT C GRADE\n");}
  else if(average>=50)
     {printf("STUDENT HAS GOT D GRADE\n");}
  else if(average>=40)
     {printf("STUDENT HAS GOT E GRADE\n");}
  else
     {printf("STUDENT HAS FOT F GRADE\n");}  
return 0;
}

C CODE FOR CONDITIONAL OPERATER

#include<stdio.h>
int main()
{
int age;
printf("ENTER YOUR AGE IN YEARS\n");
scanf("%d",&age);
(age>=18) ?printf("\nYOU SHOULD VOTE\n"): printf("\nYOU CANNOT VOTE\n");
return 0;
}

C CODE FOR GETTING ASCII CODE way2

#include<stdio.h>
int main()
{
int z;
printf("ENTER THE CHARACTER\n");
z=getchar();
printf("THE ASCII VALUE OF CHARACTER is %d\n",z);
return 0;

}

C CODE GOR ASCII CODE way1

The following code will get the ASCII code of the character entered by the user

#include<stdio.h>
int main()
{
int x;
char character;
printf("ENTER THE CHARACTER\n");
scanf("%c",&character);
x=character;
printf("THE ASCII VALUE OF %c is %d\n",character,x);
return 0;

}

Wednesday, January 26, 2011

C CODE TO REVERSE 5 DIGIT NUMBER

/*this program will reverse the five digit number*/
#include<stdio.h>
int main()
{
int no,b,c,d,e,rev; /*no is the number entered by user rev is the reverse number which will be displayed at the end*/
printf("enter a five digit no.\n");
scanf("%d",&no);
if(no<=99999 && no>=10000)        /*this if statement is to make sure that user enters a five digit number
                                    otherwise the program will display "you have not entered a five digit number"*/
{
  b=no/10;      /*int  is used so number after decimal will be neglected*/
  c=b/10;
  d=c/10;
  e=d/10;
  rev=(no-10*b)*10000;      /*this formula will calculate unit digit of number entered by user and multiply by 10000
                              in this way it will become first digit of reverse number*/  
  rev=rev+((b-10*c)*1000); /*this formula will calculate the tens digit of number entered by user and multiply by 1000
                             the resultant will be added to previous calculated no. i.e rev so first two digits are
                             reversed.in the similar way all digits will be reversed*/
  rev=rev+((c-10*d)*100);
  rev=rev+((d-10*e)*10);
  rev=rev+e;
  printf("%d\n",rev);
}
else          /*this else is with the first if statement*/
printf("you have not entered a five digit no.\n");
return 0;
}

C PROGRAM FOR BASIC FORMULAS

/*this program can solve formulas:
1)force=mass*acceleration        2)coversion of litres in gallons      3)conversion of km in meters
this program will display a menu and its your choice which formula you want to use*/
#include<stdio.h>
int main()
{
float choice;
float mass;
float acc;
float force;
float lt;
float gall;
float meter;
float km;
printf("which formula do you want to use\n\n1)force=mass*acceleration\n2)conversion of litres of petrol in gallons\n3)conversion of distance in km into meters\n");
printf("\nenter a choice 1,2 or 3\n");
scanf("%f",&choice); /*this will take a number from user that which formula he want to solve*/
if(choice==1 || choice==2 || choice==3)     /*this if statement makes sure that choice entered is 1,2
                                              or 3*/                    
{
  if(choice==1)     /*this statement wil execute if the user enters 1 coice which is to find force*/
  {
   printf("enter mass\n");
   scanf("%f",&mass);
   printf("enter acceleration\n");
   scanf("%f",&acc);
   force=mass*acc;
   printf("force is %f\n",force);
  }
  if(choice==2)     /*this statement wil execute if the user enters 2 coice*/
  {
   printf("enter volume of petrol in litres\n");
   scanf("%f",&lt);
   gall=lt*0.264172052358148;
   printf("volume of petrol in gallons is %f\n",gall);
  }
  if(choice==3)    /*this statement wil execute if the user enters 3 coice*/
  {
   printf("enter distance in km\n");
   scanf("%f",&km);
   meter=km*1000;
   printf("distance in meters is %f\n",meter);
  }
}
else            /*this else is with the first if statement*/
printf("you have entered a wrong choice\n");
return 0;
}
Related Posts with Thumbnails