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


Ads Inside Post