-->

Facebook

Jasper Roberts - Blog

Thursday, March 12, 2015

Write a C++ Program to demonstrate character data type.

#include<iostream.h>
//using namespace std;
class Person
{
char name[30];
int age;
public:
void get_data(void);
void display();
};
void Person::get_data(void)
{
cout<<"Enter the name : ";
cin>>name;
cout<<"\nEnter the age : ";
cin>>age;
};
void Person::display(void)
{
cout<<"\nName : "<<name;
cout<<"\nAge : "<<age;
};
void main()
{
Person p;
p.get_data();
p.display();
}

Write a C++ Program to calculate average and sum.

#include<iostream.h>
//using namespace std;
void main()
{
float x,y,s,a;
cout<<"Enter the number : "<<"\n";
cin>>x>>y;
s=x+y;
a=s/2;
cout<<"\nSum = "<<s;
cout<<"\nAvg = "<<a;
}

Write a C program to convert km to meter, inch, cm and feet.

#include
#include

void main(){
float km,m,feet,inch,cm; 
printf("Enter the distance between two cities(in km) - ");
scanf("%f",&km);
m = km*1000; //since 1km = 1000m
feet= km*3280.84; //since 1km=3280.84feet
inch=km*39370.1; //since 1 km=39370.1inches
cm=km*100000; //since 1km = 100000cm
printf("\nDistance in kilometres = %f ",km);
printf("\nDistance in metres = %f ",m);
printf("\nDistance in feet = %f ",feet);
printf("\nDistance in inches = %f ",inch);
printf("\nDistance in centimetres = %f ",cm);
getch();
}

go to statement in c programming language.

- In c programming language, if statement, if...else statement, switch statements provides conditional jumps.
- A goto statement in C programming language provides an unconditional jump from the goto to a labeled statement in the same function.
- Use of goto statement is highly discouraged in c programming language.
- Because it makes difficult to trace the control flow of a program making the program hard to understand and debug.

- Syntax:
goto label;
.
.
.
label: statement;

-Example:

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

void main()
{

  int number = 10;

  A:
       do
       {
          if( number == 15)
      {
         number = number + 1;
         goto A;
      }
      printf("\n value of number is: %d",number);
      number++;
   
   }while( number < 20 );

   getch();
}

OUTPUT:
value of number is:11
value of number is:12
value of number is:13
value of number is:14
value of number is:15
value of number is:16
value of number is:17
value of number is:18
value of number is:19

Write a C Program to reverse the given number.

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

void main()
{
  int number,rem;
  int reverse=0;
  printf("Enter an integer number: ");
  scanf("%d", &number);
  while(number!=0)
  {
     rem=number%10;
     reverse=reverse*10+rem;
     number=number/10;
  }
  printf("Reversed Number = %d",reverse);
  getch();
}

OUTPUT:
Enter an integer number: 123
Reversed Number = 321