Professional Documents
Culture Documents
04 Writing Code
04 Writing Code
Writing Code
College of Engineering
Department of Computer and Communications Engineering
IDE Setup
Code::Blocks
First program
/*
* File: main.c
* Author: Murad Alaqtash
*/
#include<stdio.h>
int main(){
printf("Salam Alykom\n");
return 0;
}
Writing a Program
Writing an Algorithm
Example
Given an integer N > 0, print a right triangle of *s, with height and base
of N
Example - Step 1
If N = 4, you would print:
*
**
***
****
Example - Step 2
What we have done:
print 1 star
print a newline
print 2 stars
print a newline
print 3 stars
print a newline
print 4 stars
print a newline
Example - Step 3
For N=5:
Count from 1 to 5 (inclusive) call it i
print i stars
print a newline
In general:
Count from 1 to N (inclusive) call it i
print i stars
print a newline
Example - Step 4
Test your algorithm
Example - Step 5
Translate to code
Example - Code
//Count from 1 to N (inclusive) call it i
for(int i=1;i<=N;i++){
//print i stars
//print a newline
}
Example - Code
//Count from 1 to N (inclusive) call it i
for(int i=1;i<=N;i++){
//print i stars
for(int j=1;j<=i;j++){
printf("*");
}
//print a newline
printf("\n");
}
Exercise 1
The numbers in the table below are the result of executing an algorithm
that has one parameter N, a non-negative integer, and produces
sequences of integers as outputs. For values of N from 0 to 5, the
algorithm produces the following sequences of numbers as outputs.
N Output
0 02
1 3579
2 6 8 10 12 14 16
3 9 11 13 15 17 19 21 23
4 12 14 16 18 20 22 24 26 28 30
5 15 17 19 21 23 25 27 29 31 33 35 37
Exercise 2
Check for a prime number. Your program takes an integer positive
number, check if its a prime and prints a ”Prime/Not a prime”.