0% found this document useful (0 votes)
17 views19 pages

Hello World

The document introduces programming concepts like data types, operators, control flow and functions. It contains code examples to demonstrate basic programs in C like printing text, arithmetic operations, user input, if-else conditional statements. Various exercises are provided to write programs using these basic programming constructs.

Uploaded by

gameramit7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views19 pages

Hello World

The document introduces programming concepts like data types, operators, control flow and functions. It contains code examples to demonstrate basic programs in C like printing text, arithmetic operations, user input, if-else conditional statements. Various exercises are provided to write programs using these basic programming constructs.

Uploaded by

gameramit7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Introduction to Programming

Raghavendra Kanakagiri

Fall 2023

Introduction to Programming Fall 2023 1 / 19


Hello World

1 # include < stdio .h >


2 int main ( void )
3 {
4 printf ( " Hello world \ n " ) ;
5 return 0;
6 }

Introduction to Programming Fall 2023 2 / 19


Overview

Hardware
Assembler
Compiler

Introduction to Programming Fall 2023 3 / 19


Hello World

Line 1: preprocessor directive


1 # include < stdio .h > Line 2: main function
2 int main ( void )
Line 3: open curly brace
3 {
4 printf ( " Hello world \ n " ) ; Line 4: printf function
5 return 0; Line 5: return statement
6 }
Line 6: close curly brace

Introduction to Programming Fall 2023 4 / 19


Hello World

1 # include < stdio .h >


2 int main ( void ) $ gcc hello.c
3 {
4 printf ( " Hello world \ n " ) ;
5 return 0; $ ./a.out
6 }

Introduction to Programming Fall 2023 5 / 19


Hello World

1 /*
2 Author : James Bond
3 */
4 # include < stdio .h >
Lines 1 to 3: multi-line comment
5 int main ( void )
6 { Line 7: single line comment
7 // say hello
8 printf ( " Hello world \ n " ) ;
9 return 0;
10 }

Introduction to Programming Fall 2023 6 / 19


Hello World

1 # include < stdio .h >


2 int main ( void ) { printf ( " Hello world \ n " ) ; return 0; }

Introduction to Programming Fall 2023 7 / 19


Hello World

1 # include < stdio .h >


2 int main ( void ) $ gcc -o hello hello.c
3 {
4 printf ( " Hello world \ n " ) ;
5 return 0; $ ./hello
6 }

Introduction to Programming Fall 2023 8 / 19


1 # include < stdio .h >
2 int main ( void )
3 {
4 int a ;
5 int b ; Lines 4 to 6: variable declaration
6 int c ; Lines 7 and 8: variable assignment
7 a = 10; Line 9: arithmetic operation
8 b = 20;
9 c = a + b; Line 10: printf function
10 printf ( " Sum of % d and % d is %
d\n", a, b, c);
11 return 0;
12 }

Introduction to Programming Fall 2023 9 / 19


1 # include < stdio .h >
2 int main ( void ) 1 # include < stdio .h >
3 { 2 int main ( void )
4 int a , b , c ; 3 {
5 a = 10; 4 int a = 10 , b = 20 , c ;
6 b = 20; 5 c = a + b;
7 c = a + b; 6 printf ( " Sum of % d and % d is %
8 printf ( " Sum of % d and % d is % d\n", a, b, c);
d\n", a, b, c); 7 return 0;
9 return 0; 8 }
10 }

Introduction to Programming Fall 2023 10 / 19


Lines 7 and 8: user input

$ ./a.out
1 # include < stdio .h >
2 int main ( void )
3 { 1 2
4 int a ;
5 int b ;
6 int c ; $ ./a.out
7 scanf ( " % d " , & a ) ;
8 scanf ( " % d " , & b ) ;
9 c = a - b; 1 a
10 printf ( " Difference of % d and
% d is % d \ n " , a , b , c ) ;
11 return 0; $ ./a.out
12 }

1.4 3.2

Introduction to Programming Fall 2023 11 / 19


A better way to write the previous program?

1 # include < stdio .h >


2 int main ( void )
3 {
4 int a ;
5 int b ;
$ ./a.out
6 int c ;
7 printf ( " Enter two numbers \ n " ) ;
Enter two numbers
8 scanf ( " % d " , & a ) ;
9 scanf ( " % d " , & b ) ;
10 c = a - b; 1 2
11 printf ( " Difference of % d and % d
is % d \ n " , a , b , c ) ;
12 return 0;
13 }

Introduction to Programming Fall 2023 12 / 19


Arithmetic Operators

Operator Operation Level of precedence


* Multiplication 1
/ Division 1
% Modulus (Remainder) 1
+ Addition 2
- Subtraction 2

Level of precedence: Operators with higher precedence are evaluated first.


Operators with the same level of precedence are evaluated from left to right.

Introduction to Programming Fall 2023 13 / 19


Arithmetic Operators

Operator Operation Level of precedence


* Multiplication 1
/ Division 1
% Modulus (Remainder) 1
+ Addition 2
- Subtraction 2

x = a + b * c;

x = a + b / c;

x = a * b + c;

x = a * b % c + d / e - f;

Introduction to Programming Fall 2023 14 / 19


Paranthesis

Paranthesis can be used to change the order of evaluation. They are evaluated first.
If the paranthesis are nested, the innermost paranthesis are evaluated first.
If parantheiss are on the same level, they are evaluated from left to right.

x = (a + b) * c;

x = (a + b) / c;

x = a * b % c + d / e - f;

x = ((a * b) % c) + (d / e) - f;

Introduction to Programming Fall 2023 15 / 19


if-else
1 # include < stdio .h >
2 int main ( void )
3 {
4 int a , b , c ;
5 int sum _or_di fferen ce ;
6 printf ( " Enter two numbers \ n " ) ;
7 scanf ( " % d " , & a ) ;
8 scanf ( " % d " , & b ) ;
9 printf ( " Enter 1 for sum and 2 for difference \ n " ) ;
10 scanf ( " % d " , & sum_o r_diff erenc e ) ;
11 if ( sum_o r_diff erenc e == 1) {
12 c = a + b;
13 printf ( " Sum of % d and % d is % d \ n " , a , b , c ) ;
14 }
15 else {
16 c = a - b;
17 printf ( " Difference of % d and % d is % d \ n " , a , b , c ) ;
18 }
19 return 0;
20 }
Introduction to Programming Fall 2023 16 / 19
if-else

Better?
Replace lines 11 to 18 in the previous program with the following:
1 if ( sum_o r_diff erence == 1) {
2 c = a + b;
3 printf ( " Sum of % d and % d is % d \ n " , a , b , c ) ;
4 }
5 else if ( sum_o r_diff erence == 2) {
6 c = a - b;
7 printf ( " Difference of % d and % d is % d \ n " , a , b , c ) ;
8 }
9 else {
10 printf ( " Invalid input \ n " ) ;
11 }

Introduction to Programming Fall 2023 17 / 19


Equality and Relational Operators

Operator Operation
== Equality
!= Inequality
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Introduction to Programming Fall 2023 18 / 19


Write a program to check if it is odd or even.

Write a program that reads in 4 numbers and prints the largest and
smallest of them.

Separate the digits of a number (assume the number has 4 digits).

Write a program that reads in a number and prints the sum of its
digits.

Write a program that prints the square and cube of the numbers from 1
to 10.

Write a program that prints the multiplication table (from 1 to 10)


of a number.

Introduction to Programming Fall 2023 19 / 19

You might also like