Wednesday 2 March 2016

Convert Meter to Inch and Inch to Meter

Q. Write a C program to convert Meter to Inch and Inch to Meter.

Ans

keep in mind:

1 Meter = 39.37 Inch
1 Inch  = 0.025 Meter


#include<stdio.h>
int main()
{
 int ch;
 double meter,inch;
 printf("\nEnter 1 for convert Meter to Inch.");
 printf("\nEnter 2 for convert Inch to Meter.");
 printf("\nEnter 0 for exit.");
 printf("\n\nEnter your choice : ");
 scanf("%d", &ch);
 switch(ch)
 {
  case 1:
    printf("\nEnter value in Meter: ");
    scanf("%lf", &meter);
    inch = (39.37) * meter;
    printf("\n\t-- Convert Meter to Inch --\n");
    printf("\n%lf meter = %lf Inch",meter,inch);
    break;
  case 2:
    printf("\nEnter value in Inch: ");
    scanf("%lf", &inch);
    meter = (.025) * inch;
    printf("\n\t-- Convert Inch to meter --\n");
    printf("\n%lf Inch = %lf Meter",inch,meter);
    break;
  case 0:
    goto exit;
  default:
    printf("\nYou enter invalid options.");
 }
 exit:
 return 0;

}

Output:


Flowchart for finding Armstrong number

Q. Write down the program for finding Armstrong number and also draw flowchart.

Ans.

Definition of Armstrong number: An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
In other word “A number is Armstrong if it is equal the sum of cube of its digits.”
Example of Armstrong number is 371 because according to definition cube of its digits  sum will be equal to number so
Armstrong number 371=(3)3+(7)3+(1)3
                 371=27+343+1

                 371=371


#include<stdio.h>
#include<conio.h>
int main()
{
 int n,num,rem,sum=0;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(n=num; n>=1; n=n/10)
 {
   rem=n%10;
   sum=sum+(rem*rem*rem);
 }
 if(num==sum)
    printf("Number is Armstrong number");
 else
    printf("Number is not Armstrong number");
 getch();
 return 0;
}


Output:

Enter any number :254
Entered number is not Armstrong number
                   
Enter any number : 371
Entered number is Armstrong number


Reverse all words but not string

Q. Write a C program to reverse all words but not string.

Let's assume string is: This Is A Good Blog
We wants to do: sihT sI A dooG golB


Ans:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 char str[100];
 int i,temp;
 printf("Enter any string : ");
 gets(str);
 for(i=0; str[i]!=NULL; i++)
 {
  if(str[i+1]==' ' || str[i+1]==NULL)
  {
   for(temp=i; temp>=0 && str[temp]!=' '; temp--)
     printf("%c", str[temp]);
  }
  printf(" ");
 }
 getch();
 return 0;

}

Output



Tuesday 1 March 2016

call by reference swap program

Q. what is calling by reference? How it is different from call by value? Write a C function to swap two given numbers using call by reference mechanism.

Ans:

When an argument is passed by reference, the caller actually allows the called function to modify the original variable's value.

  • Sends the address of a variable to the called function.
  • Use the address operator(&) in the parameter of the called function.
  • Anytime we refer to the parameter, therefore we actually referring to the original variable.
  • If the data is manipulated and changed in the called function, the original data in the function are changed.

#include<stdio.h>
#include<conio.h>
void swaping(int *x, int *y);
int main()
{
 int n1,n2;
 printf("Enter first number (n1) : ");
 scanf("%d",&n1);
 printf("Enter second number (n2) : "); 
 scanf("%d",&n2);
 printf("\nBefore swapping values:"); 
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 swaping(&n1,&n2);
 printf("\nAfter swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 getch();
 return 0;
}
void swaping(int *x, int *y)
{
  int z;
  z=*x;
  *x=*y;
  *y=z;
}

Output:


Quadratic eq. roots

Q. Write a program to find the roots of a quadratic equation.

Process of finding root of quadratic equation in C program:
The roots of any quadratic equation of the the form,
 

ax2+bx+c=0
has minimum two roots.
The formula of calculating these roots are as:



Where:r1,r2 are roots. First of all user entered values of a,b and c. Then it finds out numerator- which is the discriminate. Then checks if the numerator is greater, equals or less than zero. Prints out the results according to the situation.


#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
  int a,b,c;
  float r1,r2,up;
  printf("Enter value of a : ");
  scanf("%d", &a);
  printf("Enter value of b : ");
  scanf("%d", &b);
  printf("Enter value of c : ");
  scanf("%d", &c);
  up=(b*b)-(4*a*c);
  if(up>0)
 {
    printf("\n ROOTS ARE REAL ROOTS\n");
    r1 = ((-b) + sqrt(up)) /(2*a);
    r2 = ((-b) - sqrt(up)) /(2*a);
    printf("\n ROOTS : %f, %f\n", r1, r2);
  }
  else if(up==0)
  {
    printf("\n ROOTS ARE EQUAL\n");
    r1 = (-b/(2*a));
    printf("\n ROOT IS...: %f\n", r1);
  }
  else
     printf("\n ROOTS ARE IMAGINARY ROOTS\n");
  getch();
 return 0;

}

Output:

Enter value of a : 6
Enter value of b : -13
Enter value of c : 6
ROOTS ARE REAL ROOTS
ROOTS : 1.500000, 0.666667



Enter value of a : 1

Enter value of b : 1
Enter value of c : -2
ROOTS ARE REAL ROOTS

ROOTS : 1.000000, -2.000000

User Define Function- LeapYear

Q. Write a C program to find entered year is leap year or not using own created function say "leap".

Ans.

#include<stdio.h>
int leap(int );
int main()
{

 int year;
 printf("Enter any year : ");
 scanf("%d", &year);
 if(leap(year))
   printf("\n%d is leap year",year);
 else
   printf("\n%d is not leap year",year);
 return 0;
}

int leap(int y)
{
 if((y%400==0 && y%100==0)||(y%4==0))
    return 1;
 else
    return 0;

}

Output:

Enter any year:2016
2016 is a leap year

User Define Function- Reverse Number

Q. Write a C program to create a function that would reverse the any number.

For example:
Assume enter number = 57429
Result = 92475


Ans:

#include<stdio.h>
int rev(int );
int main()
{
 int num,res;  // res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 res = rev(num);
 printf("Reverse order number = %d",res);
 return 0;
}

int rev(int n)
{
 int r=0;
 for(; n>=1; n=n/10)
   r = r*10 + n%10;
 return r;

}

Output


Difference of matrix

Q. Write a C program to accept value of matrix and print out the difference or subtract  of matrix.
OR
Q. Give a example of 2d array with suitable arithmetic operation.

Ans. 

#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void diff_mat(int [MAX][MAX],int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],y[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values in first matrix :\n");
 input_mat(x,row,col);
 printf("Enter values in second matrix :\n");
 input_mat(y,row,col);
 diff_mat(x,y,z,row,col);
 printf("\nDisplay difference of two matrices :");
 show_mat(z,row,col);
 getch();
 return 0;
}


void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void diff_mat(int matA[MAX][MAX],int matB[MAX][MAX],int matC[MAX][MAX],int r,int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       matC[i][j]=matA[i][j]-matB[i][j];
  }
}


void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}

Output:

Enter no. of rows and columns : 3 3
Enter values in First matrix :
4 5 3
7 6 1
6 9 5
Enter values in second matrix :
1 3 2
7 6 4
5 1 0
Display difference of two matrices :
3 2 1
0 0 -3
1 8 5

Sum of matrix

Q. Write a C program to accept value of matrix and print out the sum of matrix.
OR
Q. Give a example of 2d array with suitable arithmetic operation. 

Ans.

#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void sum_mat(int [MAX][MAX],int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],y[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values in first matrix :\n");
 input_mat(x,row,col);
 printf("Enter values in second matrix :\n");
 input_mat(y,row,col);
 sum_mat(x,y,z,row,col);
 printf("\nDisplay sum of two matrices :");
 show_mat(z,row,col);
 getch();
 return 0;
}


void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void sum_mat(int matA[MAX][MAX],int matB[MAX][MAX],int matC[MAX][MAX],int r,int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       matC[i][j]=matA[i][j]+matB[i][j];
  }
}


void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}

Output:

Enter no. of rows and columns : 3 3
Enter values in First matrix :
4 5 3
2 1 9
6 7 5
Enter values in second matrix :
1 3 2
7 6 4
2 1 0
Display sum of two matrices :
5 8 5
9 7 13
8 8 5


Transpose of matrix



Q. Write a C program to accept value of matrix and find the transpose matrix.

Ans.

#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void trns_mat(int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values of %d X %d matrix :\n",row,col);
 input_mat(x,row,col);
 printf("\nYour entered matrix is : \n");
 show_mat(x,row,col);
 trns_mat(x,z,row,col);
 printf("\nTranspose of entered matrix is :\n");
 show_mat(z,row,col);
 return 0;
}

void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void trns_mat(int matA[MAX][MAX],int matT[MAX][MAX],int r,int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       matT[j][i] = matA[i][j];
  }
}


void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}

Output:


Product of matrix

Q. Write a C program to accept value of matrix and find out the product of matrix.
OR
Q. Give a example of 2d array with suitable arithmetic operation.

Ans. 


#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void prod_mat(int [MAX][MAX],int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],y[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values in first matrix :\n");
 input_mat(x,row,col);
 printf("Enter values in second matrix :\n");
 input_mat(y,row,col);
 prod_mat(x,y,z,row,col);
 printf("\nDisplay product of two matrices :");
 show_mat(z,row,col);
 getch();
 return 0;
}

void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void prod_mat(int matA[MAX][MAX],int matB[MAX][MAX],int matC[MAX][MAX],int r,int c)
{
 int i,j,k;
 for(i=0; i<r; i++)
 {
  for(j=0;j<c; j++)
  {
    matC[i][j]=0; 
    for(k=0;k<c; k++)
    {
     matC[i][j]=matC[i][j]+matA[i][j]*matB[i][j];
    }
  }
 }

void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}

Output:-

Enter no. of rows and columns : 3 3
Enter values in First matrix :
4 5 3
2 1 9
6 7 5
Enter values in second matrix :
1 3 2
7 6 4
2 1 0
Display product of two matrices :
12 45 18
42 18 108
36 21 0

Diagonal sum of matrix

Q. Write a C program to find the sum of diagonal elements in matrix.

Hint: If you have following typical square 3X3 matrix, then diagonal elements will be:

1  2  3
4  5  6
7  8  9

In above 3X3 matrix there are two types diagonal element as:
first diagonal elements are  1, 5, 9 and

second diagonal elements are 3, 5,7

Ans.

#include<stdio.h>
#include<conio.h>
#define MAX 5
int main()
{
  int mat[MAX][MAX],row,col;
  int i,j,d1=0,d2=0;
  printf("Enter no. of rows and columns : ");
  scanf("%d%d",&row,&col);
  printf("Enter the elements of matrix:\n");
  if(row==col)
  {
    for(i=0; i<row; i++)
    {
      for(j=0; j<col; j++)
         scanf("%d",&mat[i][j]);
    }
    for(i=0,j=col-1; i<row || j>=0; i++,j--)
    {
       d1=d1+mat[i][i];
       d2=d2+mat[i][j];
    }
    printf("\nThe sum of first diagonal elements : %d",d1);
    printf("\nThe sum of second diagonal elements : %d",d2);
  }
  else
  {
     printf("Rows and columns are not equal!");
     printf("\nTry again!");
  }
  getch();
  return 0;

}

Output:

Enter no. of rows and columns : 3  2
Enter the elements of matrix:
Rows and columns are not equal!

Try again!

Enter no. of rows and columns : 3 3
Enter the elements of matrix:
1  2  3
4  5  6
7  8  9
The sum of first diagonal elements : 15

The sum of second diagonal elements : 15 

Palindrome using pointer

Q. write a program to accept a string and find out whether it is palindrome or not  using pointer.

Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 char str[30];
 char *p,*t;
 printf("Enter any string : ");
 gets(str);
 for(p=str ; *p!=NULL ; p++);
  for(t=str, p-- ; p>=t; )
  {
    if(*p==*t)
    {
        p--;
        t++;
    }
    else
        break;
  }
  if(t>p)
       printf("\nString is palindrome");
  else
       printf("\nString is Not palindrome");
  getch();
  return 0;

}

Output:

Enter any string : SHAREMARKET

String is not palindrome

Compilation and execution of programs

                                    Compilation and execution 

  • There are many steps involved in converting a C program into an executable form, these all steps are called "build process".
  • Build process are representing in the following graphically diagram : 



If you don't understand the build process, don't worry,read following explanations of build process i.e. how a source program make executable program. It is all internal process of machine,that's done in neno seconds.

Lets now understand the steps mentioned in above figure in detail.
  1. Editor-Type your program in editor(source code).
  2. Preprocessing-During this step, the C source code is expanded based on the preprocessor directives like as #include#ifdef#define etc. The expanded source code is stored in an intermediate file with .i extension.
  3. Compilation- The expanded source code is then passed to the compiler, which identifies the syntax error in the expanded source code. If the expanded source code is error free, then the compiler transfer the expanded source code in C, into an equivalent assembly language program. The assembly code is typically stored in .ASM file. So our first.c file would be changed and stored in first.ASM.
  4. Assembling - Assembler translate the .ASM program into Relocatable Object code. Thus assembler translate our first.asm file into first.OBJ. .OBJ file is one of the binary file. This object file contained header and several section.
  5. Linking - Linking is the final step of build process i.e. creating an executable program. It's do following works :
    • Find definition of all external function
    • Find definition of all global variables
    • Combine Data Section
    • Combine Code Section
  6. Loading - Once the .EXE file is created and stored on the disk,it is ready for execution. when we execute it, it is first brought from the disk into the memory (RAM) by an OS component called Program Loader.

Ads Inside Post