0% found this document useful (0 votes)
24 views75 pages

Unit1 231CSC201T

The document outlines the syllabus for a C programming course, focusing on basic programming constructs. It covers topics such as algorithms, flowcharts, decision-making statements, and looping structures, with examples provided for each concept. The objective is for students to develop simple applications in C using these foundational elements.
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)
24 views75 pages

Unit1 231CSC201T

The document outlines the syllabus for a C programming course, focusing on basic programming constructs. It covers topics such as algorithms, flowcharts, decision-making statements, and looping structures, with examples provided for each concept. The objective is for students to develop simple applications in C using these foundational elements.
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/ 75

231CSC201T - PROGRAMMING IN C

UNIT I

C PROGRAMMING BASICS

Objective: To develop C Programs using basic programming constructs

Text Pg. No.


S.No. Topic Page Number in Notes
Book TB
1 Introduction T1 1 8

2 Algorithm T1 50 2

3 Flow Charts T1 5

4 Pseudo Code T1 3

5 Structure of C program T2 14 12

6 Compilation and linking T2 477


17
processes R2 65

7 Character set T1 2 18

8 Constants T1 4 20

9 Variables T1 3 22

10 Data Types T1 2 19

11 Expressions using operators in C T1 9 30

12 Managing Input and Output


T1 6 25
operations

13 Decision Making and Branching T1 17 44

14 Looping statements T1 22 55

Total Hours 9

Outcome: Students will be able to develop simple applications in C using basic constructs

1
spr_PC_Unit1
Algorithm

2
spr_PC_Unit1
3
spr_PC_Unit1
4
spr_PC_Unit1
5
spr_PC_Unit1
6
spr_PC_Unit1
7
spr_PC_Unit1
8
spr_PC_Unit1
9
spr_PC_Unit1
10
spr_PC_Unit1
11
spr_PC_Unit1
12
spr_PC_Unit1
13
spr_PC_Unit1
14
spr_PC_Unit1
15
spr_PC_Unit1
16
spr_PC_Unit1
17
spr_PC_Unit1
18
spr_PC_Unit1
19
spr_PC_Unit1
20
spr_PC_Unit1
21
spr_PC_Unit1
22
spr_PC_Unit1
23
spr_PC_Unit1
24
spr_PC_Unit1
25
spr_PC_Unit1
26
spr_PC_Unit1
27
spr_PC_Unit1
28
spr_PC_Unit1
29
spr_PC_Unit1
30
spr_PC_Unit1
31
spr_PC_Unit1
32
spr_PC_Unit1
33
spr_PC_Unit1
34
spr_PC_Unit1
35
spr_PC_Unit1
36
spr_PC_Unit1
37
spr_PC_Unit1
38
spr_PC_Unit1
39
spr_PC_Unit1
40
spr_PC_Unit1
41
spr_PC_Unit1
42
spr_PC_Unit1
43
spr_PC_Unit1
DECISION MAKING STATEMENTS

„C‟ language provides the following conditional (decision making) statements.


1. if statement
2. if….else statement
3. nested if….else statement
4. if….else ladder

The if statement:
The if statement is a decision making statement. It is used to control the flow of
execution of the statements and also used to test logically whether the condition is true or
false. It is always used in conjunction with condition. This statement is used when a
question requires answer.

Syntax:
if(condition is true)
{
True Statement;
}
Flowchart:

False
Condition

True

True Statement

44
spr_PC_Unit1
If the condition is true, then the True statements are executed. The „True Statement‟
may be a single statement or group of statements.
If the condition is false then the true statements are not executed, instead the
program skip past it. The condition is given by the relational operator like = =, ! =, < =, >
=. etc.,
Example:
#include<stdio.h>
void main()

{
int i;
printf(“Enter the number less than 10..”);
scanf(“%d”,&i);
if(i<=10)
{
printf(“The entered number is less than 10”);
}
}

Output:
Enter the number less than 10…5
The entered number is less than 10

The if….else statement:


It is basically two way decision making statement and always used in conjunction
with condition. It is used to control the flow of execution and also used to carry out the
logical test and then pick up one of the two possible actions depending on the logical test.
It is used to execute some statements when the condition is true and execute some
other statements, when the condition is false.
Syntax:
if(condition)
{

} }
else Flowchart:
{
45
spr_PC_Unit1
True Statement;
False Statement;

True False
Condition

True Statement False Statement

46
spr_PC_Unit1
Example:
#include<stdio.h>
#include<conio.h>
{
int a;
clrscr();
printf(“Enter the value for a:”);
scanf(“%d”,&a);
if(a%2==0)
{
printf(“The entered number is even”);
}
else
{
printf(“The entered number is odd”);
}
getch();
}
Output:
Enter the value for a:5
The entered number is odd

The nested if….else statement


When a series of if…else statements are occurred in a program, we can write an
entire if…else statement in another if…else statement called nesting, and the statement is
called nested if.
Syntax:
if(condition 1)
{
if(condition 2)
{
True statement1;
}
else
{
False statement 1;
}
47
spr_PC_Unit1
}
else
{
False statement 2;
}
Flowchart:

Conditi False

False Statement 1
True

True False
Conditi

True Statement 2 False Statement 2

48
spr_PC_Unit1
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“Enter the value for a:”);
scanf(“%d”,&a);
if(a==15)
{
printf(“Play Foot Ball”);
}
else
{
if(a==10)
printf(“Play Cricket”);
else
printf(“Don‟t Play”);
}
getch();
}
Output:
Enter the value for a: 10
Play Cricket

The if….else ladder


Syntax:
if(condition 1)
{
Statement 1;
}
else if(condition 2)
{
Statement 2;
49
spr_PC_Unit1
}
else if(condition 3)
{
Statement 3;
}
else
{
Default-statements;
}

50
spr_PC_Unit1
Flowchart:

True False
Condit

True False

Statement 1 Conditi

True False

Statement 2 Condit

Statement 3 Default Statement

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
51
spr_PC_Unit1
printf(“Enter the value for a,b,c:”);
scanf(“%d%d%d”,&a,&b,&c); if(a>b && a>c)
{
printf(“a is big”);
}
else if(b>c)
{
printf(“b is big”);
}
else
{
printf(“c is big”);
}
getch();
}

Output:
Enter the value for a,b,c: 10 20 30
C is big

SWITCH STATEMENT
The switch statement in C language is used to execute the code from
multipleconditions. It is like if else-if ladder statement.

The syntax of switch statement in c language is given below:


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;
52
spr_PC_Unit1
}

Rules for switch statement in C language


1) The switch expression must be of integer or character type.
2) The case value must be integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break
statement found in switch case, all the cases will be executed after matching the case
value. It is known as fall through state of C switch statement.

Flowchart of switch statement in C

53
spr_PC_Unit1
Example:
#include<stdio.h>
void main()

{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2:
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3:
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4:
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
}

OUTPUT:
1. Addition
2.Subtraction
3.Multiplication
4.Division
54
spr_PC_Unit1
Enter the values of a & b: 20 15
Enter your Choice : 1
Sum of 20 and 15 is : 35

LOOPING STATEMENTS
In some situations there is a need to repeat a set of instructions in specified number
of times or until a particular condition is being satisfied. This repetitive operations aredone
through a loop control structure.
The loop in a program consists of two parts, one is body of the loop and another one
is control statement.
Any looping includes the following steps:
i) Initialization of a condition variable.
ii) Test the control statement
iii) Executing the body of the loop depending on the condition.
iv) Updating the condition variable.

The following are the loop structures available in „C‟.


i) while….
ii) do….while
iii) for…..
The while loop:
It is a repetitive control structure, used to execute the statements within the body
until the condition becomes false.
The while loop is an entry controlled loop statement, means the condition is
evaluated first and it is true, then the body of the loop is executed. After executing the body
of the loop, the condition is once again evaluated and if it is true, the body is executed once
again, the process of repeated execution of the body of the loop continues until the
condition finally becomes false and the control is transferred out of the loop.
The body of the loop may have one or more statements, the blocking with the braces
are needed only if the body contains two or more statements.
Syntax:
while(condition)
{
……..
Body of the loop;
……..

55
spr_PC_Unit1
}

Flowchart:

False
Conditio

True

Body of the loop

Example: Find the reverse of a given number.

#include<stdio.h>
void main()
{
int num,rev=0,digit;
printf(“Enter the number:”);
scanf(“%d”,&num);
while(num!=0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
printf(“Reverse is:%d”,rev);
56
spr_PC_Unit1
}
Output:
Enter the number: 7896
Reverse is: 6987
The do….while loop:
In some situations it may be necessary to execute the body of the loop before the
test condition is performed, such a situation the do…while loop is useful.
It is also repetitive control structure and executes the body of the loop once
irrespective of the condition, then it checks the condition and continues the execution until
the condition becomes false.
Syntax:
do
{
…………
Body of the loop;
…………
}while(condition);

Flowchart:

Body of the loop

True Conditio

False

57
spr_PC_Unit1
Example: Addition of numbers upto 10 by using the do…while loop.

#include<stdio.h>
void main()
{
int i=1,sum=0;
do
{
sum=sum+i;
i++;
}
while(i<=10);
printf(“Sum of the numbers upto 10 is…%d”,sum);
}

Output:
Sum of the numbers upto 10 is…55

Comparison between while and do while statement


S.No. While Do…While
1. This is the top tested loop. This is the bottom tested loop.
2. This is an entry controlled loop This is an exit controlled loop
3. The condition is first tested, if It executes the body once, after
the condition is true, then the it checks the condition, if it is
block is executed until the true the body is executed until
condition becomes false. the condition becomes false.
3 Loop will not be executed if Loop is executed at least once
the condition is false. even though the condition is
false.
4. while(condition) do
{ {
body of loop body of loop
} } while (condition);

58
spr_PC_Unit1
5. Eg. Eg.
i=0; i=0;
while( i<5) do
{ {
printf(“%d”,i); printf(“%d”,i);
i++; }while(i<5);
}
The for… loop:

The for loop is another repetitive control structure, and is used to execute set of
instructions repeatedly until the condition becomes false.
The assignment, incrementation or decrementation and condition checking is done
in for statement only, where as other control structures are not offered all these feature in
one statement.
The for loop has three parts
i) Initialize counter is used to initialize counter variable.
ii) Test condition is used to test the condition.
iii) Increment/decrement counter is used to increment or decrement counter variable.

Syntax:
for( initialize counter ; test c ondition ; increment / decrement counter)
Initialization
{
body of the loop
}
Increment/Decrement
Flowchart:

Body of the loop

Conditi True

False 59
spr_PC_Unit1
191GES204T

Example: Addition of numbers upto 10 by using the for loop.

#include
<stdio.h
> void
main()
{
int i,sum=0;
for(i=1;i<=10
;i++)
{
sum=sum+i;
}
printf(“Sum of the numbers upto 10 is…%d”,sum);
}

Output:
Sum of the numbers upto 10 is…55

The nested for loop:


The loop within the loop is called nested loop. In nested for loops two
or more forstatements are included in the body of the loop.
Syntax:
for( initialize counter ; test condition ; increment / decrement counter)
{
for( initialize counter ; test condition ; increment / decrement
counter)
{
body of the loop;
}
}

ITC_PC_SPR Page 60
191GES204T

Basic C Programs to practice:

Example 1: Displaying Hello World

#include <stdio.h> */

int main() printf("Hello World");

{ return 0;

/* printf function displays the content that is }

* passed between the double quotes.

Example 2:

#include <stdio.h> if (num > 0)

printf("%d is a positive number \n", num);

void main() else if (num < 0)

{ printf("%d is a negative number \n",


num);
int num;
else

printf("0 is neither positive nor negative");


printf("Enter a number: \n");
}
scanf("%d", &num);

Example 3:

#include <stdio.h> else

int sum=0,rem; return sum;

int reverse_function(int num){ return sum;

if(num){ }

rem=num%10; int main(){

sum=sum*10+rem; int num,reverse_number;

reverse_function(num/10);

} //Take the number as an input from user

ITC_PC_SPR Page 61
191GES204T

printf("Enter any number:"); reverse_number=reverse_function(num);

scanf("%d",&num); printf("The reverse of entered number is


:%d",reverse_number);

return 0;
//Calling user defined function to perform
reverse }

Example 4:

#include <stdio.h> printf("%lf is the largest number.", num1);

int main() { // if num2 is greater than num1 & num3,


num2 is the largest
double num1, num2, num3;
if (num2 >= num1 && num2 >= num3)
printf("Enter first number: ");
printf("%lf is the largest number.", num2);
scanf("%lf", &num1);

printf("Enter second number: ");


// if num3 is greater than num1 & num2,
scanf("%lf", &num2); num3 is the largest
printf("Enter third number: "); if (num3 >= num1 && num3 >= num2)
scanf("%lf", &num3); printf("%lf is the largest number.", num3);

// if num1 is greater than num2 & num3, return 0;


num1 is the largest
}
if (num1 >= num2 && num1 >= num3)

Example 5:

#include <stdio.h> printf("Enter second number: ");

int main() { scanf("%lf", &num2);

double num1, num2, num3; printf("Enter third number: ");

printf("Enter first number: "); scanf("%lf", &num3);

scanf("%lf", &num1);

ITC_PC_SPR Page 62
191GES204T

if (num1 >= num2 && num1 >= num3) // num3 is the largest number

printf("%lf is the largest number.", num1); else

printf("%lf is the largest number.", num3);

else if (num2 >= num1 && num2 >= num3)

printf("%lf is the largest number.", num2); return 0;

// if both the above conditions are false then

Example 6:

#include <stdio.h> else

int main() { printf("%.2lf is the largest number.",


num3);
double num1, num2, num3;
}

else {
printf("Enter first number: ");
if (num2 >= num3)
scanf("%lf", &num1);
printf("%.2lf is the largest number.",
printf("Enter second number: "); num2);
scanf("%lf", &num2); else
printf("Enter third number: "); printf("%.2lf is the largest number.",
scanf("%lf", &num3); num3);

if (num1 >= num2) {

if (num1 >= num3) return 0;

printf("%.2lf is the largest number.", }


num1);

ITC_PC_SPR Page 63
191GES204T

Example 7:

#include <stdio.h> while (1) {

int main() { if (max % num1 == 0 && max % num2 ==


0) {
int num1, num2, max;
printf("LCM of input numbers %d and %d
printf("Enter two positive integers: "); is %d.", num1, num2, max);
scanf("%d %d", &num1, &num2); break;

}
// greater number between num1 and num2 is
++max;
stored in max
}
max = (num1 > num2) ? num1 : num2;
return 0;

Example 8:

#include <stdio.h> if (num1 % i == 0 && num2 % i == 0)

int main() { gcd = i;

int num1, num2, i, gcd, lcm; }

printf("Enter two positive integers: ");

//storing user input into num1 and num2 //Mathematical formula to calculate LCM
from GCD
scanf("%d %d", &num1, &num2);
lcm = (num1 * num2) / gcd;

for (i = 1; i <= num1 && i <= num2; ++i) {


printf("LCM of two input numbers %d and
%d is: %d.", num1, num2, lcm);
// check if the current value of i is a
return 0;
// factor of both integers num1 and num2
}

ITC_PC_SPR Page 64
191GES204T

Example 9:

#include <stdio.h> printf("%d is the largest


number", *p1);
int main()
}
{
else
int num1, num2, num3;
{
int *p1, *p2, *p3;
printf("%d is the largest
number", *p3);
//taking input from user }
printf("Enter First Number: "); }
scanf("%d",&num1);
else
printf("Enter Second Number: ");
{
scanf("%d",&num2);
if(*p2 > *p3)
printf("Enter Third Number: ");
{
scanf("%d",&num3);
printf("%d is the largest
number", *p2);

//assigning the address of input numbers to }


pointers
else
p1 = &num1; {
p2 = &num2;
printf("%d is the largest
p3 = &num3; number", *p3);

if(*p1 > *p2) }

{ }

if(*p1 > *p3) return 0;

{ }

ITC_PC_SPR Page 65
191GES204T

Example 10:

#include<stdio.h> if ( i <= 1 )

int main() next_term = i;

{ else

int count, first_term = 0, second_term = 1, {


next_term, i;
next_term = first_term + second_term;

first_term = second_term;
//Ask user to input number of terms
second_term = next_term;
printf("Enter the number of terms:\n");
}
scanf("%d",&count);
printf("%d\n",next_term);

}
printf("First %d terms of Fibonacci
series:\n",count);
return 0;
for ( i = 0 ; i < count ; i++ )

{ }

Example 11:

#include<stdio.h> //Displaying factorial of input number

int find_factorial(int); printf("\nfactorial of %d is: %d",num, fact);

int main() return 0;

{ }

int num, fact; int find_factorial(int n)

//Ask user for the input and store it in num {

printf("\nEnter any integer number:"); //Factorial of 0 is 1

scanf("%d",&num); if(n==0)

//Calling our user defined function return(1);

fact =find_factorial(num); //Function calling itself: recursion

return(n*find_factorial(n-1));}

ITC_PC_SPR Page 66
191GES204T

Example 12:

#include <stdio.h> for(i=num1+1; i<num2; ++i)

int main() {

{ flag_var=0;

int num1, num2, flag_var, i, j; for(j=2; j<=i/2; ++j)

/* Ask user to input the from/to range if(i%j==0)

* like 1 to 100, 10 to 1000 etc. {

*/ flag_var=1;

printf("Enter two range(input integer break;


numbers only):");
}
//Store the range in variables using scanf
}
scanf("%d %d", &num1, &num2);
if(flag_var==0)

printf("%d\n",i);
//Display prime numbers for input range
}
printf("Prime numbers from %d and %d
return 0;
are:\n", num1, num2);
}

Example 13:

#include<stdio.h>

int main() /* Value of variable num would change in the

{ below while loop so we are storing it in

int num,copy_of_num,sum=0,rem; another variable to compare the results

at the end of program

//Store input number in variable num */

printf("\nEnter a number:"); copy_of_num = num;

scanf("%d",&num);

ITC_PC_SPR Page 67
191GES204T

/* We are adding cubes of every digit /* If sum of cubes of every digit is equal to
number
* and storing the sum in variable sum
* itself then the number is Armstrong
*/
*/
while (num != 0)
if(copy_of_num == sum)
{
printf("\n%d is an Armstrong
rem = num % 10; Number",copy_of_num);
sum = sum + (rem*rem*rem); else
num = num / 10; printf("\n%d is not an Armstrong
} Number",copy_of_num);

return(0);

Example 14:

#include <stdio.h> temp/=10;

int main() }

{ /* If the original input number (num) is equal


to
int num, reverse_num=0, remainder,temp;
* to its reverse (reverse_num) then its
printf("Enter an integer: "); palindrome
scanf("%d", &num); * else it is not.
/* Here we are generating a new number */
(reverse_num)
if(reverse_num==num)
* by reversing the digits of original input
number */ printf("%d is a palindrome number",num);

temp=num; else

while(temp!=0) printf("%d is not a palindrome


number",num);
{
return 0;
remainder=temp%10;
}
reverse_num=reverse_num*10+remainder;

ITC_PC_SPR Page 68
191GES204T

Example 15:

#include <stdio.h>

int main() /* Using the format specifiers we can get the


ASCII code
{
* of a character. When we use %d format
char ch; specifier for a
printf("Enter any character:"); * char variable then it displays the ASCII
value of a char

/* Reads the entered character and stores it */

* into the char variable ch printf("ASCII value of character %c is: %d",


ch, ch);
*/
return 0;
scanf("%c", &ch);
}

Example 16:

#include<stdio.h> printf("Size of float: %ld


bytes\n",sizeof(float));
int main()
printf("Size of double: %ld bytes",
{ sizeof(double));
printf("Size of char: %ld return 0;
byte\n",sizeof(char));
}
printf("Size of int: %ld bytes\n",sizeof(int));

Example 17:

#include <stdio.h> for(count=1; count <= n; count++)

int main() {

{ sum = sum + count;

int n, count, sum = 0; }

printf("Enter the value of n(positive integer): printf("Sum of first %d natural numbers is:
"); %d",n, sum);

scanf("%d",&n); return 0;}


ITC_PC_SPR Page 69
191GES204T

Example 18:

#include <stdio.h> * for the variable "count"

int main() */

{ count=1;

int n, count, sum = 0; while(count <= n){

sum = sum + count;

printf("Enter the value of n(positive integer): count++;


");
}
scanf("%d",&n);

printf("Sum of first %d natural numbers is:


/* When you use while loop, you have to %d",n, sum);
initialize the

* loop counter variable before the loop and


increment return 0;

}
* or decrement it inside the body of loop
like we did

Example 19:

#include <stdio.h> }

int main() //calculating sum of entered array elements

{ for(int i=0; i<size; i++)

int arr[100],size,sum=0; {

printf("Enter size of the array: "); sum+=arr[i];

scanf("%d",&size); }

printf("Enter the elements of the array: "); printf("Sum of array elements is: %d",sum);

for(int i=0; i<size; i++) return 0;

{ }

scanf("%d",&arr[i]);

ITC_PC_SPR Page 70
191GES204T

Example 20:

#include <stdio.h> printf(" ");

int main() }

{ // This loop prints star in each row

int row = 0,column = 0; for(column=1; column<=row; ++column)

int numberOfRows = 0; {

printf("Enter the number of rows: "); printf("*");

scanf("%u",&numberOfRows); }

for(row=1; row<=numberOfRows; ++row) // Move the cursor to new line for next row

{ printf("\n");

// This loop prints white spaces before first }

// star of each row return 0;

for(column=row; column<numberOfRows; }
++column)

Example 21:

#include <stdio.h> }

int main() { //This is just to print the stars in new line


after each row
int row, column, numberOfRows=8;
printf("\n");
for(row=0; row<numberOfRows; row++)
}
{
return 0;
for(column=0; column<=row; column++)
}
{

printf("* ");

ITC_PC_SPR Page 71
191GES204T

Example 22:

#include <stdio.h> scanf("%d", &number);

int main() { // print the 'number'

int number; printf("Integer entered by user: %d", number);

printf("Enter an integer: "); return 0;

// reads the user input stores in 'number' }

Example 23:

#include <stdio.h> for (i = 1; i <= 10; ++i) {

int main() { printf("%d * %d = %d \n", number, i,


number * i);
int number, i;
}
printf("Enter an integer: ");
return 0;
scanf("%d", &number);
}
printf("Multiplication table of %d: \n",
number);

Example 24:

#include <stdio.h>

int main() avg= (float)(num1+num2)/2;

int num1, num2; //%.2f is used for displaying output upto two
decimal places
float avg;
printf("Average of %d and %d is:
printf("Enter first number: "); %.2f",num1,num2,avg);
scanf("%d",&num1); return 0;
printf("Enter second number: "); }
scanf("%d",&num2);

ITC_PC_SPR Page 72
191GES204T

Example 25:

#include <stdio.h> // Adding two input numbers

int main() { sum = num1 + num2;

int num1, num2, sum; printf("Sum of %d and %d is: %d", num1,


num2, sum);
printf("Enter two integers: ");
return 0;
//Storing user input into variable num1 &
num2 }

scanf("%d %d", &num1, &num2);

Example 26:

#include<stdio.h> // Modulus (%) returns remainder

int main() if ( num%2 == 0 )

{ printf("%d is an even number", num);

// This variable is to store the input number else

int num; printf("%d is an odd number", num);

printf("Enter an integer: "); return 0;

scanf("%d",&num); }

Example 27:

/* Program to check if number is even or odd scanf("%d",&n);

* using bitwise operator */ if ( n & 1)

#include<stdio.h> printf("%d is an odd number", n);

int main() else

{ int n; printf("%d is an even number", n);

printf("Enter an integer: "); return 0; }

ITC_PC_SPR Page 73
191GES204T

Example 28:

#include <stdio.h> printf("Entered character %c is an


alphabet.", ch);
int main() {
else
char ch;
printf("Entered character %c is not an
printf("Enter a character: "); alphabet.", ch);
scanf("%c", &ch); return 0;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch
}
<= 'Z'))

Example 29:

#include <stdio.h> {

int main() isVowel = true;

{ }

char ch; if (isVowel == true)

bool isVowel = false; printf("%c is a Vowel", ch);

else

printf("Enter an alphabet: "); printf("%c is a Consonant", ch);

scanf("%c",&ch); return 0;

}
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch
=='I' ||ch=='o'||ch=='O'||ch=='u'||ch=='U')

Example 30:

#include<stdio.h> char str[25];

#include<string.h> int i;

int main(){ printf("Enter the string:");

ITC_PC_SPR Page 74
191GES204T

scanf("%s",str); }

for(i=0;i<=strlen(str);i++){ printf("\nUpper Case String is: %s",str);

if(str[i]>=97&&str[i]<=122) return 0;

str[i]=str[i]-32; }

Example 31:

#include<stdio.h> printf("Enter the string: ");

#include<string.h> scanf("%s",str);

int main(){

/* This array can hold a string of upto 25 for(i=0;i<=strlen(str);i++){

* chars, if you are going to enter larger if(str[i]>=65&&str[i]<=90)


string
str[i]=str[i]+32;
* then increase the array size accordingly
}
*/
printf("\nLower Case String is: %s",str);
char str[25];
return 0;
int i;
}

ITC_PC_SPR Page 75

You might also like