You are on page 1of 74

Vyankatesha Computech C Notes

 What is C:- C is programming language program:-C program is list of instruction given to the
computer which perform a specific tab. C is very popular because it has rich set of readymade function
Header files etc. so it is very easy to handle.
C was develop in 1972 at USA at AT & T bells laboratory by Dennis Richie.
Header file:- The files which are inbuilt (provider by C). This files contains ready made function & it
has an extension. h
Eg. (stdio.h), (conio.h) (string.h)etc.
 Keyword:- The words which are automatically understood by compiler are called keywords.
There are total 32 keywords presented in ‘C’.
Eg. Int, flot, struct, etc.
a programming language, C is rather like Pascal or Fortran. Values are stored in
variables. Programs are structured by defining and calling functions. Program flow is
controlled using loops, if statements and function calls. Input and output can be directed
to the terminal or to files. Related data can be stored together in arrays or structures.
Of the three languages, C allows the most precise control of input and output. C is also rather more terse than
Fortran or Pascal. This can result in short efficient programs, where the programmer has made wise use of C's
range of powerful operators. It also allows the programmer to produce programs which are impossible to
understand.

Programmers who are familiar with the use of pointers (or indirect addressing, to use the correct term) will
welcome the ease of use compared with some other languages. Undisciplined use of pointers can lead to
errors which are very hard to trace. This course only deals with the simplest applications of pointers.

It is hoped that newcomers will find C a useful and friendly language. Care must be taken in using C. Many of
the extra facilities which it offers can lead to extra types of programming error. You will have to learn to deal
with these to successfully make the transition to being a C programmer.

A Very Simple Program


This program which will print out the message This is a C program

#include <stdio.h>

main()
{
printf("This is a C program\n");
}
Though the program is very simple, a few points are worthy of note.

Every C program contains a function called main. This is the start point of the program.

#include <stdio.h> allows the program to interact with the screen, keyboard and filesystem of your computer.
You will find it at the beginning of almost every C program.

-1-
Vyankatesha Computech C Notes
main() declares the start of the function, while the two curly brackets show the start and finish of the function.
Curly brackets in C are used to group statements together as in a function, or in the body of a loop. Such a
grouping is known as a compound statement or a block.

printf(“This is c program \n”);

prints the words on the screen. The text to be printed is enclosed in double quotes. The \n at the end of the
text tells the program to print a new line as part of the output.

Most C programs are in lower case letters. You will usually find upper case letters used in preprocessor
definitions (which will be discussed later) or inside quotes as parts of character strings. C is case sensitive, that
is, it recognizes a lower case letter and it's upper case equivalent as being different.

While useful for teaching, such a simple program has few practical uses. Let us consider something rather
more practical. The following program will print a conversion table for weight in pounds (U.S.A. Measurement)
to pounds and stones (Imperial Measurement) or Kilograms (International).

Variable Names
Every variable has a name and a value. The name identifies the variable, the value stores data. There is a
limitation on what these names can be. Every variable name in C must start with a letter, the rest of the name
can consist of letters, numbers and underscore characters. C recognizes upper and lower case characters as
being different. Finally, you cannot use any of C's keywords like main, while, switch etc as variable names.

Examples of legal variable names include

x result outfile bestyet


x1 x2 out_file best_yet
power impetus gamma hi_score
It is conventional to avoid the use of capital letters in variable names. These are used for names of constants.
Some old implementations of C only use the first 8 characters of a variable name. Most modern ones don't
apply this limit though.

The rules governing variable names also apply to the names of functions. We shall meet functions later on in
the course.

Expressions and Operators


One reason for the power of C is its wide range of useful operators. An operator is a function which is applied
to values to give a result. You should be familiar with operators such as +, -, /.

-2-
Vyankatesha Computech C Notes
Arithmetic operators are the most common. Other operators are used for comparison of values, combination
of logical states, and manipulation of individual binary digits. The binary operators are rather low level for so
are not covered here.

Operators and values are combined to form expressions. The values produced by these expressions can be
stored in variables, or used as a part of even larger expressions.

Escape Sequence

1. \n - next line
2. \t - tab spaces
3. \a - alert or alarm
4. \’ - single quote
5. \” - double quote
6. \\ - back slash
7. \r - carriage return
8. \b - back space

\n \t:-
1. printf("Hello \n Hello \t Hello”);
Output :- Hello
Hello Hello
2. printf (“\’Hello\’”);
Output :-‘Hello’
3. printf (“\”Hello\””);
Output:-“Hello”
4. printf (“\n next \t ext”);
Output:- next text
5. printf (“\\next \\text”);
Output:- \next \next
6. printf (“welcome\r”);
Output:- welcome
cursor
7. printf (“welcome \b\b”);
Output:- welcome
Backspace
 Scanf() :-
Input statement used for reading the values of variable form the user

 Syntax:-
scanf (“<format specifier>”,<address of variable list>);

Example :-
scanf(“<%d %d”, &a,&b);

Considering a&b as integer.

1. Addition of two no.

-3-
Vyankatesha Computech C Notes
/*program for addition of two no*/
#include <stdio.h>
main()
{
int a,b,c;/*declaration part*/
clrscr(); /*clear the screen*/
printf (“\n Enter two values”);
scanf (“%d%d”,&a,&b);
/* Reading values of a&b*/
c=a+b;
printf (“\n Addition of %d & %d is %d”,a,b,c);
/*Printing the result */
getch(); /* wait for key press*/
}

math.h -> Math function file

1. /* finding area of parameter of a given circle*/


# include <stdio.b>
# include <math.h> /*for power */
main()
{
float r,a,p;
clrscr ();
printf (“\n enter radius”);
scanf (“%f”, &r);
a= 3.14 * powr(r,2);
p= 2*3.14*r;
printf (“\n area: %f”, a);
printf (“\n parameter : %f ”, p);
getch ();
}

2. /* displaying all arithmetic operation on two values*/


# include <stdio.h>
main()
{
int a,b;
clrscr ();
printf (“\n enter two no:”);
scanf (“%d %d”,&a ,&b);
printf (“\n addition : %d”, a+b);
printf (“\n sub : %d”, a-b);
printf (“\n muli : %d”, a*b);
printf (“\n division : %d”, a/b);
printf (“\n reminder : %d”, a%b);
getch();
}
Types of Operators

-4-
Vyankatesha Computech C Notes
1. Arithmetic Operations-
a. Unary Operators:-
I. increment Operator (++)
int a= 10;
a++ ; /* a= a+1 */-postfix
++a; /* a= a+1*/-prefix
II. decrement Operators-
a-- ; /* a= a-1 */-postfix
--a; /* a= a-1*/-prefix
b. Binary:-
I. Addition +
II. Subtraction -
III. Multiplication *
IV. Division /
V. Remainder %
1. Arithmetic assignment or operator-
i. +=
a+=b; /* a=a+b */
ii. -=
a-=b; /* a=a-b */
iii. *=
a*=b; /* a=a*b */

iv. /=
a/=b; /* a=a/b */

v. %=
a%=b; /* a=a%b */

2. Comparison (conditional) operator


i. < less than
ii. > greater than
iii. <= less than equal to
iv. >= greater than equal to
v. != not equal
vi. == equal

3. Logical Operation/ Operator-

i. && - And operator.


a. TT -T
b. TF -F
c. FT -F
d. FF -F

-5-
Vyankatesha Computech C Notes

ii. OR Operator.
I I
I I
T T - T
T F - T
F T - T
F F - F

iii. ! not Operator.

T - F
F - T

a. Postfix operator:

Int a=10 , b;
b=a++;
b=10 a=11

b. Prefix of operation:-
Int a=10,b;
b=++a;
b=11 a=11

3. /* Example in postfix & prefix operator */


# include <stdio.h>
main ()
{
int a=10, b=20,c,d;
clrscr ();
printf (“\n before : a=%d,b=%d”,a,b);
printf (“\n postfix operation a++: %d ”, a++);
printf (“\n prefix operation =-b:%d”, --b);
printf (“\n after: a=%d, b=%d”, a,b);
c=a++ + ++b;
d=--a +b--;
printf (“\n a = %d, b = %d, c =%d, d =%d ”, a,b,c,d);
getch;
}

Output :-
Before a=10 b=20
Postfix operation a++ : 10
Prefix operation --b : 19
After :
a=11 b=19
A=11 b=19 c=31 d=31

-6-
Vyankatesha Computech C Notes
F9 – for compile/ Alt+c
For run program- ctrl+F9

Extension is C file .C

If statement (conditional statement):-

This statement is use for checking a condition & executing some set of statements if that
condition is true.

Syntax:-
if (condition)
{
Statement 1;
Statement 2;


}

 Flow chart:-

If statement :-

False

If condition

True

State-1
State-2
State-3

4. /* Finding greatest among two no*/

# include <stdio.h>
main()
{
int a,b;
clrscr();
printf (“\n Enter two no.”);

-7-
Vyankatesha Computech C Notes
scanf (“%d %d”, & a,&b);
If (a>b)
printf (“\n a = %d is greater”, a);
if (b>a)
printf (“\n b=%d is grater”, b);
getch ();
}
5. /* Discount */
# include <stdio.h>
main()
{
int a, c, b;
clrscr ();
printf (“\n Enter a amount”);
scanf (“%d” &a);
if (a>=500);
{
c=(a*10)/100;
b=a-c;
printf (“\n amount = %d”, b);
}
printf (“\n amount = %d”, a);
}
OR:-

/* 10% discount if amount is grater than 500*/


# include < stdio.h >
main()
int a, b, c;
clrscr ();
printf (“\n Enter a amount”);
scanf (“%d” &a);
If (a>500);
{
c=(a*10)/100;
b=a-c;
printf (“\n amount = %d”, b);
}
else
printf (“\n amount = %d”, a);
getch ();
}
If- Else Statement:-

In If – Else statement if the condition is true it will execute the statement in between if
and else that is true block if the condition is false it will execute the part after else that is
false block
Syntax:-

-8-
Vyankatesha Computech C Notes
if (condition)
{
Statement 1
Statement 2 True block
Statement 3
Statement 4
}
else
}
Statement1 False block
Statement2
}

is
condition

True

Stat 1
false block
true stat2 stat 1
block state3 stat 2

6. /*checking greatest among two no.*/


#include <stdio.h>
main ()
{
Int a, b;
clrscr ();
printf(“\n enter two no.”);
scanf (“\n %d%d”,&a,&b);
If (a>b)
printf(“\n a is greater %d”,a);
else
printf(“\n b is greater %d”,b);
getch ();

7. /* checking a given character for an Upper Case letter*/

#include <stdio.h>
main ()
{
char ch;
clrscr ();

-9-
Vyankatesha Computech C Notes
printf(“\n enter the character.”);
scanf (“%c”, &ch);/* reading a character*/
/* check a character with in A to Z */
If (*ch >=’A’& ch <=’Z’)
printf(“\n Upper Case letter”);
else
printf(“\n not Upper letter”);
getch ();
}

8. /*checking a given no. even or odd*/

# include <stdio.h>
main ()
{
int n;
clrscr ();
printf(“\n enter the no.”);
scanf (“%d”, &n”);
If ((n%2)==0)
printf (“\n even no.”);
else
printf(“\n odd no.”);
getch();
}

9. /* checking a given no. Positive or Negative*/

#include <stdio.h>
main ()
{
int n;
clrscr ();
printf(“\n enter the no.”);
scanf (“\n %d”, &n”);
if (n<0)
printf (“\n Negative”);
else
printf(“\n Positive”);
getch();
}

10. /* Display even no. & divisible by 3*/

#include <stdio.h>
main ()
{
Int n;
clrscr ();

- 10 -
Vyankatesha Computech C Notes
printf(“\n enter the no.”);
scanf (“\n%d”, &n”);
If ((n%2 ==0 && n%3==0)
printf (“\n even & divisible by 3”);
OR
printf(“\n the no. is %d”,n);
getch();
}

11. /* Display even no. & divisible by 3 0r 5*/


#include <stdio.h>
main ()
{
int n;
clrscr ();
printf(“\n enter the no.”);
scanf (“\n %d”, &n”);
If ((n%2 ==0)&&(n%3==0 || n%5==0))
printf (“\n no is even & divisible by 5 or 3”);
getch();
}

12. /* checking a given character for a special character*/

#include <stdio.h>
main ()
{
char ch;
clrscr ();
printf (“\n Enter a character”);
scanf (“%c”,&ch);
If((!ch>=’A’&& ch<’Z’)&& !( ch>=’a’ &&ch<’z’)&& !(ch>=’0’ & & ch<’9’))
printf (“\n special character”);
else
printf (“\n not special character”);
getch();
}
OR
If ((ch>=’A’ & & ch<=’Z’) || (ch>=’a’ & & ch<=’z’) || (ch>=’0’ & & ch<=’9’))
printf (“\n not special character ”);
else
printf (“\n special character”);

 Nested If:- Else(If – Else ladder)


If else within if else is called ladder or nested if-else.

13. /* Finding largest no. among there no. using nested if-else*/
#include <stdio.h>
main ()
{

- 11 -
Vyankatesha Computech C Notes
int a,b,c;
clrscr ();
printf (“\n enter three value”)
scanf (“%d %d%d”, &a,&b,&c);
if (a>b)
{
if (a>c)
printf (“\n a=%d is grater”, a);
else
printf (“\n c=%d is grater”, c);
}
else
{
if (b>c)
printf (“\n b=%d is greater”, b);
else
printf (“\n c=%d is greater”, c);
}
getch ();
}

14. /* Printing a category of given character */

#include <stdio.h>
main ()
{
char ch.
clrscr ();
printf (“\n Enter a character”);
scanf (“%c”, &ch); /*Reading a character*/
/* chek for Upper case letter*/
if (ch>=’A’&& ch<=’Z’)
printf (“\n Upper case letter”);
else
{
/* check for lower case letter*/
if (ch>=’a’&& ch<=’z’)
printf (“\n lower case letter”);
else
{
/*check for a digit */
if (ch>=’0’&& ch <=’9’)
printf (“\n digit”);
else
printf (“\n special character”);
}
}
getch ();
}
15. /* Reading percentage from the user & printing greater according to that*/

- 12 -
Vyankatesha Computech C Notes
# include <stdio.h>
main ()
{
int p;
clrscr ();
printf (“\n Enter percentage”);
scanf (“%d”, &p);
if (p>=75)
printf (“\n Grade=A+”);
else
{
if (p>=60 && p<=75)
printf (“\n Grade=A”);
else
{
if (p>=50 && p<=60)
printf (“\n Grade=B”);
else
{
if (p>=40 && p<=50)
printf (“\n Grade=C”);
else
{
if (P>=35 && P<=40)
printf (“Pass”);
else
printf (“Fail”);
}
}
}
}
getch ();
}
16. /* Menu driven program for arithmetic operation*/
#include <stdio.h>
main ()
{
int a,b,c;
clrscr();
printf (“\n enter two no.”);
scanf (“%d %d”, &a, &b);
printf (“\n 1 Addition \n 2 subtraction”);
printf (“\n3 Multiplication \n 4 Division”);
printf (“\n 5 Reminder”);
printf (“\n %d”, &c); /* Reading user choice*/
if (c==1)
printf (“\n Addition: %d”,a+b);
else
if (c==2)
printf (“\n subtraction: %d”,a-b);

- 13 -
Vyankatesha Computech C Notes
else
if (c==3)
printf (“\n Multiplication: %d”,a*b);
else
if (c==4)
printf (“\n Division: %d”,a/b);
else
if (c==5)
printf (“\n Reminder :%d”,a%b);
else
printf (“\n user choice wrong please try again”);
getch();
}
 Turnary Operator (conditional operator)

Syntax:-

Variable=exp1?exp2:exp3;

Here exp1 is conditional expression if It is true than the value expression 2 is assign to the variable . If
conditional exp. Is false then the value of exp.3 is assign to the variable.

17. /*Finding greatest among two no.*/

#include <stdio.h>
main ()
{
int a,b,c;
clrscr ();
printf (“\n enter two no.”);
scanf (“%d %d”, &a, &b);
c=a>b? a : b;
printf (“\n c=%d if greater”,c);
getch ();
}

OR

#include <stdio.h>
main ()
{
int a,b;
clrscr ();
printf (“\n enter two no.”);
scanf (“%d %d”, &a, &b);
a>b? printf (“\n a is greater”); printf (“\n b is greater”);
getch ();
}

18. /* finding greatest among three no. using nested turnary operator */
- 14 -
Vyankatesha Computech C Notes

#include <stdio.h>
main ()
{
int a,b,c,g;
clrscr ();
printf (“\n enter three no.”);
scanf (“%d %d %d”, &a, &b,&c);
g=a>b? (a>c ? a:c): (b>c ? b:c)
printf (“\n g=%d is greater”,g);
getch ();
}
OR
#include <stdio.h>
main ()
{
int a,b,c,g;
clrscr ();
printf (“\n enter three no.”);
scanf (“%d %d%d”, &a, &b,&c);
g=a>b? (a>c ? printf (“\n a is greater”):
printf (“c is greater”): (b>c ? printf (“ b is greater”):
printf (“c is greater”);
getch ();
}

 “Switch” statement:- In switch statement it will execute the statements under the
particular case if the switch variable value and case value is matched if switch
variable value is not matched with any case it will execute the statements under
the default . In switch statement the data type of case value and switch variable
must be same
Syntax:-
switch (switch variable)
{
case value 1:statement 1;
statement 2;
break;
Case value 2 :statement 1;
statement 2;
break;
Case value 3:statement 1;
statement 2;
break;
default : statement 1;
statement 2;
break;
}

19. /* Menu driven program for arithmetic operation using switch statement */

- 15 -
Vyankatesha Computech C Notes

#include <stdio.h>
main ()
{
int a,b,ch;
clrscr();
printf (“\n enter two no.”);
scanf (“%d %d”, &a, &b);
printf (“\n 1 Addition \n 2 subtraction”);
printf (“\n3 Multiplication \n 4 Division”);
printf (“\n 5 Reminder”);
printf (“\n Enter for choice” )
scanf (“%d”, &ch); /* Read choice*/
switch (ch)
{
case 1:printf (“\n Addition: %d”,a+b);
break;
case 2:printf (“\n subtraction: %d”,a-b);
break;
case 3:printf (“\n Multiplication: %d”,a*b);
break;
case 4:printf (“\n Division: %d”,a/b);
break;
case 5:printf (“\n Reminder: %d”,a%b);
break;
default : printf (“\n user choice is wrong”);
}
getch();
}
OR
#include <stdio.h>
main ()
{
int a,b;
char op;
printf (“\n enter the exp.”);
scanf (“%d %c %d”, &a, &op, &b);
switch (op)
{
case ‘+’: printf (“\n Addition: %d”, a+b);
break;
case ‘-’ : printf (“\n subtraction: %d”,a-b);
break;
case ‘*’: printf (“\n Multiplication: %d”,a*b);
break;
case ‘/’ : printf (“\n Division: %d”,a/b);
break;
case ‘%’:printf (“\n Remainder: %d”,a%b);
break;
default : printf (“\n user exp is wrong”);

- 16 -
Vyankatesha Computech C Notes
}
getch();
}
20. /* Evaluate conditional exp.*/

#include <stdio.h>
main ()
{
int a,b;
char op;
printf (“\n enter conditional exp.:(<,>,=)”);
scanf (“%d %c %d”, &a,&op, &b);
switch (op)
{
case’<’:a<b ? printf(“True”): printf(“False”);
break;
case’>’:a>b ? printf(“True”): printf(“False”);
break;
case’==’:a==b ? printf(“True”): printf(“False”);
break;
default : printf(“\n wrong exp.”);
}
getch();
}

 Loops:-

Loop is a cycle or repetitions . When some set of stat. are required to execute many times then loops are
used . In ‘c’ there are three types of loop.
a. For Loop
b. While Loop
c. Do-While Loop

A. For Loop:- In for loop first it will initialize the expression and than check the
condition and execute the body of loop till the condition is true if condition
become false it will break the loop

Syntax:-

For (initialization exp; condition exp; increment exp.)


{
Statement 1
Statement 2

- 17 -
Vyankatesha Computech C Notes
|
|

Flow Chart Of For Loop:-


Initialization exp
Initalisa

If
condition False

. True

Increment exp. Stat 1

Stat 2

21. /* Printing “Hello”5 times*/


# include <stdio.h>
main ()
{
int i;
clrscr();
for(i=1; i<=5; i++)
{
printf (“\n Hello”);
}
getch ();
}

I = i=1
1

0ut Put-Hello
False Hello
i<=5 Hello
Hello
- 18 -
Vyankatesha Computech C Notes
Hello

i=i+1 true

printf(hello)

B. While Loop:- In while statement it will execute the body of loop till condition is
true if condition become false it will break the loop
Syntax:-
while (condition)
{
Statement 1;
Statement 2;
|
|
}

Flow chart:-

is

Con. ? False

Increment exp.

True

statement1

statement2

22./* Printing Hello five times using while loop*/

main()
{
int I;
clrscr ();

- 19 -
Vyankatesha Computech C Notes
i=1
while(i<=5)
{
printf (“\nHello”);
i++;
}
getch();
}

Flow chart:-

I=1

False

If

I<=5

True

printf(Hello)

I++

- 20 -
Vyankatesha Computech C Notes

c. Do While Loop:-

Syntax:-

do
{
Statement1;
Statement2;
}
while (condition);

Flow chart:-

Statement1
Statement2

False

If condition

? True

In while loop the condition is checked initially i. e. before entering in the loop. Where as in Do-While loop first
it enter in the loop execute the body of loop & then it check the condition the following two program
demonstrate the difference in while and & Do-While loop.

22. /*Example of while loop*/

# include <stdio.h>
main()
{
int i;
clrscr();
i=11;
while(i<=10)

- 21 -
Vyankatesha Computech C Notes
{
printf(“i=%d”,i);
i++;
}
getch();
}
Out put:- no out put
This program is no output because variable i value is 11 & the condition for the while loop is i<=10 i.e.
11<#10 so the condition is false so it comes out of that loop is without executing .

23. /*Example of Do- while loop*/

# include <stdio.h>
main()
{
int i ;
clrscr();
i=11;
do
{
printf(“i=%d”,i);
i++;
} while(i<=10);
getch();
}
Output:- i=11

Here I is initialize as 11 that it enters in the loop execute the body of the loop so print value of i as 11
then I’s value incremented it become 12 than it check the condition (12<=10)the condition is false it
comes out of the loop. So in Do-while loop the body of the loop executing at least one times even if the
condition is initialize false.

24. /*Printing reverse of the give no.*/


# include <stdio.h>
main()
{
int n,r;
clrscr();
printf(“\n Enter the no.”);
scanf(“%d”,&n);
printf(“\n the reverse no. is”);
while(n>0)
{
r=n%10;
printf(“%d”,r);
n=n/10; /*n/=10*/
}
getch ();
}
Output:-

- 22 -
Vyankatesha Computech C Notes

Enter the no. 875

The reverse no.578


1) r = 875 % 10 = 5
n = 875 / 10 = 87
2) r = 87 % 10 = 7
n = 87 / 10 = 8
3) r = 8 % 10 = 8
n = 8 / 10 = 0

25. /* Read one no. from the were & calculate the sum & count the no. of digit */
# include <stdio.h>
main()
{
int n, r, count , sum;
clrscr();
printf(“\n Enter the no.”);
scanf(“%d”, &n);
while(n>0)
{
r = n%10;/* taking last digit*/
sum = sum+r;/*sum of digit*/
count = count++; /*count of last digit*/
n = n/10; /*reverse last digit*/
}
printf(“sum=%d”,sum);
printf(“no.of digit=%d”,count);
getch ();
}
n =4572, sum =0, count =0.
1) r =4572 % 10 =2
Sum =0+2=2
n = 4572/0 =453
count++=1
2) r =455 % 10 =3
Sum =2+3=5
n = 453/10 =45
count++=2
3) r =45 % 10 =5
Sum =5+5=10
n = 45/10 =4
count++=3
Output:-
Enter the no.786
n = 786, Sum = 0, Count=0
1) r = 786%10 = 6
Sum =0+6 = 6
n = 786/10 = 78
count++=1

- 23 -
Vyankatesha Computech C Notes
2) r = 78%10 = 8
Sum =6+8 = 14
n = 78/10 = 7
count++=2
3) r = 7%10 = 7
Sum =14+7 =21
n = 7/10 = 0
count++=3
sum of digit=21
number of digit=3

26. Armstrong no. is Addition of cube of that no. is same that no.
If the sum of cubes of the digit of a given no. is the no. itself than that no. is called Armstrong no.
e.g.153.

13+53+33=153.

27. /*Find out the given no. Armstrong no or not */

# include <stdio.h>
# include <match.h>
main()
{
int n, r, sum, m;
sum= 0;
clrscr =();
printf(“\n enter the no.”);
scanf(“\n %d ”, &n);
m==n; sum=0
while(n<0)
{
r=n%10
sum=sum+pow(r,3);
n=n/10;
}
if(sum==m);
printf(“Armstrong no.”)
else
printf(“not Armstrong no.”);
getch ();
}

28. /*Checking a given no. for palindrome */


If the reverse no. of the given no. is the no. it self then the no. is called palindrome.

Eg. 151 reverse:-151

/*Checking a given no. for palindrome*/


# include <stdio.h>

- 24 -
Vyankatesha Computech C Notes
# include <math.h>
main()
{
Int n, r, rev, m,count;
clrscr =();
printf(“\n enter the no.”);
scanf(“ %d ”, &n);
m=n;
/*counting no. of digit*/
for(count =0;m>0;count++)
m=n;
rev=0;
/* for getting reverse no.*/
for(count=count -1; count>=0; count--)
{
r=m%10;
rev+=r* pow(10,count);
m/=10;
}
If (rev==n)
printf(“palindrome”);
else
printf(“not palindrome ”);
getch ();
}

n=454 m= 454
1) m=454/10=45
count = 1
2) m=45/10=4
count = 2
3) m=4/10=0
count = 3

m=454 , n=454, rev=0, count


1) r=454%10=4
rev=0+3*102=300
m=454/10=45
count:-2
2) r = 45%10 = 5
rev = 400+5*101 = 450
m = 45/10 = 4
count:-1
3) r = 4%10 = 4
rev = 450+4*100 = 454
m = 4/10 = 0
count:-0

- 25 -
Vyankatesha Computech C Notes
29. /* printing all Armstrong no1- 500*/
# include <stdio.h>
# include <math.h>
main()
{
int r, m, sum, n;
clrscr =();
pintf(“\n Armstrong no. are:\n”);
for(n=1; n<=500; n++)
{
sum=0;
m=n;
while(m>0);
{
r = m%10;
sum+=pow(r,3);
m/=10;
}
If(n==sum)
printf(“\n %d”, n);
}
getch ();
}

1) n = 1, sum = 0; n=1
sum = 0+13 = 1 sum==n
m = 1/10 = 0 1==1

2) n =2, sum = 0; m=2


r = 2% 10 = 2 sum#n
sum = 0+23 = 8 8#2
m = 2/10 = 0

3) n = 22, sum = 0; m=22


i) r = 22%10 =2
sum = 0+23 = 8
m = 22/10 =2 sum#n
r = 2%10 = 2 10#22
sum = 8+23 =16
m = 2 /10 = 0

- 26 -
Vyankatesha Computech C Notes

30. Print all the palindrome no. in the range 1 to 1000*/

# include <stdio.h>
main ()
{
it r,s,m;
clrscr =();
m=1+1000; count=0;
for (m=1; m<=1000; m++);
{
sum=0;
m=n;
for (count=0;m>=0:count++);
{
r=m%10;
r+=r*p(10,count);
m = m/10;
rev=0; m=n;
}

31. /* Finding factorial of a given no.*/


# include <stdio.h>
main()
{
int n,f,i;
clrscr (); f=1
printf(“\n enter no.”);
scanf (“%d”,&n)
for(i=2; i<=n; i++)
{
f=*i
printf(“\n factorial is %d:%d %d”,n,n,f);
getch();
}
Output:-
Enter the no.:4
n=4 f=1
1) i=2, f=1*2=2
2) i=3
F=2*3=6
3) i=4
f=6*4=24
factorial of 4:4!=24

32. /*Finding factorial of 1 to 10 no.*/


# include <stdio.h>

- 27 -
Vyankatesha Computech C Notes
main()
{
Int n,i,f;
clrscr ();
printf(“\n factorial of 1to10 no.:/n”);
for(n=1;n<=10;n++)
{
f=1
}
for (i=2; i<=n; i++)
{
f*=I;
}
printf(“\n%d !=%d”,i,f”);
}
getch();
}

Statement Break:-

Used for breaking or quitting the loop for some other condition.
eg.

33. # include <stdio.h>


main()
{
int I;
clrscr ();
for(i=1; i<=10; i++)
{
printf (“\n i=%d”,i);
if (i==5)
break;
}
getch();
}

Output:-
I=1
I=2
I=3
I=4
I=5
Here ones i value become 5 it breaks or quit the loop even if the loop condition is till 10.

34. Statement continue:-

Used for skipping some block of statement in the loop for some condition.
# include <stdio.h>
main()

- 28 -
Vyankatesha Computech C Notes
{
int i;
clrscr ();
for(i=1; i<=10; i++)
{
if (i%2==0)
continue;
printf (“\n i=%d”,i);
}
getch();
}
Output:-
I=1
I=3
I=5
I=7
I=9
Here where i value divisible by 2 i.e. even no then its go for the next value of I i.e. continue statement of
executing with skipped printf statement So only odd no. are printed.

35. Gotoxy():- This function is used for positioning the cursor by giving x & y coordination on the screen.
Syntax:-
gotox y (column no, row no);
e.g.
gotoxy(15,5);
cursor point on 15th column &5th row.

36. Prime no.:- The no. which is divisible 1 & itself only then that no. is called prime no.
/* find the given no. prime or not*/
# include <stdio.h>
main()
{
int n, i;
clrscr ();
printf(“\n enter the no.”);
scanf (“%d”’&n);
for(i = 2; i< n; i++)
{
If (n% i==0)
break;
}
If(i==n)
printf (“\n prime no.”);
else
printf (“\n not prime no.”);
getch();
}
37. /*Print all the prime no. from 1 to 100*/
# include <stdio.h>
main()

- 29 -
Vyankatesha Computech C Notes
{
int n, i, count=0;
clrscr ();
printf(“\n prime no. with in 1 to 100”);
for(n = 1; n <= 100; n++);
{
for(i = 2; i < n ; i++);
{
if (n% i==0)
break;
}
If(i==n)
printf (“\n %d”,n);
count++;
getch();
}

38. /* Printing first given n no. in the Fibonacci series */


# include <stdio.h>
main()
{
int prev , cur, count, n , next;
clrscr ();
printf (“\n enter the value of n:”);
scanf (“%d”, &n);
prev =0;
cur =1;
printf (“\n first %d no. of the fibonaces series are:\n”,n);
printf(“\n 0,1,”);
for (count=3; count<=n; count++)
{
next = prev+ cur;
printf (“%d”,next);
prev= cur;
cur = next;
}
getch ();
}
Output:-
Enter the value of n : 8
0,1,1,2,3,5,8,13.

1 2 3 4 5 6 ------------- 10
11 12 13 14 15 ------------- 20
21 22 23 --------------30
|

- 30 -
Vyankatesha Computech C Notes
|
91 92 93 -------------100

39. /* program for printing the above table*/


# include <stdio.h>
main()
{
int i,j;
clrscr ();
printf (“\n the no. table is:\n”);
for (i=1; i<=91; i+=10)
{
for (j = i; j <= i+9 ; j++)
{
printf (“%d”, j);
}
Printf (“\n \t” );
}
getch();
}

1 11 21 -----------------91
2 12 22 92
3 13 23 93
| | |
| | |
| | |
10 20 30 --------------- 100

40. /* program to display above table*/


# include <stdio.h>
main()
{
int i,j;
clrscr ();
printf (“\n the nos. in table is:\n”);
for (i=1; i<=10; i=i+1)
{
for (j = i; j<= i+90 ; j=j+10)
{
printf (“%d”, j);
}
printf (“\n”);
}
getch();
}

1 2 3 10
2 4 6 20
3 6 9 30

- 31 -
Vyankatesha Computech C Notes
|
|
10 20 30 100

41. /* program for print the above table*/


# include <stdio.h>
main()
{
int i,j;
for (i=1; i<=10; i++)
{
for (j = i ; j<= i*10 ; j+=i)
{
printf (“%d”, j);
}
printf (“\n”);
}
getch();
}
42. /* write a program given no triangle*/
1
12
123
1234
12345
# include <stdio.h>
main()
{
int i,j=1
for (i=1; i<=5; i++)
{
for (j = I ; j<= i ; j++)
{
printf (“%d”, j);
}
printf (“\n”);
}
getch();
}

1
21
321
4321
54321
# include <stdio.h>
main()
{
int i,j;

- 32 -
Vyankatesha Computech C Notes
for (i=1; i<=5; i++)
{
for (j = i ; j>= 1; j--)
{
printf (“%d”, j);
}
printf (“\n”);
}
getch();
}
54321
4321
321
21
1
/* program for this */
# include <stdio.h>
main()
{
int i,j;
for (i=5; i>=1; i--)
{
for (j = i ; j>= 1; j--)
{
printf (“%d”, j);
}
printf (“\n”);
}
getch();
}
12345
1234
123
12
1
/* Program for print above triangle */
# include <stdio.h>
main()
{
Int i,j;
for (i=5; I >=1; i--)
{
for (j = 1 ; j<= i; j++)
{
printf (“%d”, j);
}
printf (“\n”);
}
getch();
}

- 33 -
Vyankatesha Computech C Notes

1
12
123
1234
12345
1234
123
12
1

/* Program for print above triangle */


# include <stdio.h>
main()
{
Int i,j;
clrscr ();
for (i=1; I <=5; i++)/*for upper triangle*/
{
for (j = 1 ; j<= i; j++)
printf (“%d”, j);
printf (“\n”);
}
for (i=4; i>=1; i--)/* for lower triangle*/
{
for (j = 1 ; j<= i; j++)
printf (“%d”, j);
printf (“\n”);
}
getch();
}
a
ab
abc
abcde
abcd
abc
ab
a
/* program for above triangle */
# include <stdio.h>
main()
{
int i,j;
clrscr ();
for (i=’a’; i <=’e’; i++)
{
for (j = ‘a’ ; j<= i; j++)
printf (“%c”, j);

- 34 -
Vyankatesha Computech C Notes
printf (“\n”);
}
for (i=’d’; i>=’a’; i--)
{
for (j = ‘a’ ; j<= i; j++)
printf (“%c”, j);
printf (“\n”);
}
getch();
}
1
12
123
1234
12345
# include <stdio.h>
main()
{
int i,j;
clrscr ();
for (i=1; i <=5; i++)
{
for (j = 5 ; j>= i; j--)
printf (“ ”);
for (j = 1 ; j>= i; j++)
printf (“\d”,j);
printf (“\n”);
}
getch();
}

5
45
345
2345
12345

/*Print above triangle */


# include <stdio.h>
main()
{
int i,j;
clrscr();
for (i=5; I >=1; i--)
{
for (j = 1 ; j<= i; j++)
printf (“ ”);
for (j = i ; j>= 5; j++)
printf (“%d”,j);
printf (“\n”);

- 35 -
Vyankatesha Computech C Notes
}
getch();
}
5
44
333
2222
11111

/* Program to print above triangle */


# include <stdio.h>
main()
{
int i,j;
for (i=5; i >=1; i--)
{
for (j = 1 ; j<= i; j++)
printf (“ ”);
for (j = i ; j>= 5; j++)
printf (“%d”,i);
printf (“\n”);
}
getch();
}
1
22
333
4444
55555

/* Program for print above triangle */


# include <stdio.h>
main()
{
int i,j;
clrscr();
for (i=1; i <=5; i++)
{
for (j = 5; j>= i; j--)
printf (“ ”);
for (j = 1 ; j<= i; j++)
printf (“%d”,i);
printf (“\n”);
}
getch();
}
1
2 2
3 3 3
4 4 4 4

- 36 -
Vyankatesha Computech C Notes
5 5 5 5 5
/* Program for print above triangle */
# include <stdio.h>
main()
{
int i,j;
clrscr();
for (i=1; I <=5; i++)
{
for (j = 5; j>= i; j--)
{
printf (“ ”);
}
for (j = 1 ; j<= i; j++)
{
printf (“%d”,i);
printf (“\n”);
}
getch();
}
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

/* Program for print above triangle */


# include <stdio.h>
main()
{
int I ,j, j1;
clrscr();
for (i=1; i <=5; i++)
{
for (j = 5; j>= i; j--)
{
printf (“ ”);
for (j = 1 ; j<= i; j++)
printf (“%d”,j);
for (j = i-1 ; j>= 1; j--)
printf (“%d”,j);
printf (“\n”);
}

1
12 1
1 2 3 2 1
1 2 3 4 3 2 1

- 37 -
Vyankatesha Computech C Notes
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
12 1
1

/*Print above triangle */


# include <stdio.h>
main()
{
int i, j;
clrscr();
for (i=1; i <=5; i++)
{
for (j = 5; j>= i; j--)
printf (“ ”);
for (j = 1 ; j<= i; j++)
printf (“%d”,j);
for (j = i-1 ; j>= 1; j--)
printf (“%d”,j);
printf (“\n”);
}
for(i=4; i>=I; i--)
{
for(j=5; j>=i; j--)
printf (“ ”);
for (j = 1 ; j<=i; j++)
printf (“%d”,j);
for (j = i-1 ; j>= 1; j--)
printf (“%d”,j);
printf (“\n”);
}
getch();
}

1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 4 3 2 1

/*Print above triangle */


# include <stdio.h>
main()
{
int i, j;
for (i=1; i <=5; i++)
{
for (j = 1; j<= i; j++)/*left tringle*/
printf (“%d”,j);

- 38 -
Vyankatesha Computech C Notes
for (j = 4 ; j= i; j--) /* left spacing */
printf (“ ”);
for (j =( i==5?4:i); j>= 1; j--)
printf (“%d”,j);
printf (“\n”);
}
getch();
}
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
/*print above triangle*/
# include <stdio.h>
main()
{
int i, j;
for (i=1; i <=5; i++)
{
for (j = 1; j<= i; j++)/*left tringle*/
printf (“%d”,j);
for (j = 5 ; j> i; j--)
printf (“ ”);
for (j =( i==5?4:i); j>= 1; j--)
printf (“%d”,j);
printf (“\n”);
}
for(i=4; i>=I; i--)
{
for(j=1; j<=i; j++)
printf (“ %d ”,j);
for (j = 4 ; j>=i; j--)
printf (“ ”);
for (j = 3; j>= i; j--)/*right triangle*/
printf(“ ”);
for (j=I; j>=1;j--)
printf (“%d”,j);
printf (“\n”);
}
getch();
}

/*program for print following triangle */


# include <stdio.h>
main()

- 39 -
Vyankatesha Computech C Notes
{
int i, j;
clrser();
for (i=1; i <=5; i++)
{
printf (“\n”);
for (j = 5 ; j> i; j--)
printf (“ ”);
for (j =1; j<= i; j++)
printf (“%d”,j);
for(j=i-1; j=1; j--)
printf (“%d”,j);
}
for(i=5; i>=I; i--)
{
for (j =( i==5?2:1 ); j<= i; j++)
printf (“%d”,j);
for(j=i-1; j=1; j--)
printf (“ %d ”,j);
printf(“ \n ”);
printf (“ ”);
for (j = 5 ; j= i; j--)
printf(“ ”);
}
getch();
}

1
1 2 1
1 2 3 2 1
123 4 3 2 1
1234 5 4 3 2 1 2 3 4 5 4 3 2 1
1 2 34 3 2 1
1 2 32 1
1 21
1

Arrays :- It is the combination of same data type variable or a set of same data type variable & the set
name is named as array name .
Declaration of an Array.

Data type array name [array size].


e.g.
int a[10]
Here it declaration 10 variables of types integer & the variable are a [0], a [1], a [2], a [3], a [4], a [5], a [6],
………a [9]these are called array element.

Initialization of array:-

- 40 -
Vyankatesha Computech C Notes
int a [5]={10,14,34,40,100};

Array element are Initialized as

a[0]=10
a[1]=14
a[2]=34
a[3]=40
a[4]=100

Memory allocation of an array:-

A continues memory is allocated in an array.

e.g. int a[5]={10,20,30,40,50}

here it allocation 10 bytes of continues memory because it declares 5 integer variable & 1 integer variable
requires 2 bytes of memory.
a[0] a[1] a[2] a[3] a[4]

a a 10 20 30 40 50
2 bytes 2 bytes 2 bytes
2 bytes 2 bytes

5 * 2 = 10 bytes
size of an array byte require for integer.

Note:- 1) Here 0,1,2,….. are called indexes & single no called index
2) Array always start with 0 so the last element is size -1 index.
3) All the variable in the array are independent so the array element a[2] is
pronounced as a of 2 .

/*Reading & printing an array */


# include <stdio.h>
main()
{
int a[10]; /*declaration of in array*/
int i; /*use as an index variable */
clrscr();
printf (“\n enter 10 no:\n”);
/*reading 10 no. one by one*/
for (i = 0 ; i<10; i++)
{
printf (“\n a [%d]:”,i);
scanf(“%d”,&a[i]);
}
printf (“\n the array element are”);
for(i=0; i<10;i++)
{
printf (“ \n a[%d]:%d ”,i,a[i]);

- 41 -
Vyankatesha Computech C Notes
}
getch();
}
/*printing the sum of all the array elements*/
# include <stdio.h>
main()
{
int a[10],i,sum=0;
clrscr();
printf (“\n enter 10 no:\n”);
for (i = 0 ; i<10; i++)
{
printf(“\n a[%d]:”, i);
scanf(“%d”, & a[i]);
sum+=a[i];
printf(“sum of array element are %d, sum”);
getch();
}
/*finding max min no. of in the given array*/
# include <stdio.h>
main()
{
int a[10], mix, min,i;
clrscr ();
printf(“\n enter the 10 no:\n”);
for(I = 0;I < 10, i++)
scanf(“%d”, &a[i]);
max=a[0];
min=a[0];
for=(i = 1; i < 10; i++)
{
if(a [i]< min)
min=a[i];
if (a[i]>max)
max=a[i];
}
printf(“\n minimum : %d”, min);
printf(“maximum : %d”, max);
getch();
}
/* searching a given no. is in an array print the location of that no.*/
# include <stdio.h>
main()
{
int a[10], n, found=0;
clrscr ();
printf(“\n enter the 10 no:\n”);
for(i = 0;i < 10, i++)
scanf(“%d”, &a[i]);
printf (“\n enter the searching no.”);

- 42 -
Vyankatesha Computech C Notes
scanf(“%d”, &n);
for(i=0; i<10,i++)
{
if(a[i]==n)
{
printf(“\n is passed location %d”, i);
found=1;
}
}
if(found==0)
printf(“\n no. not present”);
getch();
}
/*Write a program for printing all the even no. & all the odd no. in the given array*/
# include <stdio.h>
main()
{
int i ,a[10];
clrscr ();
printf(“\n enter the 10 no:\n”);
for(i = 0;i <10, i++)
scanf(“%d”, &a[i]);
printf (“even nos. are”);
printf(“odd nos. are”);
for(i=0; i<10,i++)
{
if(a[i]%2==0)
printf(“%d”, a[i]);
}
for (i=0; i<10; i++)
{
if (a[i]%2 !=0)
printf(“%d”, a[i]);
}
getch();
}
/* Sorting given no. in ascending order*/
# include <stdio.h>
main()
{
int i ,j, t, a[10];
clrscr ();
printf(“\n enter 10 no.”);
for(i = 0;i < 10, i++)
scanf(“%d”, &a[i]);
printf (“\n the no. ascending order are \n”);
for(i=0; i<10; i++)
{
for (j=i+1; j<10; j++)
{

- 43 -
Vyankatesha Computech C Notes
If a[i] > a [j]
{
t=a[i];
a [i]=a[j];
a[j]=t;
}
}
printf(“%d, \n”, a[i]);
}
getch();
}
/* printing & counting the positive no & negative no in given array */
# include <stdio.h>
main()
{
int a[10]; I, cop=0; con=0;
printf(“\n enter 10 no.”);
for(i = 0;i < 10, i++)
{
scanf(“%d”, &a[i]);
}
printf (“\n the positive no are: \n”);
for(i=0; i<10; i++)
{
if (a[i]>0)
{
printf(“%d\n”a[i]);
cop++;
}
printf(“\n the negative no are :\n”);
for (i=0; i<10; i++)
{
if(a[i]<0)
{
printf(“%d\n”, a[i]);
con++;
}
}
printf(“\n count of +ve nos :%d”, cop);
printf(“\n count of -ve nos :%d”, con);
getch();
}

/*Mark list program*/


# include <stdio.h>
main()
{
int marks[7],i,count=0,sum=0;
float p;
clrscr ();

- 44 -
Vyankatesha Computech C Notes
printf(“\n enter the subjects mark: \n”);
for(i = 0;i < 7, i++)
scanf(“%d”, &marks[i]);
for(i = 0;i < 7, i++)
sum+=marks[i];
if (marks[i] <40)
{
count ++;
}
}
if (count==0)
printf(“\n pass”);
else
{
if(count <=3)
printf(“\n ATKT”);
else
printf(“\n Fail”);
}
p=(sum/7);
printf (“\n percentage: %f”,p);
getch();
}
 String:-
Character array:- it is a character array which is a set of character. For reading & printing the string a
different format specifire is use that is %s.
A Null string concept:- (\0) when a string is read by a compiler a special character called null String(\0) is
appended at the end of the string. Which is used for the indication of the end of the string.
Declaring a string:-

Syntax:- char character array name [array size];


e.g. character s[20];

Initialization of a string:-

Character string [20]=”Vyenkatesha ”;

Memory allocation for a string :- A contentious memory is allocated for a string.

e.g. character string[10]=”vyenkat ”;

0 1 2 3 4 5 6 7 8 9

string V y E n K a t \0 -

str[0] str[2] str[4] str[6] str[8]


str[1] str[3] str[5] str[7] str[9]

- 45 -
Vyankatesha Computech C Notes
/*Reading & printing a string*/
# include <stdio.h>
main()
{
char str[10];
clrscr();
printf(“\n enter a string :”);
scanf(“%j”, str);/* don’t use & operator */
printf(“\n the given string is :% s”,str);
getch();
}
Enter the string :- computer
The given string is:-computer

/* printing the length of the string no. of lower & upper case character no. of special character & no.
digits*/

# include <stdio.h>
main()
{
char str[20];
int i, i=0, nou=0, nol=0,nod=0,nos=0;
printf(“\n enter the string :”);
scanf(“%s”, str);
for (i=0; str[i]!=’\0’;i++)
{
l++; /*increment length of a string*/
if (str[i]>=’A’ & str [i]<=’z’)
nou++;
else
if(str[i]>=’a’ & str[i]<=’z’)
nol++;
else
if(str[i]>=’o’ & str [i]<=’9’)
nod++;
else
nos++;
}
printf(“\n length of the string: %d”, l);
printf(“\n no.of upper case letter :%d”,nou);
printf(“\n no.of lower case letter: %d”, nol);
printf(“\n no.of digits %d”, nod);
printf(“\n no. of special character %d ”, nos);
getch();
}

/*Convert the given string in uppercase letter*/


# include <stdio.h>

- 46 -
Vyankatesha Computech C Notes
main()
{
char str[20];
int i;
clrscr ();
printf(“\n enter the string :”);
scanf(“%s”, str);
for (i=0; str[i]!=’\0’;i++)
{
if (str[i]>=’a’ & str [i]<=’z’)
str[i]=str[i]-32;
}
printf(“upper case string :%s”,str);
getch();
}
/*Printing the reverse of given string*/
# include <stdio.h>
main()
{
char str[10];
int i,l=0;
printf(“\n enter the string :”);
scanf(“%s”, str);
for (l=0; str[l]!=’\0’;l++);/*counting length*/
/*printing the string in reverse */
printf(“\n the reverse string is”);
for(i=l-1; i>=0; i--)
{
printf(“%c”, str[i]);/*print from last element to first char.*/
}
getch();
}
/* Checking the given string palindrome */
# include <stdio.h>
main()
{
char str[10];revstr[10];
int i, l, j;
clrscr();
printf(“\n enter the string :”);
scanf(“%s”, str);
/*Take length of the string*/
for (l=0; str[l]!=’10’;l++);
for(i=l-1, j=0;i>=0;i--,j++)
{
reverse[j]=str[i];
}
revstr[j] = `\0’;
printf(“\n the reverse string :%s”, revstr);
/*Comparing string for equaling */

- 47 -
Vyankatesha Computech C Notes
for(i=0;i<l; i++)
{
if(str[i]!=revstr[i])
break;
}
If(i==l)/*if all comparison are equal*/
printf(“\n palindrome”);
else
printf(“not palindrome”);
getch();
}
Output
Enter the string: a b c a
Reverse string : a c b a
Not palindrome
0 1 2 3 4
str
a b c a \0
0 1 2 3 4
reverse a c B a \0

not equal so execute break

strcmp()-string compare used for comparing two string


syntax:-
integer s= strcmp(string1,string2);
s>0(+ve)if string 1> string2
s<0(-ve) if string 1< string2
s=0 if string1=string2
e.g.
1] char s1[10]=”abc”, s2[10]=”xyz”
int s;
s=strcmp(s1,s2);
s=-ve s1 < s2
2] char s1[10]=”abc”, s2[10]=”ABC”
int s;
s=strcmp(s1,s2);
s1= +ve s1>s2
3] char s1[10]=”abcde”, s2[10]=”abcde”
int s;
s=strcmp(s1,s2);
s1=0 s1=s2

Strcpi() or strcmp()

Used for comparing two strings by ignoring their cases (does not differentiate in lower & upper case letter)

Syntax:- same as string compare function


e.g.
char s1[10]=”ABCDE”, S2[10]=”abcde”

- 48 -
Vyankatesha Computech C Notes
int s;
s= strcmpi(s1,s2);
s=0 s1=s2

#include <stdio.h>
#include<string.h>*/for string function*/
main()
{
char s1[10], s2[10];
int s;
clrscr ();
printf(“\n enter two strings:”);
scanf(“%s %s”, s1, s2);
s = strcmp(s1,s2)
printf(“\n s=%d”, s);
getch();
}
Output
Enter two string :- abcde xyz
S=-23(a,x ascic code)
Enter two string:- afhg ABDfh
S=32
Difference a & A (ASCII)
Enter two string 123 456
S=-3
Difference 1 & 4 (ASCII)
Enter two string a s d a s d
S=0
Strcat()- string concatenate
concatenate source string to the destination string.
Syntax:-
String =strcat(destination string , sourer string );
e.g.
char s1[10]=”abc”, s2[10]=”xyz”;
strcat(s1,s2);
s1=”abcxyz”
s2=”xyz”
stringcat( ) - concatenate first 3 no. of char of source to the destination string.

Syntax-string=strncat(destination string , source string ,no.of char);


char s1[10]=”abc”, s2=[10]=”xyzlmn”
strcat(s1,s2,3);
S1= “abcxyz”
S2= “xyzlmn”

The strncmp function compares first n char of two string.

/* Program for operate all string function */


#include<stdio.h>
#include<string.h>

- 49 -
Vyankatesha Computech C Notes
main()
{
char s1[20], s2[20];
int ch.
do
{
clrscr();
printf(“\n 1. Input 2. String length”);
printf(“\n 3. String reverse 4. String can”);
printf(“\n 5. String Iower 6. String upper”);
printf(“\n 7. String copy 8. exit”);
printf(“\n\n enter lower choice:”);
sanf(“%d”, &ch);
switch(ch)
{
case1: printf(“\n enter the string”);
scanf(“%s”, s1);
getch();
break;
case2: printf(“\n string : %s” s1);
printf(“\n length : %d ”, strlen(s1));
getch();
break;
case3: printf(“\n string : %s”, s1);
printf(“\n reverse : %s”, strrev(s1));
getch();
}
/* searching a char. In a given string*/
#include <stdio.h>
# include<string.h>
main()
{
char s[20],ch;
int i,n,count=0;
clrscr();
printf(“\n enter the string:”);
scanf(“%s”,s);
printf(“\n enter the searching char:”);
scanf(“% c”, &ch);
n=strlen (s);
for(i=0; i<n; i++)
{
if(s[i]==ch)
{
printf(“\n character present at location :%d”,i+1);
count++;
}
}
if(count==0)
printf(“\n char not found”);

- 50 -
Vyankatesha Computech C Notes
else
printf(“\n character present %d times ”, count);
getch();
}
 Assignments:-

Write a prag. For checking a string for palindrome using string function.

Two dimensional array :- Generally 2-d array are used for matrixes where a & b elements is recognized
by its row & columns no.

Declaration of 2 n array:-

Data type array name [row size] [column size]

e.g.
int a[3][3];

declares 3/3 matrix with integer element & the matrix is given as.
a[0] [0] a[0] [1] a[0] [2]
a[1][0] a[1][1] a[1][2]
a[2][0] a[2][1] a[2][2]
column index
Row
Index

e.g.
Initialization of 2 n array:-

int a[3][3]= {10,20,30};


{ 1, 2, 3 };
{11,12,13};
OR

int a[3][3]={10,20,30,1,2,3,11,12,13};

The element are

a[0][0]=10
a[0][1]=20
a[0][2]=30
|
|
|
a[2][2]=13

memory allocation for 2n array


A continues memory allocated in 2d array which in row wise i.e. the element of 1 st row allocated to first.
Previous e.g. memory allocation

- 51 -
Vyankatesha Computech C Notes
a 10 20 30 1 2 3 11 12 13
a[0][0] a[0][1]
a[0][2] a[1][0] a[1][0] a[1][2] a[2][0] a[2][1] a[2][2]
3+3*2 = 18 bytes
Int required 2bytes
Row size
Column size

/* Reading matrix elements from the user & then printing then in matrix order */
#include <stdio.h>
main()
{
int a [3][3];/declaring 3 -3 matrix*/
int i, j;/*i for row index , j for column index */
clrscr ();
printf(“\n enter a element of matrix \n’);
for (i=0;i<3,i++)/* for row*/
{
for(j=0;j<3,j++)/*for column*/
{
printf(“enter a [%d][%d]:”i,j);
scanf(“%d”, & a[i][j]);/* reading element row wise*/
}
}
printf(“\n the given matrix in :\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d”, a[i] [j]);
}
printf(“\n”, )/*the next line*/
}
getch();
}

/* Addition of two matrixes */


#include <stdio.h>
main()
{
int a [3][3], b[3][3], c[3][3];
int i,j;
clrscr ();
printf(“\n enter 9 elements of matrix A:\n”);
for(i=0,i<j, i++)
{
for(j=0,j<3,j++)
{
scanf(“%d”, &a[i][j]);
}

- 52 -
Vyankatesha Computech C Notes
printf(“\n enter 9 elements of matrix A:\n”);
for(i=0,i<3,i++)
{
for(j=0,j<3,j++)
{
scanf(“%d”, &a[i][j]);
}
}
/* Adding & printing matrix c*/
printf(“\n the addition matrix c is :\n”);
for(i=0,i<3,i++)
{
for(j=0,j<3,j++)
{
c[i][j]=a[i][j]+b[i][j];/*add*/
printf(“%d”, c[i][j]);
}
printf(“\n”);
}
getch ();
}

/* Multiplication of two matrixes */


A a00 a01 a02 B b00 b01 b02 c00 c10 c02
a10 a11 a12 * b10 b11 b12 =c c10 c11 c12
a20 a21 a22 b20 b21 b22 c10 c21 c22

c00 = (a00*b00)+(a01*b10)+(a02*b20)
c01 = (a00*b01)+(a01*b11)+(a02*b21)
c02 = (a00*b02)+(a00*b12)+(a02*b22)
c10 = (a10*b00)+(a11*b10)+(a12*b20)
c11 = (a10*b01)+(a11*b11)+(a11*b21)

#include<stdio.h>
main()
{
int a[3][3], b[3][3],c[3][3]={o,o,o,o,o,o,o,o,o}
int i,j,k;
clrscr ();
printf(“\n enter 9 elements of matrix A:\n”);
for(i=0,i<j,i++)
{
for(j=0,j<3,j++)
{
scanf(“%d”, & a[i][j]);
}
}
printf(“\n enter 9 element of matrix B:\n”)
for(i=0,i<3,i++)
{

- 53 -
Vyankatesha Computech C Notes
for(j=0,j<3,J++)
{
scanf(“%d”, & b[i][j]);
}
}
/*Multiplication of two matrix*/
for(i=0,i<3,i++)
{
for(j=0,j<3,j++)
{
for(k=0,k<3,k++)
{
c [i][j]= c[i][j] + a [i][k] * b[k][j];
}
printf(“%d”, c [i][j]);
}
printf(“\n”);
}
getch();
}

/* printing string array in ascending order*/


#include <stdio.h>
# include<string.h>
main()
{
char name [10][20],temp[20]
int i,j;
clscr();
/*reading 10 names*/
for(i=0;i<10;i++)
{
printf(“enter name %d :”i+1)
scanf(“%s”,name[i]);
}
for(i-0;i<10;i++)
{
for(j=i+1; j<10; j++)
{
if (strcmp (name [i], name[j])>0)
{
/*swapnig two string*/
strcpy(temp, name [i]);
strcpy(temp, name [i],name[j]);
strcpy(temp, name [j], temp);
}
}
}
/*printing name in sorted order*/

- 54 -
Vyankatesha Computech C Notes
for(i=0;i<10;i++)
{
printf(“\n %s”, name[i]);
}
getch();
}
/* Sorting array of string lengthwise */
#include <stdio.h>
# include<string.h>
main()
{
char name [10][20], temp [20]
int i,j;
clrscr();
/*reading 10 names*/
for(i=0;i<10;i++)
{
printf(“enter name %d :”i+1)
scanf(“%s”,name[i]);
}
for(i-0;i<10;i++)
{
for(j=i+1; j<10; j++)
{
if (strlen (name [i]> strlen(name[j]))
{
strcpy(temp, name [i]);
strcpy(temp, name [i],name[j]);
strcpy(temp, name [j], temp);
}
}
}
for(i=0;i<10;i++)
{
printf(“\n %s”, name[i]);
}
getch();
}
Assignment -6. Searching a given string in the given array of string.

Unformatted I/O statements:- These are input & out put statement which are using any format
specifier.

1] getchar ():- Used for reading a single char. from the user .

Syntax:- char var =getchar();

e.g. :- char ch;


ch=getchar();

- 55 -
Vyankatesha Computech C Notes
After inputing a character a delimiter is necessary for the completion of input. Which are enter key or
space key.

2]getche ():- get character with Echo


Used for reading single char.
e.g.:- char ch.
ch=getche ();

After inputing a char. Delimiter is not necessary. Generally it is called keypress function. Echos specify that
the enter character is display (echoed) on the screen.

3] getch():- get character without echo. Used for reading a single char.

ch=getch();
This is keypress function, The given char. Does not display on the screen.

4] putchar():- Used for printing a single char on the screen.


e.g.
a] char ch=’K’
putchar(ch);
b] putchar (‘A’)

/* e.g. on above function */


#include <stdio.h>
main()
{
char c1, c2, c3;
clrscr ();
printf(“\n enter a char”);
c1=getchar();
printf(“\n character”);
putchar(c1);
printf(“\n enter a char”);
c2=getche ();
printf(“\n character”);
putchar(c2);
printf(“\n enter a char.”);
c3=getch();
printf(“\n character”);
putchar(c3);
}
Output:-
Enter a char : a
Character :a
Enter a char : g
Character :g
Enter a char : enter char not display on screen.
Character :v

gets():- Used for reading & string or a sentence or a line .

- 56 -
Vyankatesha Computech C Notes
Syntax:- char s[20];
gets (s);
Enter key is necessary for completion of input. (space is not takes as delimiter).
/* Example on getch, getche, putchar */
#include <stdio.h>
main()
{
char c1, c2, c3;
clrscr ();
printf(“\n enter a char”);
c1=getchar();
printf(“\n character”);
printf(“\n enter a char”);
c2=getche ();
printf(“\n character”);
printf(“\n enter a char”);
ch=getch();
printf(“\n character”);
getch ();
}
Output:-
Enter a char : a
Character :a
Enter a char : b
Character :b
Enter a char : enter char not display on screen.
Character :c

puts():- Used for display a string on screen.

Syntax:- char s[20] =”Vyankatesha Computech”


1] puts(s)
2] puts (“Vyankatesha Computech”)

/* Example on get s & put s function */


# include<stdio.h>
main()
{
char a[25];
clrscr();
printf(“\n enter a string”);
gets();
printf(“\n given a string”);
puts();
getch();
}
/* Accepting a password & print password*/
# include<stdio.h>
main()
{

- 57 -
Vyankatesha Computech C Notes
char ch, put[13];
int i=0;
clrscr();
printf(“\n enter your password”);
do
{
ch=getch();/* Read single char without echo*/
if(ch==13)
{
break;
}
putchar(‘+’);
pwdt [i]= ch; /*store char. In array */
i++ /* for next character storage */
}while(ch!=13)
pwd [i]=’\0’;
printf(“\n your password is %s”, pwd);
getch();
}

/* Checking a given password for correct*/


#include <stdio.h>
main()
{
char ch; pwd[10], (pwd[10]=”Hello”);
int i=0;
clrscr();
printf(“\n enter your password”);
do
{
ch=getch();
if (ch==13);
break;
}
putchar(‘*’);
pwd[i]=ch;
i++;
}while(ch!=13);
If(strcmp(pwd),(pwd)==0)
printf(“\n correct password”);
else
printf(“\n not correct”);
getch();
}

/*Giving three character to user for correct password*/


#include <stdio.h>
main()
{

- 58 -
Vyankatesha Computech C Notes
char pwd[10], (pwd[10]=”Hello”);
int i=0;
clrscr();
printf(“\n enter your password”);
for(i=0;i<=3;i++)
{
printf(“\n enter your password”);
i=0;
}
do /* read password*/
{
ch=getch();
if(ch==13)
break;
}
putchar(‘*’);
pwd[i]=ch;
i++
}While(ch !=13);
if(strcmp(pwd),(cmp==0)
{
printf(“\n correct pwd at %d attempt”);
exit(0);
}
else
printf(“wrong password , try again”);
}
getch();
}

The valid passwords has the following restriction.


1. It should include Minimum 6 character & maximum 12 char.
2.Any password should start with only a letter it may be upper case or lower case letter.
3.The reading char in the password may have letters digits or a special char underscore
 Algorithm:-
1) Read password
2) Store in pwd array
3) Check the length of password in between 6 to 12 if not print error message.
4) Check first letter of pwd i.e. pwd[0] for lower case & upper case letter if not print error message.
5) check remaining letters i.e. from pwd [1] to length for lower case, upper case digits or under score.If
not print error message.

#include<stdio.h>
main();
{
char pwd[20], ch; int i=0, j;
clrscr();

- 59 -
Vyankatesha Computech C Notes
printf(“\n enter your password”);
do
{
ch=getch();
if(ch==13)
break;
pwd[i]=ch;
printf(“*”);
i++;
}while(ch!=13);
pwd[i]=’\0’;
printf(“\n\n your password is: %s”, pwd);
/*cheking for 6<=length <12*/
If (i>12)
printf(“\n invalid password c it should exceed 12 char”);
if(i<6)
printf(“\n invalid password ( it should contain min 6 char”);
/*checking first symbol for a letter */
if(! ( pwd [0]>=’a’ && pwd [0]<=’z’ && ! (pwd [0]>=’A’&&pwd[0]<=’Z’)
printf(“\n invalid password it should start with letter only”)
/* checking other symbol for letter digits or underscore*/
for (j=1, j<i; j++)
{
If (! pwd [j]>=’a’ && pwd [j]<=’z’ && !(pwd[j]>=’A’ &&pwd[j]<=’Z’)&& ! (pwd[j]>=’0’ &&pwd [j]<=’9’) && !
pwd[j]==’__’))
{
printf(“\n invalid password___(it should not include any special symbol)”)
break;
}
}
getch();
}

 Pointers:-

# include <stdio.h>
main()
{
int *p,a=5;
p=&a; /*address of a is given*/
printf(“\n Address of A=%d”, p);
printf(“\n value of A=%d”, *p);
printf(“\n Address of A=%u”, &a);
printf(“\n value of A=%d”, a);
}
 Pointing types:-
Integer pointer,
Float , character, array, structure, file
P a

- 60 -
Vyankatesha Computech C Notes

2001 5

3001 2001

The pointer & the memory location ( or the variable) towards which it is going to point both should belong
to same data type only. Any type of pointer & the memory location types are mismatch it will be a logical
error.

Pointer:-
It is variables which are used for storing memory address only.

Declaration of a pointer:-

Syntax:- data type * pointer variable;


Eg.
1) int *p;
declares an integer variable which is storing the memory address declares for integer variable.
2) Flot *fp;
Declares a float pointer where the memory address of float memory is stored.
Similarly for char. Pointer.

 Operator in pointer:-

* - An pointer operator- this operator is use in the pointer declaration to specify the variable as
pointer variable.
& - An address operator:- this is unary operator which is used memory address of the allocated
memory for the given variable.
Eg.:- int a=10
& a – gives memory address of variable a

* - As indirection operator:-

( value at address)- When in executable statement . Astrick is taken associating with a pointer variable
then that is called indirection operator which is used for indirectly accessing the value stored in
memory whose address is given by the pointer variable ( whose address is store in pointer variables.)

/* Example of pointer */
# include <stdio.h>
main()
{
int i=10;
int *p; /*declaration of pointer variable */
clrscr ();
p= &I; /*pointer variable (p)points to memory location of I */
printf(“/n value of i in i=%d ”, i);
printf(“/n address of i is & i =%u”, i);
printf(“/n value of p is p = %u”, p);
printf(“/n address of p is & p = %u”,&p);
printf(“/n value of address p is *p = %d”);

- 61 -
Vyankatesha Computech C Notes
printf(“/n value of i is *(&i) = %d”, )*(&i);
getch();
}
P Points to i

65498
Memory 65496
add of &P memory address

99 of I & i
65496 97
10

Output:-
Value of I is =10
Address I is & I = 65496
Value of p is p =65496
Address of p is & p = 65498
Value at address p is *p =10
Value of I is *(&i)=10

When a different data type pointer variable is initialized with the address of other data type variable
then the compiler will not show error but it show warring .

/* Example of assigning difff datatype address to differ type pointer*/


# include<stdio.h>
main()
{
int i=65, *p;
float f=3.10, *fp;
char c=’A’, *cp;
clrscr ();
/* assigning address of int var to float pointer*/
fp= &i;
printf(“\n \n value at address fp :%f”,*fp);
/* assigning address of int variable to char pointer*/
cp=&I;
printf(“\n value at address cp : %c”,*cp);
/* assigning add of float to var int pointer*/
p=&f;
printf(“\n value of add p :%d”, *p);
/* assigning add of float var to char.*/
cp=&f;
printf(“\n value at add cp:%d”,*cp);
getch ();
}

- 62 -
Vyankatesha Computech C Notes
 Operation of Pointer:- Generally for the pointer only arithmetic addition & subtraction operation are
take place in that usually only the increment & decrement are in use.
1] When the integer pointer var is incremented by one it increment by two considering the memory
required for integer (2 bytes)
2] When a float pointer is increment by one it increment by four because float data type required 4 bytes
memory.
3] Similarity for char. It increment by one.
Note :- When the pointer var. Is incremented it is pointing to the next memory or next variable.
/* Example showing diff. operation on pointer*/
#include <stdio.h>
main();
{
int*p, a=10, b=20;
float *fp, f1=10.20, f2=20.330;
clrscr();
p=&a; /*p point to a */
fp=&f1; /* fp point to f1*/
printf(“\n \n value of p=%u”,p);
printf(“\n \n add. of a=% u”, &a);
printf(“\n\n value at address p=%d”,*p)
p++; /*increment int pointer by 1/*);
printf(“\n\n address of b=%u”, &b);
printf(“\n value of p=%u”,p);
printf(“\n value at add. p=%d”,*p);
printf(“\n value of fp=%u”,fp);
printf(“\n value of f1=%u”,*f1);
printf(“\n value of add.fp=%f”, *fp);
fp++;/*increment float pointer1*/
printf(“\n add. Of f2=%u”,&fp);
printf(“\n value Of fp=%u”,&fp);
printf(“\n value at add of fp=%f”,*fp);
getch();
}
 Before increment
P a b
p 65486 65488
65486 10 20

&a

Fp f1 f2
65492 65492 65496
10.20 10.20 20.30
Xf1

P points to a
Fp pointer to f1

- 63 -
Vyankatesha Computech C Notes
 After increment p pointers tob

65488 65488b

65486+1
10 20
65488

Fp f1 f2
65492+1 10.20 65496
20.30
65496

Value of p=65488
Add. Of a=65488
Value at add. P=10
Add.of b=65490
Value of P = 65490
Value at add. P=20
Value of fp=65492
Add. Of f1 =65492
Value at add. Fp=10.20000
Add. Of f2=65496
Value of fp=65496
Value at add. Fp=20.29
In above program the memory allocation in the b & f2 are immediately allocated after a&f1 which is the
coincidences this not happen every time . In the other way the pointer p & fp are not pointing to b & f2
every time.

Pointers & arrays:- when any array is declares then the name of that array is nothing but a pointer
variable which holds the initial add. Of that array that is add. Of first element so it is always pointing to the
array.

Eg. int a[5]={11,12,13,14,15};


A pointing to array

a array name 8000 11 8001 a[0]=*(a+0)=*(8000)=11

800 0 8002 12 8003 a[1]=*(a+1)=*(8000+2)=*(8002)=12

Initial address 8004 13 8005 a[2]=*(a+2)=*(8000+4)=*(8004)=13

8006 14 8007 a[3]=*(a+3)=*(8000+(3*2))=*(8006)=14

8008 8009 a[4]=*(a+4)=*(8000+(4*2))=*(8008)=15

15

- 64 -
Vyankatesha Computech C Notes
When the specific array element is accessed by compiler then the compiler dynamically calculate the
memory add. of that element given by the following formula.Considering array name are the pointer which
is pointing to that arrayed that is a.

a[i]=*(a+(i*mem bytes required by data type ))

#include <stdio.h>
{
int a[5]={1,2,3,4,5};
clrscr();
printf(“\n \n value of a=%u”,a);
printf(“\n add. Of &a[0]=%u”,&a[0]);
printf(“\n value at add a:*a=%d”,*a);
printf(“\n value of a[0]=%d”, a[0]);
getch();
}

Output :_
Value of a=65494
Add. Of a[0]:&a[0]=65494

Value at add at a:*a=1


Value of a[0]=1

/* Program on accessing array element indirectly by the array name which is acting as a pointer to that
array */

# include <stdio.h>
main()
{
int a[3]={10,20,30};
clrscr();
for(i=0; i<3; i++)
{
printf(“\n &a [%d]=%u, a[%d]=%d, *(a+i):*(%u+%d)=%d”, i,&a[i],i,a[i],a,i,*(a+i));
}
getch();
}
Array name acting as a pointer to array
65496 10 a[0]=*(a+0)=*(65496+0)=10

65497

a 65498 20 a[1]=*(a+1)=*(65496+1)=*(65498=)20

65496 65499

65500 30 a[2]=*(a+2)=*(65496+2)=(65500)=30

65501

&a[0]=65496,a[0]=10*(a+i):*(65496+0)=10

- 65 -
Vyankatesha Computech C Notes
&a[1]=65498,a[1]=20*(a+i):*(65496+1)=20
&a[2]=65500,a[2]=30*(a+1):*(65496+2)=30
&a[2]=65500,a[2]=30*(a+1):*(65496+2)+

/*program of accessing array element using an external pointer ver. P*/


#include<stdio.h>
main()
{
int a[3]={10,20,30}*p,i;
clrscr();
/*storing initial add. Of an array in pointer ver. P, so that p point to that array*/
p=a; /*p=&a[0];*/
for(i=0; i<3; i++)
{
printf(“\n &a[%d]: %u, p %u, *p:%d”, I, &a[ i ], p, *p)
p++;/*p point to the next array element */
}
getch();
}

Array name (not changing)


p
A 65494 10 a[0] 65494

65495 external pointer (can b change)

65494 65496 20 a[1]

5497

65498 30 a[2]

65499

&a[0]:65494, p:65494, *p:10


&a[1]:65496, p:65496, *p:20
&a[2]:65498, p:65498, *p:30

/* program of accessing array element using an external pointer var. p as an array*/


#include<stdio.h>
main()
{
int a[3]={10,20,30}, *p,i;
clrscr();
/*storing initial add. of in array in pointer var. p, so that p points to that array*/

- 66 -
Vyankatesha Computech C Notes
p=a; /*p=& a[0]*/
for(i=0; i<3; i++)
{
printf(“\n p:%u, p[%d]:%d”, p, i,p[i])
getch(); p
} 65494
a 65494 10 a[0]=p[0]=*(p+0)=*(65494)=10

65494 65495

65496 20 a[1]=p[1]=*(p+1)=*(65496)=20

65497

65498 & 65499 30 a[2]=p[2]=*(p+2)=30

P:65494, p[0]:10
P:65494, p[1]:20
P:65494, p[2]:30
 Assignment:-
Write a program for reading 10 array element & sort then using pointer.

 Functions:-

#include<stdio.h>
main()
{
clrscr ();
message();
message();
getch();
}
message()
{
printf(“\n Laugh the world laugh with u”);
}
message()
{
printf(“\n cry the world laugh on u”);
}
/* Simple addition program by using functions */
#include <stdio.h>
main()

- 67 -
Vyankatesha Computech C Notes
{
clrscr();
add();
getch();
}
add()
{
int a, b, c, sum
printf(“enter 3 numbers”);
scanf(“%d%d%d”, &a, &b, &c);
sum=a+b+c;
printf(“\n sum=%d”, sum)
}
Function:-

A function is ,in effect a subprogram that can act on data & return a value. Every C program at least one
function main(). Each function has its own name , & when that name is encountered the execution of the
program branches to the body of that function. When the function returns, execution resume on the next line
of the calling function.
Function name (parameters)
{
Statement1;
Statement2;

Statement n;
}
/*finding max & min of 3 nos using function*/
#include <stdio.h>
main()
{
int a, b, c
clrscr();
printf(“enter 3 nos”);
scanf(“%d%d%d”, &a, &b, &c);
maximum(a,b,c);
minimum(a,b,c);
getch();
}
maximum(int x, int y, int z)
{
int m,
m=x>y? (x>z? x:z):(y>z? y:z);
printf(“\n maximum :%d ”, m);
}

- 68 -
Vyankatesha Computech C Notes
minimum (int x, int y, int z)
{
printf(“\n minimum : %d”, x<y? (x<z? x:z): (y<z? y:z));
}
/* function declaration is not required as function are passing & returning default integer argument */
#include <stdio.h>
main()
{
int a, b, c, max ,min;
clrscr();
printf(“enter 3 nos”);
scanf(“%d%d%d”, &a, &b, &c);
/*calling function*/
max=maximum (a,b,c);
min=minimum (a,b,c);
printf(“\n maximum : %d”, max);
printf(“\n minimum : %d”, min);
getch();
}
/*define function*/
maximum (int x, int y, int z)
{
int m,
/*finding maximum no using turnary operator*/
m= x>y? (x>z? x:z): (y>z? y:z);
return(m);
}
minimum(int x, int y, int z)
{
return(x<y? (x<z? x:z): (y<z? y:z));
Output:-
Enter 3 nos 1 2 3 - 6
Maximum=12
Minimum=-6

/* checking given no for prime no using function*/


#include <stdio.h>
main()
{
int n;
clrscr();
printf(“enter a no”);

- 69 -
Vyankatesha Computech C Notes
scanf(“%d”, &n);
prime(n);
getch();
}
prime(int a)
{
int i;
for (i=2; i<n;i++)
{
if(n%i==0)
break;
}
If (n==1)
printf(“\n %d : prime no.”, n);
else
printf(“\n %d : not prime no”,n);
}

# include<stdio.h>
area (float); /*declaring function*/
perimeter (float);
main)
{
float r;
clrscr();
printf(“\n enter the radius :”);
scanf(“%f ” &r);
area(r);
perimeter (r);
getch();
}
area(float r1) *defining function*/
{
float a1,
a1=3.1415 *r1*r1;
printf(“\n area :% f”, a1);
}
perimeter (float r2)
{
printf(“\n perimeter : %f”, 2*3 :1415*r2);
}
Enter the radius :-3
Area:-28.7
Perimeter:-18.85

- 70 -
Vyankatesha Computech C Notes

Call by value:-
When the variables are passed to the function then the value of the actual argument are copied in formal
variable. This type of function call in known as call by value. In call by value when the changes are made in
formal argument then these changes not affecting on actual argument the following example demonstrate
this concept.
main()
{
int a=10, b=20
printf(“\n before : a=%d, b=%d”, a, b);
swap (a, b)/* call by value*/
printf(“\n after : a=%d, b=%d”, a, b);
getch();
}
swap(int x, int y)
{
int t;
t= x;
x=y;
y=t;
printf(“\n in function x=%d, y=%d ”, x, y);
}
Output:-
Before :- a=10, b=20
Infunction:- x=20, y=10
After:- a=10, b=20

Call by reference:- While calling the function when add. Of variable are passed to the function when this type
of function called Is call by reference using pointer concept the some memory is shared by the calling &
called function.

/* program for checking a no. for prime no. using function*/


# include<stdio.h>
main()
{
int n;
clrscr ();
printf(“enter the no.”);
scanf(“%d”, &n);
/*checking each no. for prime no. using function*/
if (in prime (n))
printf(“\n %d : prize no.”, n);
else
printf(“\n %d : not prime no.”, n);

- 71 -
Vyankatesha Computech C Notes
getch();
}
/*main()ends */
/*- function for checking a no for prime no
- Accepting a no. is var n
- Function return 1 (true)
{
int I;
if(n==1)/*if no. is 1 then it is prime*/
return (1)
for (i=2; i<n; i++)
{
if(n%i==0)
break;
}
if(n==i)/*if prime no.*/
return (1)/*return true*/
else
return(0); /*return false*/
}
/* passing array element to the function*/
/* program for checking each element of an array for prime no. using function*/
#include <stdio.h>
main()
{
int a[5], i;
clrscr ();
/* reading element of an array*/
for (i=0; i<5; i++)
{
printf(“\n enter a[%d]:”, i);
scanf(“%d”, &a[i]);
}
/*checking each element for prime no. using a function */
for(i=0; i<5; i++)
{
/*passing array element by value */
check _prime(a[i]);
}
getch();
}
Function for checking a no. for prime no. accessing array element in var n*/
check_prime(int n)
{

- 72 -
Vyankatesha Computech C Notes
int i;
for(i=2; i<n; i++)
{
if(n%i==0)
break;
}
if(n%==i)
printf(“\n %d: prime no.”, n);
else
printf(“\n %d : not prime no.”, n);
}
/*program for checking each element array for prime no. using array*/
#include <stdio.h>
main()
{
int a[5],I;
clrscr();
for(i=0; i<5; i++)
{
printf(“enter a[%d]”, i);
scanf(“%d”, &a[i])
if(is prime(a[i])
printf(“\n %d : prime no.”, a[i])
else
printf(“\n%d : not prime no.” a[i]);
getch();
} /*main () ends */
return (1)
is prime (int n)
{
int I;
if (n==1)
return(1)
for(i=2; i<n; i++)
{
if (n%i==0)
break;
}
If (n==i)/* if prime no.*/
return(1); /* return true*/
else
return(0); /*return false*/
}
/* print all the prime no. from 1 to 100 & count total prime no.*/

- 73 -
Vyankatesha Computech C Notes
#include<stdio.h>
main()
{
int n, i, count=0;
clrscr();
printf(“\n prime no within 1 to 100”);
for (n=1; n<=100; n++)
{
for(i=2; i<n; i++)
{
if(n%i==0)
break;
}
if(i==n)
{
printf(“\n%d”, n);
count++;
}
}
printf(“\n total prime no. %d”, count);
getch();
}

- 74 -

You might also like