Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

MODULE2L Class2 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

CPC

MODULE 2
 What is the difference between assignment and equality operators? (3)
 What is a static variable? When should it be used? (3)
 What is the importance of precedence and associativity? Write the table for operator
precedence.(3)
 Differentiate between ‘break’ and ‘continue’ statements. (3)
 Explain arithmetic, logical and bitwise operators with examples. (7)
 Write a C Program to check if a given number is a strong number or not. A
 strong number is a number in which the sum of the factorial of the digits is equal to the
number itself.(7)
Eg:- 145=1!+4!+5!=1+24+120=145(7)
 14 a) Write C program to convert the given decimal number into binary number. (7)
 What do you mean by Formatted Input? Explain in detail the prototype of
 ‘scanf()’ function in C including its argument list and return type.(7)
 Explain different data types supported by C language with their memoryrequirements.(7)
 Write a C program to check if a number is present in a given list of numbers. If
present, give location of the number otherwise insert the number in the list atthe end.
 Write a C program to find the sum of first and last digit of a number. (7)
 What is type casting? Name the inbuilt typecasting functions available in C Language.
What is the difference between type casting and type conversion?
 Differentiate between while and do-while loops using an example. (3)
 Why is the use of goto statement discouraged in C programs? (3)
 Explain different data types supported by C language with their memoryrequirements.(7)
 Write down a C program to check if a number is present in a given list of numbers. If
present give location of the number otherwise insert the number inthe list at the end.
 Explain formatted and Unformatted I/O functions of C language with syntax and example.
(7)
 Write a C program to read a character from the user and check whether it is a vowel or
consonant.
Assignment and equality
operators

The '=' is the so-called assignment operator and is used to assign the result of the expression on
the right side of the operator to the variable on the left side. The '==' is the so-called equality
comparison operator and is used to check whether the two expressions on both sides are equal
or not.

STATIC VARIABLES

Static variables have the property of preserving their value even after they are out of their scope! Hence, a
static variable preserves its previous value in its previous scope and is not initialized again in the new scope.
Syntax:static data_type var_name = var_value;Following are some interesting facts about static variables in
C:1) A static int variable remains in memory while the program is running. A normal or auto variable is
destroyed when a function call where the variable was declared is over. For example, we can use static int to
count the number of times a function is called, but an auto variable can’t be used for this purpose.
PRECEDENCE AND ORDER OF EVALUATION
OPERATOR PRECEDENCE AND ASSOCIATIVITY
The precedence is used to determine how an expression involving more than
one operator is evaluated. There are distinct levels of precedence and an operator may
belong to
one of these levels. The operators at the higher level of precedence are evaluated first.
The
operators of the same precedence are evaluated either from „left to right‟ or „right to
left‟,
depending on the level. This is known as the associativity property of an operator.
The precedence rules decides the order in which different operators are applied.
The table below
Associativity ruleprovides
decides athe
complete
order inlist of operators,
which their precedence
multiple occurrences of the same level
levels,
operatorand their
are rules of association. The groups are listed in the order of decreasing
applied.
precedence.
Rank 1 indicates the highest precedence level and 15 the lowest.
SWITCH
STATEMENTS

switch ( expression )
{
case value-1:
statement-1;
break;
case value-2:
statement-2;
break;
.........
.........
case value-n:
statement-n;
break;
default :
default statement;
break;
}
statement-x;
GOTO

goto skip_point;
printf ("This part was skipped.\n");
skip_point: /* skip_point is a label*/
printf("Hi there!\n");
THE while STATEMENT
The while is an entry-controlled loop statement. The test-condition is evaluated and
if the condition is true, then the body of the loop is executed. This process of repeated execution
of the body continues until the test-condition finally becomes false and the control is transferred
out of the loop. On exit, the program continues with the statement immediately after the body of
the loop.

The basic format of the while statement is :


While(condition)
{
Body of the loop
}
Example
main()
{ int n,sum;
n=1; /*initialization*/
sum=0;
whie( n<=5) /* testing*/
{ sum=sum+n;
n=n+1; /*incrementing*/
} printf(“sum =%d”, sum);
break AND continue
When a break statement is encountered inside a loop, the loop is immediately exited
and the program continues with the statement immediately following the loop. When
the loops
are nested, the break would only exit from the loop containing it. That is, the break will
exit only
a single loop.
General form of break is:
for(--------)
{
----------
-----------
if(error)
break;
-------
--------
}
-----------
Example

for(loop=0; loop<50; loop++)


{
if(loop==10)
break;
printf("%i\n",loop);
}
output
Only numbers 0 through 9 are printed.
The continue causes the loop to be continued with the next iteration after skipping
any statements in between. In while and do loops, continue causes the control to go directly to
the test-condition and then to continue the iteration process. In the case of for loop, the increment
section of the loop is executed before the test-condition is evaluated.
General form of continue is:

for(--------)
{
----------
-----------
if(---------)
continue;
-------
--------
}
-----------
Examples:
for (loop=0;loop<100;loop++)
{
if (loop==50)
continue;
printf ("%i\n",loop);
}
The numbers 0 through 99 are printed except for 50.
DECIMAL TO BINARY
1.#include<stdio.h>
2.#include<stdlib.h>
3.int main(){
4.int a[10],n,i;
5.system ("cls");
6.printf("Enter the number to convert: ");
7.scanf("%d",&n);
8.for(i=0;n>0;i++)
9.{
10.a[i]=n%2;
11.n=n/2;
12.}
13.printf("\nBinary of Given Number is=");
14.for(i=i-1;i>=0;i--)
15.{
16.printf("%d",a[i]);
17.}
18.return 0;
19.}
Enter the number to convert: 5
Binary of Given Number is=101
FORMATTED I/O AND
UNFORMATTED I/0
The I/O procedure is known as "formatted I/O". It enables you to read or write data in a
certain format. Printf() and scanf() are two examples of C routines that handle formatted
I/O. The type and format of the data to be read or written are specified by format strings,
which are used by these operations. The program's execution replaces the placeholders for
the data found in the format strings with the actual data.
Syntax:
Formatted output syntax for the printf() function:
1.printf(format string, argument list);
Unformatted I/O in C
Unformatted I/O refers to a category of I/O operations that reads or writes data as a
stream of bytes without regard to any format. Unformatted I/O in C is carried out with the
aid of functions like fread() and fwrite(). Without formatting, these operations are used
to read and write data directly to and from files.
Syntax:
Syntax for using the fwrite() function to print unformatted data:
1.fwrite(data, size, count, file pointer);
Here, count is the number of elements to be written, size is the size of each element to
be written, and the file pointer is a pointer to the file where the data will be written.
Syntax for using the fread() method with unformatted input:
fread(data, size, count, file pointer);
In this syntax, a pointer to the buffer where the data will be read, the size of each
element to be read, the number of elements to be read, and a pointer to the file from
which the data will be read
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 byte −32,768 to 32,767
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to
2,147,483,647
signed long int 4 byte -2,147,483,648 to
2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
#include <stdio.h>
#define MAX_SIZE 100
int main()
{ int numbers[MAX_SIZE];
int n, i, num, found = 0;
// Input the number of elements in the list
printf("Enter the number of elements in the list:
"); scanf("%d", &n);
// Input the list of numbers
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++)
{ scanf("%d", &numbers[i]);
}
// Input the number to be checked or inserted
printf("Enter the number to be checked or
// Check if the number is present in the list
for (i = 0; i < n; i++)
{ if (numbers[i] == num)
{
printf("Number %d found at index %d.\n", num, i);
found = 1; break; } }
// If the number is not found, insert it at the end of the list
if (!found)
{ printf("Number %d not found. Inserting at the end.\n", num);
numbers[n] = num; n++;
// Increase the size of the list } //
Print the updated list
printf("Updated list:\n");
for (i = 0; i < n; i++)
{ printf("%d ", numbers[i]); }
printf("\n");
0 1 2 3 4
1 2 3 4 5

FOR(I=0;I<N;I++)
NUMBER[I]==NUM
NUM=5
/**
* C program to find sum of first and last digit of a number
*/

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

int main()
{
int num, sum, digits, firstDigit, lastDigit;

sum = 0;

/* Input a number from user */


printf("Enter any number to find sum of first and last digit: ");
scanf("%d", &num);

/* Find last digit */


lastDigit = num % 10;

/* Find total number of digits - 1 */


digits = (int) log10(num);

/* Find first digit */


firstDigit = (int) (num / pow(10, digits));

/* Calculate the sum */


sum = firstDigit + lastDigit;

printf("Sum of first and last digit = %d", sum);

return 0;
}
Data types are crucial in determining the nature and behavior of variables in the
realm of programming. Typecasting allows us to convert one data type into another.
This procedure is called type casting, and the C programming language offers it as
a useful tool. In this blog, we will examine the syntax, practical applications, and
advantages of typecasting in C.
The procedure of changing a variable's data type is known as type casting. It can
be helpful in a variety of situations, for as when processing user input,
doing mathematical calculations, or interacting with other libraries that need
data types. In C language, we use cast operator for typecasting which is denoted by
(type).
Syntax:
1.(type)value;

Note: It is always recommended to convert the lower value to higher to avoid data loss.

Without Type Casting:


int f= 9/4;
Type conversion in C is the process of converting one data type to another. The
type conversion is only performed to those data types where conversion is
possible. Type conversion is performed by a compiler. In type conversion, the
destination data type can’t be smaller than the source data type. Type
conversion is done at compile time and it is also called widening conversion
because the destination data type can’t be smaller than the source data
type. There are two types of
2^0=1
2^1=2
2^2=4
2^3=8
101=2^2+0+2^0=4+0+1=5
111=2^2+2^1+2^0=4+2+1=7
The C goto statement is a jump statement which is sometimes
also referred to as an unconditional jump statement. The goto
statement can be used to jump from anywhere to anywhere
within a function.
// C program to check if a number is
// even or not using goto statement
#include <stdio.h>

// function to check even or not


void checkEvenOrNot(int num)
{
if (num % 2 == 0)
// jump to even
goto even;
else
// jump to odd
goto odd;

even:
printf("%d is even", num);
// return if even
return;
odd:
printf("%d is odd", num);
}

int main()
{
int num = 26;
checkEvenOrNot(num);
return 0;
}

You might also like