Copy-C Language TUTION Copy 1
Copy-C Language TUTION Copy 1
Character Constants:
Character literals are enclosed in single quotes, e.g., 'x'
can be stored in a simple variable of char type.
Example:
'A'
'c'
String Literals:
String literals or constants are enclosed in double quotes "".
A string contains characters that are similar to character
literals: plain characters, escape sequences, and universal
characters.
Example:
"Hello"
"ram" etc.
~A = 1100 0011
5.Assignment Operators
6.Misc Operators ↦ sizeof & ternary
EXPRESSIONS
An expression is a formula in which operands are linked
to each other by the use of operators to compute a
value. An operand can be a function reference, a
variable, an array element or a constant.
Let's see an example:
a-b;
In the above expression, minus character (-) is an
operator, and a, and b are the two operands.
Arithmetic Expressions
An arithmetic expression is an expression that
consists of operands and arithmetic operators. An
arithmetic expression computes a value of type int,
float or double.
E.g: 6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)
Relational Expressions
A relational expression is an expression used to compare two operands.
It is a condition which is used to decide whether the action should be taken or
not.
E.g: a>=9 , x%2 = = 0
#include <stdio.h>
int main()
{
int x=4;
if(x%2==0)
{
printf("The number x is even");
}
else
printf("The number x is not even");
return 0;
}
Logical Expressions
A logical expression is an expression that computes either a zero or
non-zero value.
It is a complex test condition to take a decision.
#include <stdio.h>
int main()
{
int x = 4;
int y = 10;
if ( (x <10) && (y>5))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Conditional Expressions
A conditional expression is an expression that returns 1 if the condition is true otherwise 0.
A conditional operator is also known as a ternary operator.
The Syntax of Conditional operator:
exp1 ? exp2 : exp3
#include<stdio.h>
#include<string.h>
int main()
{
int age = 25;
char status;
status = (age>22) ? 'M': 'U';
if(status == 'M')
printf("Married");
else
printf("Unmarried");
return 0;
}
C - Header Files
A header file is a file with extension .h which contains C
function declarations and macro definitions to be shared
between several source files. There are two types of header
files: the files that the programmer writes and the files that
comes with your compiler.
You request to use a header file in your program by including
it with the C preprocessing directive #include, like you have
seen inclusion of stdio.h header file, which comes along with
your compiler.
SYNTAX:
#include <file> :This form is used for system header files.
#include "file" :This form is used for header files of your own
program. It searches for a file named 'file' in the directory
containing the current file.
Library Files
Library is the place where the actual functionality is
implemented i.e. they contain function body.
Example : Math.h is a header file which includes the
prototype for function calls like sqrt(), pow() etc,
whereas libm.lib, libmmd.lib, libmmd.dll are some of
the math libraries. In simple terms a header file is like a
visiting card and libraries are like a real person, so we
use visiting card(Header file) to reach to the actual
person(Library).
PreProcessor Directives
Preprocessors are programs that process our source code before
compilation.Preprocessor programs provide preprocessors
directives which tell the compiler to preprocess the source code
before compiling. All of these preprocessor directives begin with a
‘#’ (hash) symbol. The ‘#’ symbol indicates that, whatever
statement starts with #, is going to the preprocessor program, and
preprocessor program will execute this statement.
#define
Substitutes a preprocessor macro.A macro is a code snippet that
is replaced by some value of the code of macro. Any macro is
mostly described and defined by its #define directive.
#include
Inserts a particular header from another file.The #include
preprocessor directive is used to paste code of given file into
current file. It is used include system-defined and user-defined
header files. If included file is not found, compiler renders error.
Preprocessors Examples:
#define MAX_ARRAY_LENGTH 20: This directive tells the CPP to
replace instances of MAX_ARRAY_LENGTH with 20.
#include <stdio.h>:These directives tell the CPP to get
stdio.h from System Libraries and add the text to the
current source file.
EXAMPLE:
#include <stdio.h>
#include <conio.h>
#define PI 3.1415
Void main()
{
printf("%f",PI);
getch();
}
EXAMPLE:WAP IN C TO DEFINE A MACRO Area() that will compute
the area of rectangle?
Ans:
#include <stdio.h>
// macro with parameter
#define AREA(l, b) (l * b)
int main()
{
int l1 = 10, l2 = 5, area;
return 0;
}
COMMENT IN C LANGUAGE
A comment starts with a slash asterisk /* and ends with a
asterisk slash */ and can be anywhere in your program.
Comments can span several lines within your C program.
Comments are typically added directly above the related C
source code.
SYNTAX:
The syntax for a comment is:
/* comment goes here */
OR
/*
* comment goes here
*/
Q: WAP IN C TO CHECK TO ENTER A NUMBER AND CHECK WETHER THE NO IS POPOSITIVE OR NEGATIVE NO?
ANS:
if (i > 15)
{
printf("10 is less than 15");
}
getch();
}
nested-if statement in C
A nested if in C is an if statement that is the target of another if
statement. Nested if statements means an if statement inside another
if statement. Yes, both C and C++ allows us to nested if statements
within if statements, i.e, we can place an if statement inside another if
statement.
Syntax:
if (condition1)
{
/*Executes when condition1 is true*/
if (condition2)
{
/* Executes when condition2 is true*/
}
}
Q: C program to illustrate nested-if statement
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i = 10;
if (i == 10)
{
if (i < 15)
printf("i is smaller than 15\n");
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
getch();
}
if-else-if ladder in C
Here, a user can decide among multiple options. The C if statements are
executed from the top down. As soon as one of the conditions controlling
the if is true, the statement associated with that if is executed, and the
rest of the C else-if ladder is bypassed. If none of the conditions are true,
then the final else statement will be executed.
Syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Q: C program to illustrate nested-if statement?
#include <stdio.h>
#include<conio.h>
void main() {
clrscr();
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
getch();
}
SWITCH STATEMENT
Switch statement in C tests the value of a variable and
compares it with multiple cases. Once the case match is
found, a block of statements associated with that particular
case is executed.
Each case in a block of a switch has a different
name/number which is referred to as an identifier. The
value provided by the user is compared with all the cases
inside the switch block until the match is found.
If a case match is NOT found, then the default statement is
executed, and the control goes out of the switch block.
Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
#include<stdio.h>
#include<conio.h>
int main(){
clrscr();
Int number=100;
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
getch();
}
JUMP STATEMENTS
break statement
The break statement is used to terminate the loop or statement in
which it present. That is it terminates the loop or switch statement
and transfers execution to the statement immediately following the
loop or switch.
continue statement
The continue statement is also used inside loop. When compiler finds
the continue statement inside a loop, compiler will skip all the
followling statements in the loop and resume the loop.
Goto Statement
The goto statement is a jump statement which jumps from one point
to another point within a function.
Example of goto statement
#include<stdio.h>
#include<conio.h>
void main()
{
Clrscr();
printf("\nStatement 1.");
printf("\nStatement 2.");
printf("\nStatement 3.");
goto last;
printf("\nStatement 4.");
printf("\nStatement 5.");
last:
printf("\nEnd of Program.");
getch();
}
return Statement
The return statement returns the flow of the execution to the
function from where it is called. This statement does not
mandatorily need any conditional statements. As soon as the
statement is executed, the flow of the program stops immediately
and return the control from where it was called. The return
statement may or may not return anything for a void function, but
for a non-void function, a return value is must be returned.
Example of return statement:
#include <stdio.h>
int SUM(int a, int b)
{
int s1 = a + b;
return s1;
}
int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
printf("The sum is %d", sum_of);
return 0;
}
Loops in C
• A Loop executes the sequence of statements many times until the
stated condition becomes false. A loop consists of two parts, a
body of a loop and a control statement. The control statement is a
combination of some conditions that direct the body of the loop
to execute until the specified condition becomes false. The
purpose of the loop is to repeat the same code a number of times.
• Types of Loops in C:
• 1. Entry controlled loop
do {
statements
} while (expression);
Q:Wap in C to print the table of 2?
Ans:
#include<stdio.h>
#include<conio.h>
int main()
{ Clrscr();
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
For loop
The initial value of the for loop is performed only
once.
The condition is a Boolean expression that tests and
compares the counter to a fixed value after each
iteration, stopping the for loop when false is
returned.
The incrementation/decrementation increases (or
decreases) the counter by a set value.
SYNTAX:
for (initial value; condition; incrementation or
decrementation )
{
statements;
}
Q: WAP in C to find the factorial of a number ?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
getch();
}
Q: WAP in C to check wether the entered chracter is Vowel or consonant ?
Soln:
include <stdio.h>
#include<conio.h>
int main()
clrscr();
char c;
scanf("%c", &c);
if (lowercase_vowel || uppercase_vowel)
else
getch();
}
Q:WAP IN C TO FIND THE LARGEST NUMBER AMONG 3 NUMBERS ?
Soln:
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
getch();
}
Q:WAP in C to check wether a year is leap year or not ?
#include <stdio.h>
int main() {
int year;
scanf("%d", &year);
if (year % 400 == 0) {
// but divisible by 4
else if (year % 4 == 0) {
else {
}
Nesting of for loop
Q:wap in c to print the following pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Ans:
# include<stdio.h>
#include<conio.h>
Void main()
{
clrscr();
for (int i=1; i<=5; i++)
{
for (int j=1; j<=i; j++)
{
printf(j + " ");
}
printf(“\n”);
}
getch();
}
Example of continue statement
Q:WAP in C to print value from 1 to 10 except 6 ?
Ans:
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
for (int i = 1; i <= 10; i++)
{
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
}
return 0;
}
Q:WAP in C to print the following pattern:
*
**
***
****
*****
Ans:
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
getch();
}
Q:WAP in C to print the following pattern:
12345
1234
123
12
1
Soln:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Q: WAP in C to check wther the enterdnnumber is Prime number or not ?
Soln:A prime number is a positive integer that is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11, 13, 17 etc.
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Q:WAP in C to check wether a no is PALINDROME no or not?
Ans:An integer is a palindrome if the reverse of that number is equal to the original number
#include <stdio.h>
int main() {
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
while (n != 0) {
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n = n/10;
}
if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);
return 0;
}
Q: WAP in C to find the factorial of a no?
Ans:
#include<stdio.h>
#include<conio.h>
Void main()
{
clrscr();
int num;
int fact=1;
Printf(“ Enter the number \n”);
Scanf(“%d”, &num);
for(int i=num; i>0 ; i- -)
{
fact= fact*i;
}
Printf(“Factorial of %d = %d”, num,fact);
getch();
}
Q: WAP IN C TO CHECK WETHER A NO IS ARMSTRONG NO NOT ?
Ans:A no is armstrong no if the sum of cubes of each digit is equal to the number itself. For
example, 153 is an Armstrong number.
#include <stdio.h>
int main()
{
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
/*remainder contains the last digit*/
remainder = originalNum % 10;
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Q:WAP in C to print FIBONACCI Series ?
Ans:The Fibonacci sequence is a sequence where the next term is the sum of the
previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2); /*printing 0 and 1*/
for(i=2;i<number;++i) /*loop starts from 2 because 0 and 1 are already printed*/
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}