CPCS301_Topic2
CPCS301_Topic2
languages
Objective
Students should get familiar with: C, Python, PHP and MATLAB language and some simple
programs.
Current Lab Learning Outcomes (LLO)
Lab Description
1. Running programs in C:
INPUT / OUTPUT:
The important aspects of C programming language are its ability to handle input and output
(I/O). A program using input / output functions must include the standard header file (stdio.h) in
it using the directive.
P a g e 1 | 21
Example 1:
C program for printing Fibonacci series up to n numbers
Code
/* PROGRAM FOR PRINTING FIBONACCI SERIES UPTO N NUMBERS */
#include <stdio.h>
#include <conio.h>
void main()
{
int num1 = 0, num2 = 1, no, counter, fib;
printf("\n\n<----FIBONACCI SERIES---->");
printf("\n\n\t\t %d %d", num1, num2);
Libraries In C Language:
#include <stdio.h>: standard input/output library for using functions like printf and scanf
#include <conio.h>: For console input/output functions like getch() (used to wait for a
keypress).
#include <string.h>: Provides string-related functions like strlen.
getch();: Waits for the user to press a key before the program exits.
P a g e 2 | 21
1. For Integers:
o %d or %i: Signed decimal integer (e.g., 42, -42)
o %u: Unsigned decimal integer (e.g., 42 but not -42)
o %o: Unsigned octal integer (e.g., 052)
o %x or %X: Unsigned hexadecimal integer (x for lowercase, X for uppercase, e.g.,
0x2A)
2. For Floating-Point Numbers:
o %f: Decimal floating-point number (e.g., 3.14)
o %e or %E: Scientific notation (e.g., 3.14e+00)
3. For Characters:
o %c: Single character (e.g., 'A')
o %s: String of characters (e.g., "Hello")
4. For Pointers:
o %p: Pointer address (e.g., 0x7fff5fbff718)
5. For Length Modifiers (Used with %d, %u, %f, etc.):
o %ld: Long integer (e.g., 2147483647L)
o %lld: Long long integer (e.g., 9223372036854775807LL)
o %hd: Short integer (e.g., 32767)
o %lf: Double-precision floating-point number (e.g., 3.141592653589793)
o %Lf: Long double (extended precision floating-point number)
6. Miscellaneous:
o %%: Prints the % character.
scanf("%d", &no);
• The & operator is used to pass the memory address of the variable no to scanf.
• This allows scanf to directly store the value entered by the user into the memory location of
no.
Code
/* PROGRAM FOR PRINTING FIBONACCI SERIES UPTO N NUMBERS */
#include <stdio.h>
void main()
{
int num1 = 0, num2 = 1, no, counter, fab;
P a g e 3 | 21
printf("\n\n<----FIBONACCI SERIES---->");
printf("\n\n\t\t %d %d", num1, num2);
Example 2:
C program to find the sum of individual digits of a positive integer:
Code
#include<stdio.h>
#include<conio.h>
void main()
{
int num, k = 1, sum = 0;
printf("\n **Enter the number whose digits are to be added: ");
scanf("%d", &num);
while (num != 0)
{
k = num % 10;
P a g e 4 | 21
sum = sum + k;
k = num / 10;
num = k;
}
printf("\n **Sum of the digits: %d", sum);
getch();
}
Output (in Visual Studio 2013)
Code
#include<stdio.h>
void main()
{
int num, k, sum = 0;
printf("\n **Enter the number whose digits are to be added: ");
scanf("%d", &num);
while (num != 0)
{
k = num % 10;
sum = sum + k;
k = num / 10;
num = k;
}
printf("\n **Sum of the digits: %d", sum);
}
Output
P a g e 5 | 21
Note:
• Modulus Operator (%):
Example 3:
C program to count the lines, words and characters in given text:
Code
/* Write a C program to count the lines, words and characters in given text*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char line[81], ctr;
int i, c,end = 0,characters = 0,words = 0,lines = 0;
printf("\n KEY IN THE TEXT.\n");
printf(" GIVE ONE SPACE AFTER EACH WORD.\n");
printf(" WHEN COMPLETED, PRESS 'ENTER' twice.\n\n");
while (end == 0)
{
/* Reading a line of text */
c = 0;
while ((ctr = getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
/* counting the words in a line */
if (line[0] == '\0')
break;
P a g e 6 | 21
else
{
words++;
for (i = 0; line[i] != '\0'; i++)
if (line[i] == ' ' || line[i] == '\t')
words++;
}
/* counting lines and characters */
lines = lines + 1;
characters = characters + strlen(line);
}
printf("\n");
printf(" Number of lines = %d\n", lines);
printf(" Number of words = %d\n", words);
printf(" Number of characters = %d\n", characters);
getch();
}
Output (in Visual Studio 2013)
Code
/* Write a C program to count the lines, words and characters in a given text*/
#include <stdio.h>
#include <string.h>
void main()
{
/* line[81]: Stores the input line (up to 80 characters) */
/* ctr: Reads characters from input */
/* end: Flag to indicate the end of text entry (set to 0 initially) */
char line[81], ctr;
int i, c,end = 0,characters = 0,words = 0,lines = 0;
printf("\n KEY IN THE TEXT.\n");
printf(" GIVE ONE SPACE AFTER EACH WORD.\n");
printf(" WHEN COMPLETED, PRESS 'ENTER' twice.\n\n");
P a g e 7 | 21
/* This loop continues until the user presses Enter twice, signaling the end of
input */
while (end == 0)
{
/* Reading a line of text */
c = 0;
while ((ctr = getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
/* counting the words in a line */
if (line[0] == '\0')
break;
else
{
words++;
for (i = 0; line[i] != '\0'; i++)
if (line[i] == ' ' || line[i] == '\t')
words++;
}
/* counting lines and characters */
lines = lines + 1;
characters = characters + strlen(line);
}
printf("\n");
printf(" Number of lines = %d\n", lines);
printf(" Number of words = %d\n", words);
printf(" Number of characters = %d\n", characters);
}
Output
There are two main components we will be using to write a program in Python;
• Python Shell
• File Window
Code
#Euclid Algorithm
import math
def Euclid (m,n):
P a g e 9 | 21
while n!=0:
r=m%n #m % n computes the remainder of m divided by n
m=n #This remainder (r) will become the new value of n in the next iteration.
n=r
return m
• Commented lines in Python start with a “#”. All text beyond “#” will be ignored.
“System.out.println” in Java.
Try it out:
P a g e 13 | 21
3. Running PHP programs using XAMPP:
1. Write the following code, and save the file in c:\\xampp\htdocs\cpcs301 directory
as c:\\xampp\htdocs\cpcs301\Welcome.php. [Create cpcs301 folder in
c:\\xampp\htdocs.]
<html>
<?php
$name = "Introduction to Internet Programming"; //
declaration and initialization
?>
<head>
<title> Welcome </title>
</head>
<body>
<p>
Welcome to PHP, <?php print( "$name\n" ); ?>!
</p>
</body>
</html>
2. Now, open the browser and write the following in the address bar
http://localhost/cpcs301/Welcome.php to see the program output. [Again,
I assume that the Apache Web server is running. If not double click on the xampp
shortcut on the desktop which would open xampp control panel application. Start Apache
by clicking on the start button. You’d see the following.]
P a g e 14 | 21
Result is shown in following figure.
P a g e 15 | 21
In PHP, you can print output to the screen using functions like print and echo. Both are used to
display text, variables, or HTML content.
1. Using echo
• Syntax:
• Examples:
P a g e 16 | 21
2. Using print
• Syntax:
• Examples:
print("Hello, World!");
print("This is a heading");
$name = "PHP";
print("Welcome to $name programming!");
- Double clicking on the MATLAB icon that should be on the desktop of your computer.
P a g e 17 | 21
- The Command Window allows a user to enter simple commands.
- Next press the Enter or Return key.
For instance,
>> s = 1 + 2
s=
3
P a g e 18 | 21
Table 1 some commands of Matlab
P a g e 19 | 21
P a g e 20 | 21
Extra (supplementary) Materials (if any)
P a g e 21 | 21