Introduccion A La Programacion

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

APPENDIX F

Examples of Programs
in C, C++, and Java

In this appendix we present some examples o programs written in three languages, C,


C++, and Java, to give a general idea about the structure o these three common lan-
guages. Note that the line number at the le t o each program is not part o the program;
it is added to make re erences easier. Also note that the text in color are comments and
ignored by the compiler when the program is compiled into machine language.

F.1 PROGRAMS IN C LANGUAGE


In this section, we show three simple programs in C. The goal is not to teach the language;
it is to give an idea what programs in C look like.

Example F.1
Program F-1 is the simplest program written in the C language that prints the message
‘Hello World!’. It is an example that uses only the sequence construct, which means that
the code is executed line by line without branching or repeating some sections.

Program F-1 First program in C


1 /*
2 This program shows how we can use only sequence construct
3 to achieve a simple goal.
4 */
5
6 #include <stdio.h>
7
8 int main ()
9 {
582 Examples of Programs in C, C++, and Java

Program F-1 First program in C (Continued)


10 // Statement
11 printf (“Hello World\n”);
12 return 0;
13
14 } // End of main

Run:
Hello World

Example F.2
Program F-2 is an example o a simple program in C that uses both sequence and branch-
ing construct. I a condition is met, the program executes some lines; i not, other lines are
executed. We run the program twice to show the two di erent cases.

Program F-2 Second program in C


1 /*
2 This program shows how to make a decision in a program written in C.
3 The program gets an integer and finds if it is divisible by 7.
4 */
5
6 #include <stdio.h>
7
8 int main ()
9 {
10 // Declaration
11 int num;
12 // Statement
13 printf (“Enter an integer: “);
14 scanf (“%d”, &num);
15 // Selection
16 if (num % 7 == 0)
17 {
18 printf (“The number %d”, num);
19 printf (“ is divisible by 7.\n”);
20 }
21 else
22 {
F.1 Programs in C Language 583

Program F-2 Second program in C (Continued)


23 printf (“The number %d”, num);
24 printf (“ is not divisible by 7.\n”);
25 }
26 return 0;
27 } // End of main
Run:
Enter an integer: 24
The number 24 is not divisible by 7.
Run:
Enter an integer: 35
The number 35 is divisible by 7.

Example F.3
Program F-3 shows the combination o sequence and repetition construct. We use a loop
to repeatedly print a number, but the number is changing in each repetition. We run the
program twice: the frst time, the limit is 6; the second time the limit is 9.

Program F-3 Third program in C


1 /*
2 This program shows how to use repetition in C.
3 The program prints number from 1 to n, in which n is given by the user.
4 */
5
6 #include <stdio.h>
7
8 int main ()
9 {
10 // Declaration
11 int n;
12 int i;
13 // Statement
14 printf (“Enter the upper limit: “);
15 scanf (“%d”, &n);
16 // Repetition
17 for (i = 1; i <= n; i++)
18 {
19 printf (“%d\n”, i);
584 Examples of Programs in C, C++, and Java

Program F-3 Third program in C (continued)


20 }
21 return 0;
22 } // End of main
Run:
Enter the upper limit: 6
1
2
3
4
5
6
Run:
Enter the upper limit: 9
1
2
3
4
5
6
7
8
9

F.2 PROGRAMS IN C++ LANGUAGE


In this section, we show how to write the same three previous programs using the C++
language. The point is to show the similarity and di erences between the languages. The
C language is a procedural language in which there are no classes and objects. On the
other hand, C++ is an object-oriented language in which we can have classes and objects.

Example F.4
Program  F-4 accomplishes the same purpose as Program  F-1, but it is written in C++
instead o C. We can see the main di erence in line 14. To print data in C++, we need to
use an object. The term cout defnes an object that is responsible to output data.

Program F-4 First program in C++


1 /*
2 This program demonstrates some of the components of a simple
3 program written in C++
4 */
5
6 #include <iostream>
7 #include <iomanip>
8
F.2 Programs in C++ Language 585

Program F-4 First program in C++ (Continued)


9 using namespace std;
10
11 int main ()
12 {
13 // Statement
14 cout << “Hello World!” << endl;
15 return 0;
16 } // End of main
Run:
Hello World

Example F.5
Program  F-5 accomplishes the same purpose as Program  F-2, but it is written in C++
instead o C. The main di erence between this program and its C version is in lines 16,
17, 21, 22, 26, and 27 in which we need to use input object (cin) and output objects (cout)
or input and output.

Program F-5 Second program in C++


1 /*
2 This program shows how to make a decision in a program written in C++.
3 The program gets an integer and prints it if it is less than 50.
4 */
5
6 #include <iostream>
7 #include <iomanip>
8
9 using namespace std;
10
11 int main ()
12 {
13 // Declaration
14 int num;
15 // Statement
16 cout << (“Enter an integer: “);
17 cin >> num;
18 // Decision
19 if (num % 7 == 0)
586 Examples of Programs in C, C++, and Java

Program F-5 Second program in C++ (Continued)


20 {
21 cout << “The number “ << num;
22 cout << “ is divisible by 7.” << endl;
23 }
24 else
25 {
26 cout << “The number “ << num;
27 cout << “ is not divisible by 7.” << endl;
28 }
29 return 0;
30 } // End of main
Run:
Enter an integer: 22
The number 22 is not divisible by 7.
Run:
Enter an integer: 21
The number 21 is divisible by 7.

Example F.6
Program  F-6 accomplishes the same purpose as Program  F-3, but it is written in C++
instead o C. The main di erence between this program and its C version is in lines 17,
18, and 23 in which we need to use input object (cin) and output objects (cout) or input
and output.

Program F-6 Third program in C++


1 /*
2 This program shows how to use repetition in C++.
3 The program prints number from 1 to n, in which n is given by the user.
4 */
5
6 #include <iostream>
7 #include <iomanip>
8
9 using namespace std;
10
11 int main ()
12 {
13 // Declaration
14 int n;
F.3 Programs in Java Language 587

Program F-6 Third program in C++ (Continued)


15
16 // Statement
17 cout << “Enter the upper limit: “;
18 cin >> n;
19
20 // loop
21 for (int i = 1; i <= n; i++)
22 {
23 cout << i << endl;
24 }
25
26 return 0;
27 } // End of main
Run:
Enter the upper limit: 4
1
2
3
4
Run:
Enter the upper limit: 8
1
2
3
4
5
6
7
8

F.3 PROGRAMS IN JAVA LANGUAGE


In this section, we show how to write the same three programs in Java language. The
point is to show the similarity and di erences between the languages. The frst di erence
we encounter is in the main unction in C++ and main method in Java. In C++, the main
unction is a stand-alone program; in Java the main method should be part o a class. We
call these classes First, Second, and Third respectively in these programs.

Example F.7
Program  F-7 accomplishes the same purpose as Program  F-4, but it is written in Java
instead o C++. We need a class to host the main method. Another di erence is in line10
where we use a predefned object (System.out) or output.
588 Examples of Programs in C, C++, and Java

Program F-7 First program in Java


1 /*
2 This program demonstrates some of the components of a simple
3 program written in Java
4 */
5
6 public class First
7 {
8 public static void main (String[] args)
9 {
10 System.out.println (“Hello World!”);
11 } // End main
12 } // End class

Example F.8
Program  F-8 accomplishes the same purpose as Program  F-5, but it is written in Java
instead o C++. We need a class to host the main method. Other di erences are in lines
13, 14, 15, 20, 21, 26, and 27 where we use an object o class Scanner or input and a
predefned object (System.out) or output.

Program F-8 Second program in Java


1 /*
2 This program shows how to make a decision in a program written in Java.
3 The program gets an integer and checks if it is divisible by 7.
4 */
5
6 import java.util.*;
7
8 public class Second
9 {
10 public static void main (String[] args)
11 {
12 // Declaration
13 Scanner input = new Scanner (System.in);
14 System.out.print (“Enter an integer: “);
15 int num = input.nextInt ();
16
F.3 Programs in Java Language 589

Program F-8 Second program in Java (Continued)


17 // Decision
18 if (num % 7 == 0)
19 {
20 System.out.print (“The number “ + num);
21 System.out.println (“ is divisible by 7”);
22
23 }
24 else
25 {
26 System.out.print (“The number “ + num);
27 System.out.println (“ is not divisible by 7.”);
28
29 }
30 } // End main
31
32 } // End class
Run:
Enter an integer: 25
The number 25 is not divisible by 7.
Run:
Enter an integer: 42
The number 42 is divisible by 7.

Example F.9
Program  F-9 accomplishes the same purpose as Program  F-6, but it is written in Java
instead o C++. We need a class to host the main method. Other di erences are in lines
13, 14, 15, and 20 where we use an object o class Scanner or input and a predefned
object (System.out) or output.

Program F-9 Third program in Java


1 /*
2 This program shows how to use a loop in Java.
3 The program prints number from 1 to n, in which n is given by the user.
4 */
5
6 import java.util.*;
7
8 public class Third
590 Examples of Programs in C, C++, and Java

Program F-9 Third program in Java (Continued)


1 {
2 public static void main (String[] args)
3 {
4 // Statements to get the value of n
5 Scanner input = new Scanner (System.in);
6 System.out.print(“Enter the upper limit: “);
7 int n = input.nextInt ();
8
9 // Loop
10 for (int i = 1 ; i <= n; i++)
11 {
12 System.out.println (i);
13 }
14 } // End main
15
16 } // End class
Run:
Enter the upper limit: 3
1
2
3
Run:
Enter the upper limit: 7
1
2
3
4
5
6
7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy