Sep 28, 2019

C++ Programming Examples

c programming examples, c proograms
c++ programming examples
      Recommended link :Basic C++ Programs
      1.       Write a C++ program that takes two numbers and display the product of two numbers
#include<stdio.h>
#include<conio.h>
void main ()
{
int a, b, sum;
printf (“enter two numbers”);
scanf (“%d”, &a);
scanf (“%d”, &b);
sum= a*b;
printf (“product of given number is =%d”, sum);
getch ();
}

      2.       Write a C++ program that takes three numbers to calculate and print the average of the numbers
#include<stdio.h>
#include<conio.h>
void main ()
{
int a, b, c, sum;
float avg;
printf (“enter three numbers”);
scanf (“%d”, &a);
scanf (“%d”, &b);
scanf (“%d”, &c);
sum= a+ b+ c;
avg= sum/3.0;
printf (“Average of given number is =%f”, avg);
getch ();
}

     3.       Write a C++ program to compute the area of a hexagon.
              Area of a hexagon = (6 * s^2)/(4*tan(π/6))
              where s is the length of a side
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int s, b;
float c, area;
printf (“enter the length of hexagon”);
scanf (“%d”, &s);
b= 6* pow (s, 2);
c= 4* tan (3.14/6);
area = b/c;
printf (“Area of hexagon is =%f”, area);
getch ();
}

     4.       Write a C++ program to compute the area of a polygon.
       Area of a polygon = (n*s^2)/(4*tan(π/n))
       where n is n-sided polygon and s is the length of a side
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int s, b, n;
float c, area;
printf (“ Enter the length of polygon”);
scanf (“%d”, &s);
printf (“ Enter the side of polygon”);
b= n* pow(s, 2);
c= 4* tan (3.14/n);
area = b/c;
printf (“Area of polygon is =%f”, area);
getch ();
}

      5.       Write a C++ program to compute the distance between two points on the surface of earth.
Distance between the two points [ (x1,y1) & (x2,y2)]
               d = radius * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2))
               Radius of the earth r = 6371.01 Kilometers
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int x1, x2, y1, y2;
float radius = 6371010, a, b, c, d;
printf (“Enter the given points sequentially  \nx1 = ”);
scanf (“%d”, & x1);
printf (“\n x2=”);
scanf (“%d”, & x2);
printf (“\n y1=”);
scanf (“%d”, & y1);
printf (“\n y2=”);
scanf (“%d”, & y2);
a = sin(x1) + cos(x1) * cos (x2) * cos(y1 – y2);
b = acos (a);
c = b * 180/3.14;
d = radius * c;
printf (“ distance of given points is  =%f”, d);
getch ();
}

      6.       Write a C++ program that accepts an integer (n) and computes the value of n+ nn+ nnn
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int  n, a;
printf (“ Enter number ”);
scanf (“%d”, &n);
b= n* pow(s, 2);
a= n + pow(n,2)+ pow(n,3);
printf (“desired result is =%d”, a);
getch ();


    

0 comments:

Post a Comment

Please do not enter any spam link in the comment box