0% found this document useful (0 votes)
10 views24 pages

Unit I QB

The document covers fundamental concepts of Embedded C Programming, including definitions of variables and constants, data types, functions, and control structures. It explains the syntax and usage of various programming constructs like arrays, loops, and function prototypes, along with examples. Additionally, it discusses the advantages of user-defined functions and the differences between call by value and call by reference methods.

Uploaded by

vanitha
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)
10 views24 pages

Unit I QB

The document covers fundamental concepts of Embedded C Programming, including definitions of variables and constants, data types, functions, and control structures. It explains the syntax and usage of various programming constructs like arrays, loops, and function prototypes, along with examples. Additionally, it discusses the advantages of user-defined functions and the differences between call by value and call by reference methods.

Uploaded by

vanitha
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/ 24

lOMoARcPSD|57066064

EMB C UNIT I QB (1) - Embedded C Programming

Electrical & Electronics Engineering (Anna University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Vanitha Rathinakumar ([email protected])
lOMoARcPSD|57066064

EE3017 EMBEDDED C PROGRAMMING


UNIT I BASIC C PROGRAMMING

PART A

1. Define the term variable and constant.


Variable:
A variable is a data name that is used to store a value. A variable may take different
values at different times. A variable can be chosen by the programmer in a meaningful way. So
as to reflect its function or nature in the program.
Eg:
city college a1
Total_mark Name Avg
Constant:
A constant is a value that does not change during the program execution. A constant used in C
does not occupy memory.

2.Specify the use of printf( ) and scanf( ) functions.

The function scanf() is used for formatted input from the standard input and provides many of
the conversion facilities.
It is used for formatted output to standard output device,( screen) . The format specification
contains string and the data to be output, as the arguments(parameter) to the printf() function.

3.Write an initialization of an array?


The general form of declaring the array during the initialization is Datatype arrayname[size]={
list of values}; The Values in the list are separated by commas.

4.Specify the syntax used for the ‘for’ statement.


The for loop is entry controlled loop the provides a more concise loop control structure.
The general format of the loop is

for(intilialization;test-condition;increment/decrem
ent)
{
Body of the loop;
}

5.What are function prototypes?


A function declaration is also called as function prototype consists of four parts.
· Function type(return type)
· Function name
· Parameter list
· Terminating semicolon.

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

6. What are the different types of array?


There are three types of arrays. They are: One dimensional array Two dimensional array &
Multidimensional array.

7.What is the use of void data type?


The void data type is typically used in the definition and prototyping of functions to indicate that
either nothing is being passed in and/or nothing is being returned.
void data type
A data type that has no values or operators and is used to represent nothing.

8.What is a recursive function? Give an example.


Recursive function is a function which contains a call to itself.
Eg:
int factorial(int n)
{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}

9. Write the general form of structure of C functions.


A C program basically has the following form
o preprocessor commands
o type definitions
o function prototype
o variable declaration
o function body

10. What is an identifier?


Identifier are names that are given to various program elements such as variables, functions and
arrays. Identifier consist of letters and digits, in any order, except that the first character must be
a letter. Both upper and lower case letters are permitted though common usage favours the use of
lower letters. The underscore is treated as a letter.

11.What are the advantages of functions?

v It reduces the complexity in a program by reducing the code.


v Functions are easily understandable and execution is faster.
v It also reduces the time to run a program.
v It’s easy to find-out the errors due to the blocks made as function definition outside the
main function.

12.What are the steps in writing a function in a program?


Function Declaration (Prototype declaration):
Every user-defined functions has to be declared before the main().

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

Function Callings: The user-defined functions can be called inside any functions like main(),
userdefined function, etc.
Function Definition: The function definition block is used to define the user-defined functions
with statements.

13.State the advantages of user defined functions over pre-defined function.


A user defined function allows the programmer to define the exact function of the module as per
requirement. This may not be the case with predefined function. It may or may not serve the
desired purpose completely. A user defined function gives flexibility to the programmer to use
optimal programming instructions, which is not possible in predefined function.

14.Write the syntax of while statement in C.

The general format of the while statement is

while(test_condition)
{
Body of the loop;
}

15. What are keywords? Give example.

Keywords have fixed meaning and these meaning cannot be changed. These keywords
can be used only for their intended purpose, they cannot be used as programmer-defined
identifiers.
The following are examples of few keywords are predefined by C
auto double int struct
break else if switch
case enum do while

PART B &C
1.Define a data type. Mention the different data types supported by C language, giving an
example to each
Each variable in C has an associated data type. It specifies the type of data that the variable can
store like integer, character, floating, double, etc. Each data type requires different amounts of
memory and has some specific operations which can be performed over it. The data type is a
collection of data with values having fixed values, meaning as well as its characteristics.
The data types in C can be classified as follows:
Types Description

Primitive Data Primitive data types are the most basic data types that are used for
Types representing simple values such as integers, float, characters, etc.

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

User Defined
The user-defined data types are defined by the user himself.
Data Types

The data types that are derived from the primitive or built-in
Derived Types
datatypes are referred to as Derived Data Types.

Different data types also have different ranges up to which they can store numbers. These ranges
may vary from compiler to compiler. Below is a list of ranges along with the memory
requirement and format specifiers on the 32-bit GCC compiler.
Size
Range Format
Data Type (bytes)
Specifier

2 -32,768 to 32,767 %hd


short int

unsigned
2 0 to 65,535 %hu
short int

unsigned int 4 0 to 4,294,967,295 %u

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

int 4 -2,147,483,648 to 2,147,483,647 %d

long int 4 -2,147,483,648 to 2,147,483,647 %ld

unsigned long
4 0 to 4,294,967,295 %lu
int

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long
8 0 to 18,446,744,073,709,551,615 %llu
long int

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

float 4 %f
1.2E-38 to 3.4E+38

double 8 %lf
1.7E-308 to 1.7E+308

long double 16
3.4E-4932 to 1.1E+4932 %Lf

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

The following are some main primitive data types in C:


Integer Data Type
The integer datatype in C is used to store the whole numbers without decimal values. Octal
values, hexadecimal values, and decimal values can be stored in int data type in C.
● Range: -2,147,483,648 to 2,147,483,647
● Size: 4 bytes
● Format Specifier: %d
Syntax of Integer
We use int keyword to declare the integer variable:
int var_name;
The integer data type can also be used as
1. unsigned int: Unsigned int data type in C is used to store the data values from zero to
positive numbers but it can’t store negative values like signed int.
2. short int: It is lesser in size than the int by 2 bytes so can only store values from
–32,768 to 32,767.
3. long int: Larger version of the int datatype so can store values greater than int.
4. unsigned short int: Similar in relationship with short int as unsigned int with int.
Note: The size of an integer data type is compiler-dependent. We can use sizeof operator to
check the actual size of any data type.
Example of int

// C program to print Integer data types.


#include <stdio.h>
int main()
{
// Integer value with positive data.
int a = 9;
// integer value with negative data.
int b = -9;
// U or u is Used for Unsigned int in C.
int c = 89U;
// L or l is used for long int in C.
long int d = 99998L;
printf("Integer value with positive data: %d\n", a);
printf("Integer value with negative data: %d\n", b);
printf("Integer value with an unsigned int data: %u\n",
c);
printf("Integer value with an long int data: %ld", d);
return 0;
}

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

Output
Integer value with positive data: 9
Integer value with negative data: -9
Integer value with an unsigned int data: 89
Integer value with an long int data: 99998
Character Data Type
Character data type allows its variable to store only a single character. The size of the character is
1 byte. It is the most basic data type in C. It stores a single character and requires a single byte of
memory in almost all compilers.
● Range: (-128 to 127) or (0 to 255)
● Size: 1 byte
● Format Specifier: %c
Syntax of char
The char keyword is used to declare the variable of character type:
char var_name;
Example of char

// C program to print Integer data types.


#include <stdio.h>
int main()
{
char a = 'a';
char c;
printf("Value of a: %c\n", a);
a++;
printf("Value of a after increment is: %c\n", a);

// c is assigned ASCII values


// which corresponds to the
// character 'c'
// a-->97 b-->98 c-->99
// here c will be printed
c = 99;
printf("Value of c: %c", c);
return 0;
}

Output
Value of a: a
Value of a after increment is: b
Value of c: c
Float Data Type
In C programming float data type is used to store floating-point values. Float in C is used to store
decimal and exponential values. It is used to store decimal numbers (numbers with floating point
values) with single precision.

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

● Range: 1.2E-38 to 3.4E+38


● Size: 4 bytes
● Format Specifier: %f
Syntax of float
The float keyword is used to declare the variable as a floating point:
float var_name;
Example of Float

// C Program to demonstrate use


// of Floating types
#include <stdio.h>
int main()
{
float a = 9.0f;
float b = 2.5f;
// 2x10^-4
float c = 2E-4f;
printf("%f\n", a);
printf("%f\n", b);
printf("%f", c);
return 0;
}

Output
9.000000
2.500000
0.000200
Double Data Type
A Double data type in C is used to store decimal numbers (numbers with floating point values)
with double precision. It is used to define numeric values which hold numbers with decimal
values in C.
The double data type is basically a precision sort of data type that is capable of holding 64 bits of
decimal numbers or floating points. Since double has more precision as compared to that float
then it is much more obvious that it occupies twice the memory occupied by the floating-point
type. It can easily accommodate about 16 to 17 digits after or before a decimal point.
● Range: 1.7E-308 to 1.7E+308
● Size: 8 bytes
● Format Specifier: %lf
Syntax of Double
The variable can be declared as double precision floating point using the double keyword:
double var_name;
Example of Double

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

// C Program to demonstrate
// use of double data type
#include <stdio.h>
int main()
{
double a = 123123123.00;
double b = 12.293123;
double c = 2312312312.123123;
printf("%lf\n", a);
printf("%lf\n", b);
printf("%lf", c);
return 0;
}

Output
123123123.000000
12.293123
2312312312.123123
Void Data Type
The void data type in C is used to specify that no value is present. It does not provide a result
value to its caller. It has no values and no operations. It is used to represent nothing. Void is used
in multiple ways as function return type, function arguments as void, and pointers to void.
Syntax:
// function return type void
void exit(int check);
// Function without any parameter can accept void.
int print(void);
// memory allocation function which
// returns a pointer to void.
void *malloc (size_t size);
Example of Void

// C program to demonstrate
// use of void pointers
#include <stdio.h>
int main()
{
int val = 30;
void* ptr = &val;
printf("%d", *(int*)ptr);
return 0;
}

Output
30

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

2.Briefly discuss C functions with number of arguments.


A function in C can be called either with arguments or without arguments. These functions may
or may not return values to the calling functions. All C functions can be called either with
arguments or without arguments in a C program. Also, they may or may not return any values.
Hence the function prototype of a function in C is as below:

Call by Value
Call by value in C is where in the arguments we pass value and that value can be used in function
for performing the operation. Values passed in the function are stored in temporary memory so
the changes performed in the function don’t affect the actual value of the variable passed.
Example:

// C Program to implement
// Call by value
#include <stdio.h>
// Call by value
int sum(int x, int y)
{
int c;
c = x + y;
// Integer value retured
return c;
}
// Driver Code
int main()
{
// Integer Declared
int a = 3, b = 2;
// Function Called
int c = sum(a, b);
printf("Sum of %d and %d : %d", a, b, c);
return 0;
}

Output
Sum of 3 and 2 : 5
Call by Reference
Call by reference is the method in C where we call the function with the passing address as
arguments. We pass the address of the memory blocks which can be further stored in a pointer

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

variable that can be used in the function. Now, changes performed in the values inside the
function can be directly reflected in the main memory.
Example:

// C Program to implement
// Call by reference
#include <stdio.h>

// Call by reference
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
// Driver Code
int main()
{
// Declaring Integer
int x = 1, y = 5;
printf("Before Swapping: x:%d , y:%d\n", x, y);
// Calling the function
swap(&x, &y);
printf("After Swapping: x:%d , y:%d\n", x, y);
return 0;
}

Output
Before Swapping: x:1 , y:5
After Swapping: x:5 , y:1
Types of Function According to Arguments and Return Value
Functions can be differentiated into 4 types according to the arguments passed and value returns
these are:
1. Function with arguments and return value
2. Function with arguments and no return value
3. Function with no arguments and with return value
4. Function with no arguments and no return value
1. Function with arguments and return value
Syntax:
Function declaration : int function ( int );
Function call : function( x );
Function definition:
int function( int x )
{
statements;
return x;
}

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

2. Function with arguments but no return value


When a function has arguments, it receives any data from the calling function but it returns no
values. These are void functions with no return values.
Syntax:
Function declaration : void function ( int );
Function call : function( x );
Function definition:
void function( int x )
{
statements;
}
3. Function with no argument and no return value
When a function has no arguments, it does not receive any data from the calling function.
Similarly, when it does not return a value, the calling function does not receive any data from the
called function.
Syntax:
Function declaration : void function();
Function call : function();
Function definition :
void function()
{
statements;
}

4. Function with no arguments but returns a value


There could be occasions where we may need to design functions that may not take any
arguments but returns a value to the calling function. An example of this is getchar function
which has no parameters but it returns an integer and integer-type data that represents a
character.
Syntax:
Function declaration : int function();
Function call : function();
Function definition :
int function()
{
statements;
return x;
}

3.Explain the various control statements in C language with examples .


Control statements are an essential aspect of programming languages like C, as they allow
programmers to control the flow of execution of their programs. In C, there are three types of
control statements: selection statements, iteration statements, and jump statements.
Control statements in C are programming constructs that are used to control the flow of
execution in a program. They allow a programmer to specify conditions that determine which

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

statements are executed and which are skipped, to loop through statements until a condition is
met, or to jump to a different part of the program.
Characteristics:
○ Control statements serve to control the execution flow of a program.
○ Control statements in C are classified into selection statements, iteration statements, and
jump statements.
○ Selection statements serve to execute code based on a certain circumstance.
○ Iteration statements loop through a code block until a condition is met.
○ Jump statements serve to move control from one section of a program to another.
○ The if statement, for loop, while loop, switch statement, break statement, and continue
statement are C's most widely used control statements.
Usage:
Control statements are used extensively in programming, especially in more complex programs.
By managing the flow of execution based on particular conditions, they enable a programmer to
design more efficient and understandable code.
C Code:
Here is an example of how control statements can be used in C:
1. #include <stdio.h>
2.
3. int main() {
4. int num = 10;
5.
6. if (num > 0) {
7. printf("The number is positive\n");
8. } else {
9. printf("The number is negative\n");
10. }
11.
12. for (int i = 0; i < 5; i++) {
13. printf("%d\n", i);
14. }
15.
16. int i = 0;
17. while (i < 5) {
18. printf("%d\n", i);
19. i++;
20. }
21.
22. switch (num) {
23. case 0:
24. printf("The number is zero\n");
25. break;
26. case 10:
27. printf("The number is ten\n");

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

28. break;
29. default:
30. printf("The number is not zero or ten\n");
31. break;
32. }
33.
34. return 0;
35. }
Output
The number is positive
0
1
2
3
4
0
1
2
3
4
The number is ten
The above C code demonstrates the use of different control statements such as if-else statements,
switch statements, for loops, while loops and return statements. The code initializes a variable
'num' to 10 and then proceeds to use different control statements to manipulate the flow of the
program based on the value of 'num'.
The first control statement is an if-else statement that examines whether 'num' is greater than
zero. If it is, the program prints to the console, "The number is positive." If not, it displays "The
number is negative." This shows how to utilize selection statements in C.
The for loop is the second control statement used, which sets a variable 'i' to 0 and then
performs a block of code as long as 'i' is less than 5. The program prints the value of 'i' to the
console within the code block. This shows how to utilize iteration statements in C.
The while loop is the third control statement used. It sets a variable 'i' to 0 and then performs a
code block if 'i' is less than 5. The program within the block of code prints the value of 'i' to the
console and increments 'i' by 1. This also shows how to utilize iteration statements in C.
The fourth control statement used is the switch statement, which checks the value of 'num' and
executes different blocks of code depending on its value. If 'num' is 0, the program prints "The
number is zero". If 'num' is 10, the program prints "The number is ten". If 'num' is neither 0 nor
10, the program prints "The number is not zero or ten". This demonstrates the use of switch
statements in C.
Finally, the program ends with a return statement, which returns the value 0 to the operating
system to indicate that the program ran successfully.
In conclusion, the above C code provides a simple example of how different control statements
can be used in a program to manipulate the flow of execution based on certain conditions. It is a
good starting point for beginners to understand the basic concepts of control statements in C.
Advantages:

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

○ Control statements allow programmers to control the flow of execution in their programs,
enabling it to be simple to develop efficient and understandable code.
○ They allow programmers to handle different scenarios and conditions in their programs.
Disadvantages:
○ Overuse of control statements can make code difficult to read and maintain.
○ Complex control statements can be difficult to debug and can introduce bugs in the code.
4. What is an operator? Explain the arithmetic, relational, logical, and assignment
operators in C language.
An operator is a symbol that operates on a value or a variable. For example: + is an operator to
perform addition.
C has a wide range of operators to perform various operations.

C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).
Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division (modulo division)


Example 1: Arithmetic Operators
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

return 0;
}
Run Code
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is also an integer. The
compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is
1. The % operator can only be used with integers.
Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,
// Either one of the operands is a floating-point number
a/b = 2.5
a/d = 2.5
c/b = 2.5
// Both operands are integers
c/d = 2

C Increment and Decrement Operators


C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two
operators are unary operators, meaning they only operate on a single operand.
Example 2: Increment and Decrement Operators
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

return 0;
}
Run Code
Output
++a = 11

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes. These two operators can also be used as
postfixes like a++ and a--. Visit this page to learn more about how increment and decrement
operators work when used as postfix.

C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b
Example 3: Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

return 0;
}
Run Code
Output
c=5
c = 10
c=5
c = 25
c=5
c=0

C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0

Example 4: Relational Operators


// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

printf("%d != %d is %d \n", a, b, a != b);


printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

return 0;
}
Run Code
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.
Opera Meaning Example
tor

&& Logical AND. True only if If c = 5 and d = 2 then, expression


all operands are true ((c==5) && (d>5)) equals to 0.

|| Logical OR. True only if If c = 5 and d = 2 then, expression


either one operand is true ((c==5) || (d>5)) equals to 1.

! Logical NOT. True only if If c = 5 then, expression !(c==5)


the operand is 0 equals to 0.

Example 5: Logical Operators


// Working of logical operators

#include <stdio.h>

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}

Run Code
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program
● (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
● (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
● (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
● (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
● !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
● !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
5.Write an algorithm/flowchart and C program to compute roots of quadratic equation
Start
Read a, b, c values
Compute d = b2 4ac
if d > 0 then
r1 = b+ sqrt (d)/(2*a)

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

r2 = b sqrt(d)/(2*a)
Otherwise if d = 0 then
compute r1 = -b/2a, r2=-b/2a
print r1,r2 values
Otherwise if d < 0 then print roots are imaginary
Stop

# include<stdio.h>
# include<math.h>

int main () {
float a,b,c,r1,r2,d;

printf ("Enter the values of a b c: ");


scanf (" %f %f %f", &a, &b, &c);

d= b*b - 4*a*c;
if (d>0) {
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", r1, r2);
}

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

else if (d==0) {
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("Roots are equal =%f %f", r1, r2);
}
else
printf("Roots are imaginary");

return 0;
}
6.Write a C program using a function to check whether the given number is palindrome or
not.
Algorithm
A simple method for this problem is to first reverse all the digits of a given number and then
compare the reverse of the number with a given number. If both are the same, then return true,
else false.
1. Declare an integer reversed = 0 and num = original_number
2. Now to reverse the number, do the following while num >= 0
1. reverse = 10 * reverse + num % 10;
2. num /= 10;
3. After getting the reverse number, compare it with the original _number,
4. If the reverse is equal to the original_number, then the number is palindrome.
5. Else, the number is not palindrome.
C program to check whether a number is palindrome or not
#include <stdio.h>
// Driver code
int main()
{
// This is our given number
int original_number = 12321;

// This variable stored reversed digit


int reversed = 0;
int num = original_number;
// Execute a while loop to reverse
// digits of given number
while (num != 0) {
int r = num % 10;
reversed = reversed * 10 + r;
num /= 10;
}
// Compare original_number with
// reversed number
if (original_number == reversed) {
printf(" Given number %d is a palindrome number",
original_number);

Downloaded by Vanitha Rathinakumar ([email protected])


lOMoARcPSD|57066064

}
else {
printf(
" Given number %d is not a palindrome number",
original_number);
}
return 0;
}
7.Write an algorithm and C program to find the sum of numbers from 1 to n‘
Step by step descriptive logic to find sum of n natural numbers.
1. Input upper limit to find sum of natural numbers. Store it in some variable say N.
2. Initialize another variable to store sum of numbers say sum = 0.
3. In order to find sum we need to iterate through all natural numbers between 1 to n.
Initialize a loop from 1 to N, increment loop counter by 1 for each iteration. The
loop structure should look like for(i=1; i<=N; i++).
4. Inside the loop add previous value of sum with i. Which is sum = sum + i.
5. Finally after loop print the value of sum.
#include <stdio.h>
int main()
{
int i, n, sum=0; /* Input upper limit from user */
printf("Enter upper limit: ");
scanf("%d", &n);
/* Find sum of all numbers */
for(i=1; i<=n; i++) { sum += i; }
printf("Sum of first %d natural numbers = %d", n, sum);
return 0; }
8.Write a C program to perform the addition of two matrices.
Step 1: Start
Step 2: Declare matrix mat1[row][col];
and matrix mat2[row][col];
and matrix sum[row][col]; row= no. of rows, col= no. of columns
Step 3: Read row, col, mat1[][] and mat2[][]
Step 4: Declare variable i=0, j=0
Step 5: Repeat until i < row
5.1: Repeat until j < col
sum[i][j]=mat1[i][j] + mat2[i][j]
Set j=j+1
5.2: Set i=i+1
Step 6: sum is the required matrix after addition
Step 7: Stop

Downloaded by Vanitha Rathinakumar ([email protected])

You might also like