Monday, 27 August 2012

Programming "C"


Write down a program to compute the area of a rectangle. It first prompts the user
for the length and width of the rectangle and then displays the area.

#include <stdio.h>
#include <conio.h>

void main()
{
int l,w;
clrscr();
printf("Please Enter Length:");
scanf("%d",&l);
printf("Please Enter Width:");
scanf("%d",&w);
printf("The area is:%d",l*w);
getch();
}


Write a program that computes the volume of a cube. Have the program prompt
the user for each dimension.

#include <stdio.h>
#include <conio.h>

void main()
{
int l,w,h;
clrscr();
printf("Length:");
scanf("%d",&l);
printf("Width:");
scanf("%d",&w);
printf("Hight:");
scanf("%d",&h);
printf("The area is:%d",l*w*h);
getch();
}


Write a program that computes the number of seconds in a year
#include <stdio.h>
#include <conio.h>
void main()
{
 printf("The number of seconds in a Year:%f",365.0*24.0*3600.0);
 getch();
}


Ramesh’s basic salary is input through the keyboard. His house rent allowance is
40% and medical allowance is 5% of the basic salary. Write a program to
calculate the gross (total) salary.

#include <stdio.h>
#include <conio.h>

void main()
{
int n,s;
clrscr();

printf("Enter the Basic Salary:");
scanf("%d",&n);

s=n+n*.4+n*.05;
printf("The gross Salary:%d",s);

getch();
}


The distance between two cities (in Km) is input through the keyboard. Write a
program to convert and print this distance in meters, feet, inches and centimeters.

#include <stdio.h>
#include <conio.h>

void main()
{
float km,m,ft,in,cm;
clrscr();

printf("Enter the distance between two cities(in km.):");
scanf("%f",&km);

m=km*1000;
ft=m*3.28;
in=ft*12;
cm=in*2.54;

printf("The distance in meters:%f m\n",m);
printf("The distance in feet:%f ft\n",ft);
printf("The distance in inches:%f in\n",in);
printf("The distance in centimeters:%f ",cm);

getch();
}


Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a
program to convert this temperature into centigrade degrees.

#include <stdio.h>
#include <conio.h>

void main()
{
float f;
clrscr();

printf("Give the temperature in Farenheit:");
scanf("%f",&f);

printf("The temperature in Centigrade:%f",f*5.0/9.0-32.0*5.0/9.0);

getch();
}


Two numbers are input through the keyboard into two locations (variables) c and
d. Write a program to interchange the contents of c and d.

#include <stdio.h>
#include <conio.h>

void main()
{
int c,d,a,b;
clrscr();

printf("Enter the value of c:");
scanf("%d",&c);

printf("Enter the value of d:");
scanf("%d",&d);
a=c;
b=d;
c=b;
d=a;
printf("The value of c:%d\n",c);
printf("The value of d:%d",d);

getch();
}


Write a program to determine whether a number is positive or negative.
#include <stdio.h>
#include <conio.h>

void main()
{
int n;
clrscr();

printf("Enter the Number:");
scanf("%d",&n);

if(n<0)
printf("The Number is: Negative");

else
printf("The Number is: Positive");

getch();
}


Write a program to determine whether a integer number is even or odd.
#include <stdio.h>
#include <conio.h>

void main()
{
int n;
clrscr();
printf("Please Enter the Number:");
scanf("%d",&n);

if(n%2==0) printf("The Number is: Even");
else printf("The Number is: Odd");
getch();
}


Write a program that will take two numbers as inputs and then give the result of the
division of those two numbers.

#include <stdio.h>
#include <conio.h>

void main()
{
float x,y;
clrscr();
printf("Enter the First Number:");
scanf("%f",&x);
printf("Enter the Second Number:");
scanf("%f",&y);

if(y==0) printf("The Result is: Infinitive");
else printf("The Result is:%f",x/y);
getch();
}



Write a program that requests two numbers and then displays either their sum or
product or division or difference.
#include <stdio.h>
#include <conio.h>

void main()
{
int n,x,y;
float z;
clrscr();

printf("Enter the First Number:");
scanf("%d",&x);

printf("Enter the Second Number:");
scanf("%d",&y);

printf("Please enter what you want:\n");
printf("For: 1.Sum, 2.Difference, 3.Product, 4.Division\n");
printf("Number:");
scanf("%d",&n);

if(n==1)                           /* For "Sum" */
printf("The Sum is:%d",x+y);

else if(n==2)                      /* For "Difference" */
  {
   if(x>y) printf("The difference is:%d",x-y);
   else printf("The Difference is:%d",y-x);
  }

else if(n==3)                      /* For "Product" */
printf("The product is:%d",x*y);

else if(n==4)                      /* For "Division" */
  {
   if(y==0) printf("The Result is Undefine");
   else
     {z=x/y;
      printf("The Division is:%f",z);}
  }

else printf("Error!! Please give the right Number");

getch();
}


Write a program that requests two numbers and then displays either their sum or
product or division or difference.

#include <stdio.h>
#include <conio.h>

void main()
{
int n,x,y;
clrscr();

printf("Enter the First Number:");
scanf("%d",&x);

printf("Enter the Second Number:");
scanf("%d",&y);

printf("Please enter what you want:\n");
printf("For: 1.Sum, 2.Difference, 3.Product, 4.Division\n");
printf("Enter Number:");
scanf("%d",&n);

if(n==1)
printf("The Sum is:%d",x+y);

else if(n==2)
  {
   if(x>y) printf("The Difference is:%d",x-y);
   else printf("The Difference is:%d",y-x);
  }

else if(n==3) printf("The product is:%d",x*y);

else if(n==4)
  {
   if(y==0) printf("The Result is Undefine");
   else printf("The Division is:%f",x/y);
  }

else printf("Error!! Please Didn't gave right Number");

getch();
}


The marks obtained by a student in 5 different subjects are ionput through the
keyboard. The students gets a division as per the following rules:
a. Percentage above or equal to 60--first division
b. Percentage between 50 and 59 -- second division
c. Percentage between 40 and 49—third division
d. Percentage less than 40—fail
e.
Write a program to calculate the division obtained by the student.

#include <stdio.h>
#include <conio.h>

void main()
{
int n;

clrscr();

printf("Enter the students Number:");
scanf("%d",&n);

if(n<=100&&n>=60) printf("The student gets First Division");

else if(n<60&&n>=50) printf("The student gets Second Division");

else if(n<50&&n>=40) printf("The student gets Third Division");

else if(n<40&&n>=0) printf("The student Faided");

else printf("Please enter correct Number");

getch();
}


If cost price and selling price of an item is input through the keyboard, write a
program to determine whether the seller has made profit or incurred loss. Also
determine the amount of profit or loss.

#include <stdio.h>
#include <conio.h>

void main()
{
float cost,sell,loss,profit;

clrscr();

printf("Enter the cost Price:");
scanf("%f",&cost);

printf("Enter the sell Price:");
scanf("%f",&sell);

if(cost>sell)                                                                              /* loss */
 {loss=cost-sell;
  printf("Seller has made Loss\n");
  printf("The loss is:%f\n",loss);
  printf("The Percentage of loss is:%f",loss/cost*100);
 }

else if(cost<sell)                                                                  /* Profit */
 {profit=sell-cost;
  printf("Seller has made Profit\n");
  printf("The Profit is:%f\n",profit);
  printf("The Percentage of Profit:%f",profit/cost*100);
 }

else                                                                                     /* Equal */
printf("No Profit & No Loss");

getch();
}


Write a program to determine whether a year is a leap year or not.

#include <stdio.h>
#include <conio.h>

void main()
{
int n,flag=0;
clrscr();

printf("Please Enter the Year:");
scanf("%d",&n);

if(n%4==0 && n%100==0 && n%400==0) flag=1;
if(n%4==0 && n%100!=0) flag=1;

if(flag==1) printf("Year %d is A Leap Year",n);
else printf("Year %d is Not A Leap Year",n);
getch();
}


Any character is entered through the keyboard. Write a program to determine
whether the character entered is a capital letter, a small case letter, a digit or a
special symbol.

#include <stdio.h>
#include <conio.h>

void main()
{
char l;

clrscr();

printf("Please Enter a Letter:");
scanf("%c",&l);

if(l>=48&&l<=57) printf("The letter is: Digit");

else if(l>=65&&l<=90) printf("The letter is: Capital Letter");

else if(l>=97&&l<=122) printf("The letter is: Small Letter");

else printf("The letter is: Special Symbol");

getch();
}


Write a program that will print 1 to 10.

#include <stdio.h>
#include <conio.h>

void main()
{
int n;
clrscr();

for(n=1;n<11;n++)
 printf("%d\t",n);

getch();
}


Write a program that will print all the numbers between 15 to 100 that are
divisible by 15.

#include <stdio.h>
#include <conio.h>

void main()
{
int i;
clrscr();

for(i=16;i<100;i++)
 if(i%15==0)
  printf("%d\t",i);

getch();
}


Write a program that will calculate 1+2+3+…..+n=?

#include <stdio.h>
#include <conio.h>

void main()
{
int n,i,s;
clrscr();

printf("Enter the value of n:");
scanf("%d",&n);

s=0;
for(i=1;i<=n;i++)
 {s=s+i;}
printf("The Result:%d",s);

getch();
}


Write down a program that will calculate the factorial of any number.


#include <stdio.h>
#include <conio.h>

void main()
{
int n,i,sum=1;
clrscr();

printf("Please Enter The Number:");
scanf("%d",&n);

for(i=1;i<=n;i++) sum=sum*i;

printf("The Result is:%d",sum);

getch();
}


Write down a program that will check whether a number is prime or not.

#include <stdio.h>
#include <conio.h>

void main()
{
int n,i,flag=0;
clrscr();

printf("Please Enter the Number:");
scanf("%d",&n);

for(i=2;i<n;i++)
 if(n%i==0) flag=1;

if(flag==1) printf("Not a Prime Number");
else printf("%d is a Prime Number",n);

getch();
}


Write down a program that will check whether a number is a perfect number or
not

#include <stdio.h>
#include <conio.h>

void main()
{
int n,i,s=0;
clrscr();

printf("Please Enter a Number:");
scanf("%d",&n);

for(i=1;i<=n/2;i++)
 if(n%i==0) s=s+i;

if(s==n) printf("%d is a Perfect Number",n);
else printf("Not a Perfect Number");

getch();
}


Write down a program that will show the square of all the numbers between 10 to
100.

#include <stdio.h>
#include <conio.h>

void main()
{
int i,s;
clrscr();

for(i=11;i<100;i++)
 {
  s=i*i;
  printf("%d\n",s);
 }

getch();
}


Write a program that finds all the prime numbers between 2 and 1000.

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
int i,j;
clrscr();

for(i=3;i<1000;i++)
 {j=sqrt(i);
  if(i%2==0||i%3==0||i%5==0||j*j==i){}
  else printf("%d\n",i);}

getch();
}


Write a program to calculate 1/1!+2/2!+3/3!+…..+n/n!=?

#include <stdio.h>
#include <conio.h>

void main()
{
int n,i;
float f=1,s=0,x;
clrscr();

printf("Enter the value of n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
 {
  f=f*i;
  x=i/f;
  s=s+x;
 }
printf("The sum is:%f",s);
getch();
}


Write down a program that will take 10 float numbers in an array and then
calculate the sum of those numbers.

#include <stdio.h>
#include <conio.h>

void main()
{
float a[10],i,sum=0;
clrscr();

printf("Please Enter Numbers:");
for(i=0;i<10;i++)
{
 scanf("%f",&a[i]);
 sum=sum+a[i];
}
printf("The sum is:%f",sum);

getch();
}


Write down a program that will take 5 integer input and then find the maximum,
minimum and the average of those 5 inputs.

#include <stdio.h>
#include <conio.h>

void main()
{
int a[5],i,max,min,sum=0;
float r;
clrscr();

printf ("Please Enter the Numbers:");
for(i=0;i<5;i++)
{
 scanf ("%d",&a[i]);
 sum=sum+a[i];
}

max=a[0];
min=a[0];
for(i=0;i<5;i++)
{
 if(a[i]>max) max=a[i];
 if(a[i]<min) min=a[i];
}

printf("The maximum Number is:%d\n",max);
printf("The minimum Number is:%d\n",min);
printf("The average Number is:%f",sum/5);

getch();
}


Write down a program that will search a value in an array (size of n). If it finds
the array then print the location of that value.

#include <stdio.h>
#include <conio.h>
#define MAX 100

void main()
{
int a[MAX],i,n,flag=0,loc;
clrscr();

printf("Please Enter how many array location do you want?\n");
scanf("%d",&n);

printf("Please Enter Numbers:\n");
for(i=0;i<n;i++)
 scanf("%d",&a[i]);

printf("Please Enter another number for searching location:\n");
scanf("%d",&n);

for(i=0;i<n;i++)
{
 if(n==a[i]) {loc=i; flag=1;}
}

if(flag==1)
{ printf("The Number is in the Array\n");
  printf("Location: Array[%d]",loc); }

else printf("The Number is Not in the Array");
getch();
}


Write a program that will find the summation of even and odd positions.

#include <stdio.h>
#include <conio.h>
#define MAX 100

void main()
{
int a[MAX],i,n,even=0,odd=0;
clrscr();

printf("Please Enter how many array location do you want?\n");
scanf("%d",&n);
printf("Please Enter the Number:\n");
for(i=0;i<n;i++)
 scanf("%d",&a[i]);

for(i=0;i<n;i++)
 {if(i%2==0) even=even+a[i];
  else odd=odd+a[i];}

printf("The Summation of Even Numbers:%d\n",even);
printf("The Summation of Odd Numbers:%d",odd);
getch();
}


Write a program that will sum the square of the values of an array.

#include <stdio.h>
#include <conio.h>
#define MAX 100

void main()
{
int a[MAX],i,n,sum=0;
clrscr();

printf("Please Enter how many array location do you want?\n");
scanf("%d",&n);
printf("Please Enter Numbers:\n");
for(i=0;i<n;i++)
{
 scanf("%d",&a[i]);
 sum=sum+a[i]*a[i];
}
printf("The sum of the square of the values is: %d",sum);
getch();
}


Mat A+Mat B=?

#include <stdio.h>
#include <conio.h>
#define MAX 100

void main()
{
 int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],m1,n1,m2,n2,i,j;
 clrscr();

 printf("Please Enter the Dimension of First Matrix (row*column):\n");
 scanf("%d",&m1);
 scanf("%d",&n1);
 printf("Please Enter First Matrix:\n");
 for(i=0;i<m1;i++)
  for(j=0;j<n1;j++)
   scanf("%d",&a[i][j]);

 printf("Please Enter the Dimension of Second Matrix (row*column):\n");
 scanf("%d",&m2);
 scanf("%d",&n2);
 printf("Please Enter Second Matrix:\n");
 for(i=0;i<m2;i++)
  for(j=0;j<n2;j++)
   scanf("%d",&b[i][j]);

 if(m1==m2||n1==n2)
 {
 printf("Result Matrix:\n");
 for(i=0;i<m1;i++)
  {
   for(j=0;j<n1;j++)
    {c[i][j]=a[i][j]+b[i][j];
     printf(" %d ",c[i][j]);}
   printf("\n");}
  }
 else printf("Dimention Error");

 getche();
}


1+1+2+3+5+8+13+21+...............+n=?

#include <stdio.h>
#include <conio.h>

int fab(int x)
{
 if(x==1) return 1;
 else if(x==2) return 1;
 else return fab(x-1)+fab(x-2);
}
void main()
{
 int n,i,s=0;
 clrscr();
 printf("Please Enter the position:\n");
 scanf("%d",&n);

 for(i=1;i<=n;i++)
  s=s+fab(i);

 printf("The Summetion is:%d",s);
 getch();
}


Input the value of a, b, c, x; then find out ax²+bx+c = ?

#include <stdio.h>
#include <conio.h>

void main()
{
    int a,b,c,x;
    clrscr();

    printf("Enter the value of a:");
    scanf("%d",&a);

    printf("Enter the value of b:");
    scanf("%d",&b);

    printf("Enter the value of c:");
    scanf("%d",&c);

    printf("Enter the value of x:");
    scanf("%d",&x);

    printf("The result is: %d",a*x*x+b*x+c);

   getch();
}


Input the value of a, b, c, x, y; then find out  (ax²+bx+c) / (bx+cy) = ?

#include <stdio.h>
#include <conio.h>

void main()

{
    float a, b, c, x, y, m, n;
    clrscr();

    printf("Enter the value of a:");
    scanf("%f",&a);

    printf("Enter the value of b:");
    scanf("%f",&b);

    printf("Enter the value of c:");
    scanf("%f",&c);

    printf("Enter the value of x:");
    scanf("%f",&x);

    printf("Enter the value of y:");
    scanf("%f",&y);

    m = a*x*x + b*x + c;
    n = b*x + c*y;

    if(n==0) printf("Result is Undefine");
    else printf("The answer is:%f", m/n );

   getch();

}


If  ax²+bx+c=0 ; then find out the value of x

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()

{
    float a,b,c,m,n;
    clrscr();

    printf("Enter the value of a:");
    scanf("%f",&a);

    printf("Enter the value of b:");
    scanf("%f",&b);

    printf("Enter the value of c:");
    scanf("%f",&c);

    m=b*b-4*a*c;
 
    if(m<0)
    {
      n=sqrt(-m);
       printf("The value of x1: %f +%f i", -b/2/a, n/2/a);
      printf("\nThe value of x2: %f %f i ", -b/2/a, -n/2/a);
     }

    else if(m==0) {printf("The value of x: %f", -b/2/a);}

    else
   {
       printf("The value of x1: %f",(-b+sqrt(m))/2/a);
       printf("\nThe value of x1: %f",(-b-sqrt(m))/2/a);
   }

    getch();

}