EXERCISE – 1

A) Write a simple program on printing "Hello World".
B) Write a simple program on printing "Hello Name" where name is the input from the user.
C) Convert any two programs that are written in C into C++.
D) Write a description of using g++.

A) AIM: Write a simple program on printing "Hello World".
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
    cout<<"Hello World";
}
OUTPUT:
Hello World

B) AIM: Write a simple program on printing "Hello Name" where name is the input from the user.
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
    char name[20];
    cout<<"Enter your name:\n";
    cin>>name;
    cout<<"Hello "<<name;
}
OUTPUT:
Enter your name:
RISE
Hello RISE

C) AIM: Convert any two programs that are written in C into C++.
/* Addition of two numbers */
SOURCE CODE:
In C
#include<stdio.h>
void main()
{
    int a,b,c;
    printf("Enter a and b values::\n");
    scanf("%d%d",&a,&b);
    c=a+b;
    printf("\nSum of two numbers is %d",c);
}
OUTPUT:
Enter a and b values::
9
7
Sum of two numbers is 16

In C++
#include<iostream>
using namespace std;
int main()
{
    int a,b,c;
    cout<<"Enter a and b values::\n";
    cin>>a>>b;
    c=a+b;
    cout<<"\nSum of two numbers is "<<c;
}
OUTPUT:
Enter a and b values::
9
7
Sum of two numbers is 16

/* Factorial of given number */
SOURCE CODE:
In C
#include<stdio.h>
void main()
{
    int n,i,fact=1;
    printf("Enter number:\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        fact=fact*i;
    }
    printf("\nFactorial of %d is %d.",n,fact);
}
OUTPUT:
Enter number:
5
Factorial of 5 is 120.

In C++
#include<iostream>
using namespace std;
int main()
{
    int n,i,fact=1;
    cout<<"Enter number:\n";
    cin>>n;
    for(i=1;i<=n;i++)
        fact=fact*i;
    cout<<"\nFactorial of "<<n<<" is "<<fact;
}
OUTPUT:
Enter number:
5
Factorial of 5 is 120

D) AIM: Write a description of using g++.
INTRODUCTION

GNU provides are a publicly-available optimizing compilers (translator) for C, C++, Ada 95, and Objective C that currently runs under various implementations of Unix. g++ and gcc are, for most practical purposes, identical programs. Running g++ is nearly the same as gcc -lg++.

RUNNING THE COMPILER

You can use g++ both to compile programs into object modules and to link these object modules together into a single program. It looks at the names of the files you give it to determine what language they are in and what to do with them. Files of the form name.cc (or name.cpp) are assumed to be C++ files and files matching name.o are assumed to be object (i.e., machine-language) files. To translate a C++ source file, file.cc, into a corresponding object file, file.o, use the g++ command:

g++ -c compile-options file.cc

To link one or more object files, file1.o, file2.o, ..., to produced from C++ files into a single executable file called prog, use the following command:

g++ -o proglink-options file1.o file2.o ... other-libraries

You can bunch these two steps: compilation and linking, into one with the following command.

g++ -o progcompile-and-link-options file1.cc file2.cc ... other-libraries

After linking has produced an executable file called prog, it becomes, in effect, a new Unix command, which you can run with

./progarguments

where arguments denotes any command-line arguments to the program.


No comments:

Post a Comment

Total Pageviews