Professional Documents
Culture Documents
Unit 1 & Unit 2
Unit 1 & Unit 2
UNIT I
COMPUTATIONAL THINKING
problems
problems
Numeric
Text
Images
Sound
Data Encoding
All data that is stored in a computing system is encoded as bit strings.
Text
Textual characters are usually encoded as integer values using the encoding schemes.
Each number is arbitrarily associated with the image.
The associations between numbers and text are known collectively as a character encoding
scheme.
A – 65
a – 97
Color
Pictures
• The most common encoding of a digital image is that of a two-dimensional grid of colors.
• Images are typically encoded as a sequence of pixels where each pixel corresponds to a single
color.
C Programming
C programming is a general-purpose,
procedural, imperative computer
programming language developed in
1972 by Dennis M. Ritchie at the Bell
Telephone Laboratories to develop the
UNIX operating system.
The programmer writes the code for performing some task in the
human readable language, almost English every time, and saves
the file into a prescribed format. For Example – ‘.java’ for JAVA
code, ‘.cs’ for C# code, etc.
Each variable in C has a specific type, which determines the size and layout of the
variable's memory; the range of values that can be stored within that memory; and the set
of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character.
A variable definition tells the compiler where and how much storage to
create for the variable.
Syntax
Datatype variable_list;
Example
int i, j, k;
char ch, c;
float price, salary;
Assigning Value to the variable
Syntax not alter during its execution. These fixed values are
Input Input
getc();
scanf();
getchar();
gets(); fscanf();
Output
putc(); Output
putchar();
printf();
puts();
fprint();
Simple C Programs
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the
result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Addition of Two
Numbers #include <stdio.h>
int main()
{
int x, y, z;
printf("Enter two numbers to add\n");
scanf("%d%d", &x, &y);
z = x + y;
printf("Sum of the numbers = %d\n", z);
return 0;
}
#include <stdio.h>
Addition for Decimal
Values int main()
{
double a, b, c;
Enter two numbers printf("Enter two numbers\n");
3.21
7.47
scanf("%lf%lf", &a, &b);
10.680000 c = a + b;
printf("%lf\n", c);
return 0;
}
Decimal Places #include <stdio.h>
int main()
{
double a, b, c;
printf("Enter two numbers\n");
scanf("%lf%lf", &a, &b);
c = a + b;
printf("%.1lf\n", c);
printf("%.2lf\n", c);
printf("%.3lf\n", c);
printf("%.4lf\n", c);
printf("%.5lf\n", c);
printf("%.6lf\n", c);
printf("%.7lf\n", c);
printf("%.8lf\n", c);
return 0;
}
Operators in C
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators Relational Operators
Assignment Operator Logical Operator
= a=b a=b
&& Logica Returns true if both x < 5 && x
l and statements are true < 10
+= a += b a = a+b
|| Logica Returns true if one x < 5 || x < 4
-= a -= b a = a-b l or of the statements is
true
*= a *= b a = a*b
! Logica Reverse the result, !(x < 5 &&
/= a /= b a = a/b l not returns false if the x < 10)
result is true
%= a %= b a = a%b
Bitwise Operators
Operator Description
0 0 0 0 0 ^ Bitwise XOR
~
0 1 0 1 1 Bitwise Complement
switch(expression) { break;
case 2:
case x: printf("Tuesday");
break;
// code block case 3:
printf("Wednesday");
break; break;
case 4:
case y: printf("Thursday");
break;
// code block case 5:
printf("Friday");
break; break;
case 6:
default: printf("Saturday");
break;
// code block default:
printf("Sunday");
} break;
}
}
Output
Thursday
Loop in C
Looping Statements in C execute the sequence of
statements many times until the stated condition
becomes false.
A loop in C consists of two parts, a body of a loop
and a control statement.
A loop statement allows us to execute a statement
or group of statements multiple times.
The purpose of the C loop is to repeat the same
code a number of times.
Types of Loops in C
Entry controlled loop
Exit controlled loop
In an entry control loop in C, a condition is checked
before executing the body of a loop. It is also called as
a pre-checking loop.
In an exit controlled loop, a condition is checked after
executing the body of a loop. It is also called as a post-
checking loop.
Loop and Control Statements
4 nested loops
You can use one or more loops inside any other
while, for, or do..while loop.
While Loop in C
Sample Script
Syntax
while (condition)
#include<stdio.h>
{ #include<conio.h>
statements; int main()
} {
Output int num=1; //initializing the variable
1 while(num<=10) //while loop with condition
2 {
3 printf("%d\n",num);
4
num++; //incrementing operation
5
}
6
return 0;
7
}
8
9
10
do-while loop in C