You are on page 1of 65

Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.1 : Basic Expression Programs


#include<stdio.h>
#include <conio.h> Output
void main()
{ Hello world
hello
clrscr();
printf(“hello world”);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> Output
void main()
{ hello worldhow are you
clrscr();
printf(“hello world”);
printf(“how are you”);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main()
{ Output
clrscr();
hello world
printf(“hello world\n”);
how are you
printf(“how are you”);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main()
{ Output
clrscr();
hello world
printf(“hello world\n”);
printf(“\nhow are you”);
how are you
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------

1
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

#include<stdio.h>
#include <conio.h> Output
void main()
{ hello world how are you
clrscr();
printf(“hello world\t”);
printf(“how are you”);
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------
Write a program to read one number and display it .
#include <stdio.h>
#include<conio.h>
void main() Output
{
5 (user has to input from keyboard)
int n ;
5 (computer will display)
clrscr();
scanf(“%d” , &n);
printf(“%d” , n );
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a program to read one number and display it (with messages)
#include <stdio.h>
#include <conio.h>
void main()
{
int n ; Output
clrscr();
Enter a number : 5
printf(“enter a number : “);
you have entered : 5
scanf(“%d” , &n);
printf(“you have entered : %d” , n);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a program to add two numbers .
#include <stdio.h>
Output
#include <conio.h>
void main() Enter first number : 5
{ Enter second number : 10
int n1,n2,n3 ; Addition =15
clrscr();

2
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

printf(“enter first number : “);


scanf(“%d” , &n1);
printf(“enter second number : “);
scanf(“%d” , &n2);
n3=n1+n2;
printf(“addition=%d”,n3);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

Write a program to multiply two numbers .


#include <stdio.h>
Output
#include <conio.h>
void main() Enter first number : 5
{ Enter second number : 10
int n1,n2,n3 ; Multiplication =50
clrscr();
printf(“enter first number : “);
scanf(“%d” , &n1);
printf(“enter second number : “);
scanf(“%d” , &n2);
n3=n1*n2;
printf(“multiplication=%d”,n3);
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------

Write a program to find area of a rectangle .


#include <stdio.h>
#include <conio.h>
Output
void main()
{ Enter one side : 10
int side1,side2,area; Enter second side : 20
clrscr(); Area of rectangle = 200
printf(“enter one side : “);
scanf(“%d” , &side1);
printf(“enter second side : “);
scanf(“%d” , &side2);
area=side1*side2;
printf(“area of rectangle =%d”,area);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

3
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Write a program to find volume of cube .


#include <stdio.h>
#include<conio.h>
void main()
{
int h , l , b , v;
clrscr();
Output
printf(“enter hight : “);
scanf(“%d” , &h); Enter hight : 10
printf(“enter length : “); Enter length : 3
scanf(“%d” , &l); Enter breadth : 4
printf(“enter breadth : “); Volume of cube= 120
scanf(“%d” , &b);
v=l*h*b;
printf(“volume of cube =%d”,v);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

Write a program to find area of triangle .


#include <stdio.h>
#include<conio.h> Output
void main()
{ Enter height : 5.5
float h , b , a ; Enter base : 6.6
clrscr(); Area of triangle = 18.15000
printf(“enter hight : “);
scanf(“%f” , &h);
printf(“enter base : “);
scanf(“%f” , &b);
a=h*b*0.5;
printf(“area of triangle =%f”, a);
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------

Write a program to calculate simple interest .


#include <stdio.h>
#include<conio.h>
void main() Output
{
float p , r , n , i ; Principle amount : 1000
clrscr(); Rate of interest : 12.5
printf(“principal amount : “); Number of years : 2.5
scanf(“%f” , &p); Interest : 312.500000
printf(“rate of interest : “);

4
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

scanf(“%f” , &r);
printf(“number of years : “);
scanf(“%f” , &n);
i=(p*r*n)/100 ;
printf(“interest =%f”,i);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

Write a program to compute Fahrenheit from centigrade


#include <stdio.h> Output
#include<conio.h>
void main()
{ Enter Temperature in centigrade : 32.5
Float f,c : Temperature in Fahrenheit = 90 .500000
clrscr();
Printf(“Enter . Temperature in Centigrade : “) ;
Scanf (“%f”, &c)
f=1.8*c+32;
Printf(“temperature in fehrenhelt =%f” ,f);
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a program to calculate area of circle .
#include <stdio.h> Output
#include<conio.h>
void main()
{ Enter radius of circle : 10
float r, pi=3.14 , a; Area = 314.000
clrscr();
0printf(“enter radius of circle : “);
scanf(“%f” , &r);
a=pi*r*r ;
printf(“area = %f”,a );
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

5
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.2 : Conditional Programs


(if , if-else , if-else if ladder, switch-case, goto )

Write a program to read marks of a student from keyboard whether the student is pass (if)

#include<stdio.h>
#include <conio.h> Output
void main()
Enter marks : 65
{
Pass
int m ;
Bye
clrscr();
printf (“enter marks :”);
scanf(“%d” , &m); Output
if(m>=35)
{
printf(“pass”); Enter marks : 30
} Bye
printf(“\nbye”);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

Write a program to read marks of a student from keyboard whether the student is pass or fail
(if-else)

#include<stdio.h>
#include <conio.h> Output
void main()
Enter marks : 65
{
Pass
int m ;
Bye
clrscr();
printf (“enter marks :”);
scanf(“%d” , &m);
if(m>=35)
{ Output
printf(“pass”);
Enter marks : 30
}
Fail
else
Bye
{
printf(“fail”);
}

6
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

printf(“\nbye”);
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------

Write a program to read three numbers from keyboard and find out maximum out of these three
(nested if – else )

#include<stdio.h>
#include <conio.h>
void main() Output
{
Three number : 10 20 2
int a , b , c ;
20 is max
clrscr();
printf (“three numbers :”);
scanf(“%d%d%d” , &a,&b,&c);
{
Output
if(a>b)
if(a>c) Three number : 30 2 51
{ 51 is max
printf(“%d is max “ , a);
}
else
{
printf(“%d is max ” ,c);
}
else if(b>c)
{
printf(“%d is max” , b);
}
else
{
printf(“%d is max” , c) ;
}
getch();
}
}

--------------------------------------------------------------------------------------------------------------------------------------

7
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Write a program to read marks from keyboard and your program should display equivalent grade
according to following table . (If – else if ladder)
Marks Grade
100-80 Distinction
60-79 First class
36-59 Second class
0-39 Fail

#include<stdio.h>
#include <conio.h>
void main() Output
{
int m ; Enter marks : 65
clrscr(); First class
printf (“enter marks : ”); Have a nice day
scanf(“%d” , &m);
if(m>=80 && m<=100)
{ Output
printf("distinction”);
} Enter marks : 50
Second class
Have a nice day
else if (m>= 60 && m<80)
{
printf (“first class”);
} Output
else if (m>=35 && m<60)
{ Enter marks : 105
printf(second class “); Invalid Marks
} Have a nice day
else if (m>= 0 && m<35)
{
printf(“fail “) ;
}
else
{
printf (“invalid marks” ) ;
}
printf(“\n have a nice day “);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

8
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Write a program that reads a number from 1 to 7 and accordingly it should display MONDAY To
SUNDAY (switch – case)

#include<stdio.h> Output
#include <conio.h>
void main() Enter day number : 4
{ Thursday
int day ; Have a nice day
clrscr();
{
printf (“enter day number : ”);
scanf(“%d” , &day); Output
switch(day)
{ Enter day number : 10
case 1 : Invalid day number
printf("monday”); Have a nice day
break;
case 2 :
printf("tuesday”);
break;
case 3 :
printf("wednesday”);
break;
case 4 :
printf(“thursday”);
break;
case 5 :
printf("friday”);
break;
case 6 :
printf("saturday”);
break;
case 7 :
printf("sunday”);
break;
default :
printf(“invalid day number”);
}
printf”\n have a nice day”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

9
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Check whether the given number is positive , negative or zero


#include<stdio.h>
#include <conio.h>
void main()
{
int n ;
clrscr();
printf (“enter any number : ”);
scanf(“%d” , &n);
if(n>0) ,printf (“%d is positive” , n);-
else if (n<0) {printf (“%d is negative “,n);-
else {printf(“%d is zero “,n) ;-
getch();
}

Output

Enter any number : 5


5 is positive

Output

Enter any number : -220


-220 is negative

Output

Enter any number : 0


0 is zero

--------------------------------------------------------------------------------------------------------------------------------------

Write a simple calculator (using if . . . else if )


Output
#include<stdio.h>
#include <conio.h> Enter First number : 20
void main() Enter Second number : 4
{ Operation (+ , - , * , / ) : +
char op ; Result : 24.000000
float num1, num2, res ;
Output
printf(“enter first number : “);
scanf(“%f” , &num1); Enter First number : 20
printf(“enter second number : “) ; Enter Second number : 4
Operation (+ , - , * , / ) : -
Result : 16.000000 10
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

scanf(“%f” , &num2);
printf(“operation (+ , - , * , / ) : “ );
fflush(stdin);
scanf(“%c” , &op); Output
if(op== ’+’)res=num1+num2;
else if (op == ’-‘)res=num1-num2; Enter First number : 20
else if (op== ‘*’)res=num1*num2; Enter Second number : 4
else if (op== ‘/‘)res=num1/num2; Operation (+ , - , * , / ) : *
else{printf(“invalid choice”);} Result : 80.000000
printf(“result : %f” , res);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a simple calculator (using switch - case)

#include<stdio.h>
#include <conio.h>
void main()
{ Output
char op ;
float num1, num2, res ; Enter First number : 20
printf(“enter first number : “); Enter Second number : 4
scanf(“%f” , &num1); Operation (+ , - , * , / ) : +
printf(“enter second number : “) ; Result : 24
scanf(“%f” , &num2);
printf(“operation (+ , - , * , / ) : “ );
fflush(stdin);
scanf(“%c” , &op);
switch(op)
{
case ‘ + ‘ :
res=num1+num2;
break;
case ‘ - ‘ :
res=num1-num2;
break;
case ‘ * ‘ :
res=num1*num2;
break;
case ‘ / ‘ :
res=num1/num2;
break;
default :
printf(“invalid choice . .”);
return;

11
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

}
printf(“result : %f” , res );
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------
Write a program to find out given year , which is leap or not . A leap year is a year which is evenly
divisible by 4 , but if it is evenly divisible by 100 then it Is not a leap year , but if it evenly
divisible by 400 then it is a leap year .
#include<stdio.h>
#include <conio.h>
void main()
{
int year ;
clrscr();
printf(“enter a year : “);
scanf(“%d” , &year);
if( (year%4==0) )
{
printf(“%d is leap year” , year);
}
else
{
printf(“%d is not leap year” , year);
}
getch();
}

Output Output

Enter any year : 1996 Enter any year : 1986


1996 is leap year 1986 is not leap year

Output Output

Enter any year : 2000 Enter any year : 1900


2000 is leap year 1900 is not leap year

--------------------------------------------------------------------------------------------------------------------------------------

12
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.3 : Loops


To read any five number and print the average value
#include<stdio.h>
#include <conio.h> Dry run
void main()
{ I num Sum=0
int num , sum = 0 ;
1 50 0+50=50
int i ;
2 60 50+60=110
printf(“enter five number : “ ); 3 40 110+40=150
for(i=1; i<=5; ++i ) 4 80 150+80=230
{ 5 70 230+70=300
scanf(“%d” , &num); 6 Loop will terminate
sum=sum+num;
Output
}
printf(“average = %d” , sum/5 ); Enter five numbers : 50 60
getch(); 40 80 70
}
Average = 60

--------------------------------------------------------------------------------------------------------------------------------

Write a program to find out all the numbers divisible by 5 and 7 between 1 to 100 .
#include<stdio.h>
#include <conio.h> Output
void main()
{ 35
70
int i ;
clrscr();
for(i=1 ; i<=100 ; ++i )
{
if(i % 5 ==0 && i % 7 ==0 )
{
printf(“ %d \ n” , i );
}
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

13
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Find sum of digits of given numbers .


#include<stdio.h>
#include <conio.h> Dry run
void main()
Num num % 10 Sum=0
{
int num , sum = 0; 12345 5 0+5=5
clrscr(); 1234 4 5+4=9
printf(“enter a number : ”); 123 3 9+3=12
scanf(“%d” , & num); 12 2 12+2=14
while(num>0) 1 1 14+1=15
{ 0 Loop will terminate
sum+=num%10;
Output
num/=10;
} Enter a number : 1 2 3 4 5
printf(“sum of digit = %d” , sum ); Sum of digit = 15
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

To Find out the total number of odd digits within the given number and print the sum of all odd
digits .
#include<stdio.h>
#include <conio.h> Dry run
void main() N n % 10 Sum=0
{
int n , sum = 0 , digit ; 12345 5 0+5=5
clrscr(); 1234 4
printf(“enter the number : ”); 123 3 5+3=8
scanf(“%d” , &n ); 1 2 2
while(n > 0) 1 1 8+1=9
0 Loop will terminate
{
digit = n %10;
if (digit % 2 ! = 0)
{
sum = sum+digit; Output
}
Enter a number : 1 2 3 4 5
n=n / 10 ;
Sum of odd digit.= 9
}
printf(“addition of odd digital = %d” , sum );
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------

14
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Reserve a number

#include<stdio.h>
#include <conio.h> Dry run
void main() num num Sum=0
{ % 10
clrscr(); 12345 5 (0*10)+5=5
long int num , sum =0 ; 1234 4 (5*10)+4=54
printf(“enter a number : ”); 123 3 (54*10)+3=543
scanf(“% ld” , &num ); 12 2 (543*10)+2=5432
while(num > 0) 1 1 (5432*10)+1=54321
0 Loop will terminate
{
sum=sum*10+num%10;
num/ =10; Output
}
Enter a number : 1 2 3 4 5
printf(“reverse = % ld” ,sum-;
Reverse : 54321
getch();
}

--------------------------------------------------------------------------------------------------------------------------------------

Find out sum of first and last digit of a given number .


#include<stdio.h>
#include <conio.h> Dry run
void main() Num sum=0 Remark
{
int num , sum =0 ; 12345 0+5=5 Before loop
clrscr(); 1234
printf(“enter a number : ”); 123
12
scanf(“% d” , &num );
1 Loop will terminate
sum=sum+num%10;
1 5+1=6 After loop
while(num > 9) {num/=10;}
sum=sum+num; Output
printf(“sum of first and last “);
digit=”%d”,sum); Enter a number : 1 2 3 4 5
getch(); Sum of first and last digit = 6
}

--------------------------------------------------------------------------------------------------------------------------------------

15
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

WAP to find out largest digit in an given integer e.g. 1 2 3 4 , 5 is the greatest

#include<stdio.h> Dry run


#include <conio.h>
void main() Num num=0 max=0
{
int num , max =0 ; 3452 2 2
clrscr(); 345 5 5
34 4 5
printf(“enter a number : ”);
3 3 5
scanf(“% d” , &num );
0 Loop will terminate
while(num>0)
{
if(num%10 > max)
Output
{
max=num%10; Enter a number : 3 4 5 2
} Largest digit = 5
num=num/10;
}
printf(“largest digit =%d” , max);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
WAP to calculate average and total of 5 students for 3
subject (Use 2 for loops ) .
#include<stdio.h>
#include <conio.h>
void main()
{
Dry run
int i , j , sum , m ;
clrscr(); Enter three marks for student # 1 : 50 60 70
for(i=1; i<=5; ++i) Average = 60
{ Enter three marks for student # 2 : 40 50 60
sum=0; Average = 50
printf(“enter three marks for student # %d : ” , i); Enter three marks for student # 3 : 55 65 75
for(j=1; j<=3; ++j ) Average = 65
{ Enter three marks for student # 4 : 20 30 40
scanf(“%d” , &m); Average = 30
sum=sum+m; Enter three marks for student #5 : 45 50 55
} Average = 50
printf(“average = %d\n” , sum/3);
}
getch();
}
------------------------------------------------------------------------------------------------------------------------------------

Read five persons height and weight and count the number of persons having greater than 170
and weight less than 50.

16
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

#include<stdio.h>
#include <conio.h>
void main()
{
float h , w ;
int count =0 , i;
for(i=1; i<=5 ; ++i)
{
printf(“enter height : “);
scanf(“%f” ,&h);
printf(“enter weight : “);
scanf(“%f” ,&w);
if(h>=170 &&w<=50 )
{
count ++;
}
printf(“total persons = % d ”, count );
}
getch(); Dry run
}
Enter Height : 165
Dry run Enter Weight : 60
Enter Height : 180
I h W count=0 Enter Weight : 45
1 165 60 0 Enter Height : 177
2 180 45 1 Enter Weight : 40
3 177 40 2 Enter Height : 150
4 150 30 2 Enter Weight : 30
Enter Height : 156
5 156 55 2
Enter Weight : 55
6 Loop will terminate
Total person = 2
--------------------------------------------------------------------------------------------------------------------------------------
Check whether the given number is perfect of not. A number is perfect if its sum of digit is its sum
of digit is name as multiplication of digits
#include<stdio.h> Dry run
#include <conio.h>
num sum=0 mul=1
123 0+3=3 1*3=3
void main() 12 3+2=5 3*2=6
{ 1 5+1=6 6*1=6
int num , sum=0 , mul =1; Output 0 Loop will terminate
clrscr();
printf(“enter a number : )” Enter a number :123
scanf(“%d” , &num); Perfect
while(num>0)
{

17
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

sum+num%10;
mul*=num%10;
Dry run
num/=10;
num sum=0 mul=1
}
123 0+5=5 1*5=5
if(mul = =sum) 12 5+6=11 5*6=30
{printf(“perfect”) ; - 1 11+3=14 30*3=90
else {printf(“not perfect”);- Output 0 Loop will terminate
printf(“\n”);
getch(); Enter a number : 365
} Not Perfect

-------------------------------------------------------------------------------------------------------------------------------------
Check for Armstrong number , A number is Armstrong if sum of cube of every digit is same as the
original number .
3 3 3
E.g. 153 = 1 + 5 + 3 = 153
#include<stdio.h>
Output
#include <conio.h>
void main() Enter a number : 153
{
Armstrong
int num , sum=0 , temp;
clrscr();
printf(“enter a number : )”
scanf(“%d” , &num); Dry run Temp=153
temp=num;
num num Sum=0
while(num>0)
%10
{
153 3 0+(3*3*3)=27
sum=sum+( (num%10) * (num%10) * (num%10) );
15 5 27+(5*5*5)=152
num=num/10;
1 1 152+(1*1*1)=153
}
0 Loop will terminate
if(sum = = temp)
{ Dry run Temp=123
printf(“armstrong”) ;
} num num Sum=0
else %10
{ 153 3 0+(3*3*3)=27
printf(“not Armstrong”); 15 5 27+(5*5*5)=152
} 1 1 152+(1*1*1)=153
getch(); Output 0 Loop will terminate
}
Enter a number : 123
Not Armstrong

18
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.4 : Series


1+2+3+………+n
Dry run
#include<stdio.h>
#include <conio.h> i Sum=0
void main()
{ 1 0+1=1
int n , i , sum=0; 2 1+2=3
3 3+3=6
clrscr();
4 6+4=10
printf(“1+2+3+ . . . +n enter n : “) ;
5 10+5=15
scanf(“%d” , &n); 6 Loop terminate

for(i=1; i<=n; i++)


{
sum=sum+i;
Output
}
printf(“sum=%d” , sum); 1+2+3+ . . . +n Enter n : 5
getch(); Sum = 15
}
--------------------------------------------------------------------------------------------------------------------------------------

2+4+6+………+n
Dry run
#include<stdio.h>
#include <conio.h> i Sum=0
void main()
{ 2 0+2=2
int n , i , sum=0; 4 2+4=6
6 6+6=12
clrscr();
8 12+8=20
printf(“2+4+6+ . . . +n enter n : “) ;
10 20+10=30
scanf(“%d” , &n); 12 Loop terminate

for(i=2; i<=n; i=i+2)


{
Output
sum=sum+i;
} 2+4+6+ . . . +n Enter n : 10
printf(“sum=%d” , sum); Sum = 30
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

19
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

1+3+5+………+n
Dry run
#include<stdio.h>
#include <conio.h> i Sum=0
void main()
{ 1 0+1=1
int i , n , sum=0; 3 1+3=4
5 4+5=9
clrscr();
7 9+7=16
printf(“1+3+5+ . . . +n enter n : “) ;
9 16+9=25
scanf(“%d” , &n); 11 Loop terminate

for(i=1; i<=n; i=i+2)


{
Output
sum=sum+i;
} 1+3+5+ . . . +n Enter n : 10
printf(“sum=%d” , sum); Sum = 25
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

12 + 22 + 32 + 42 ……… +n2
#include<stdio.h>
Dry run
#include <conio.h>
void main() i Sum=0
{
int n , i , sum=0; 1 0+(1*1)=1
clrscr(); 2 1+(2*2)=5
printf(“1^2+2^2+3^2+4^2 . . . . . +n^2 enter n : “) ; 3 5+(3*3)=14
scanf(“%d” , &n); 4 14+(4*4)=30
5 terminate
for(i=1; i<=n; i++)
{sum=sum+(i*i);
Output
}
printf(“sum=%d” , sum); 1^2+2^2+3^2+4^2 . . . . . +n^2 Enter n : 4
getch(); Sum = 30
}

--------------------------------------------------------------------------------------------------------------------------------------

20
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

1+4-9+16-25+36 . . . . . +n2
Dry run
#include<stdio.h> n=4
#include <conio.h> i k=1 n=4
Sigh=1
void main() 2 1+(2*2)*(1)=5 -1
{ 3 5+(3*3)*(-1)=-4 1
4 -4+(4*4)*(1)=12 -1
int n , i , sum=1, sign=1;
5 Loop will terminate
clrscr();
printf(“enter n : “) ;
scanf(“%d” , &n);
for(i=2; i<=n; i++) Output
{sum=sum+(i*i)*sign;
Enter n : 4
sign=sign*-1;
Sum = 12
}
printf(“sum=%d” , sum);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

1! +2!+3!+4!+ .....n!

#include<stdio.h> Dry run


n=4
#include <conio.h> i k=1 n=4
Sigh=1
void main() 1 1*1=1 0+1=1
{ 2 1*2=2 1+2=3
int n , i , sum=0, k=1; 3 2*3=6 3+6=9
4 6*4=24 9+24=33
printf(“enter n : “) ;
5 Loop will terminate
scanf(“%d” , &n);
for(i=1; i<=n; i++)
{k=k*i;
sum=sum+k; Output
}
printf(“sum=%d” , sum); Enter n : 4
getch(); Sum = 33
}

--------------------------------------------------------------------------------------------------------------------------------------

21
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.5 : Patterns

#include<stdio.h>
#include <conio.h> Dry run
Output n=5
void main() i j
{ * 1 1
int i , j ,n=5; ** 2 1,2
clrscr(); *** 3 1,2,3
4 1,2,3,4
For(i=1; i<=n; ++i) ****
5 1,2,3,4,5
{ ***** 6 Loop terminates
for(j=1; j<=i; ++j)
{
printf(“*“) ;
}
printf(“\n”);
}
getch();
}

--------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include <conio.h>
void main()
{ Dry run
Output n=5
int i , j ,k , n=5;
clrscr(); i k J
* 1 1,2,3,4 1
For(i=1; i<=n; ++i)
** 2 1,2,3 1,2
{
*** 3 1,2 1,2,3
for(k=1; k<=n-i; ++k) 4 1 1,2,3,4
****
{ 5 - 1,2,3,4,5
*****
printf(“ “) ; 6 Loop terminates
}
for(j=1; j<=i; ++j)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}

--------------------------------------------------------------------------------------------------------------------------------------

22
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

#include<stdio.h>
#include <conio.h>
void main()
{
int i , j , n=5;
Output Dry run
clrscr();
n=5
for(i=1 ; i<=n; ++i) 1 i J
{ 12 1 1
for(j=1 ; j<=i; ++j) 123 2 1,2
{ 1234 3 1,2,3
printf(“%d”,j); 12345 4 1,2,3,4
} 5 1,2,3,4,5
printf(“\n”); 6 Loop terminates
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

Output
#include<stdio.h>
#include <conio.h> 1
void main() 12
{ 123
int i , j ,k , n=5; 1234
clrscr(); 12345
for(i=1; i<=n; ++i)
Dry run
{ n=5
for(k=1; k<n-i ; ++k ) i k J
{ 1 1,2,3,4 1
printf(“ ”); 2 1,2,3 1,2
} 3 1,2 1,2,3
for(j=1 ; j<=i ; ++j) 4 1 1,2,3,4
5 - 1,2,3,4,5
{
6 Loop terminates
printf(“%d”, j );
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

23
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

#include<stdio.h>
#include <conio.h> Dry run
n=5
void main()
i J
5 1,2,3,4,5
{
4 1,2,3,4
int i , j ,n=5; Output 3 1,2,3
clrscr(); 2 1,2
for(i=n; i>=1 ; - -i) 1 1
{ $$$$$ 0 Loop terminates
for(j=1; j<=i ;++j) $$$$
{printf(“$”); $$$
} $$
printf(“\n”) ; $
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include<conio.h>
void main()
{ Dry run
output n=5
Int i,j,k,n=5 ;
clrscr(); $$$$$ I K J
for (i=n; i>=1; - -i) 5 - 1,2,3,4,5
$$$$
4 1 1,2,3,4
{ $$$
3 1,2 1,2,3
for(k=1 ; k<=n-i ; ++k) Output $$
2 1,2,3 1,2
{ $ 1 1,2,3,4 1
Printf(“ “) ; 0 Loop terminates
}
for (j=1 ; j<=i ; ++j)
,printf(“$”) ;-
Printf(“\n”) ;
}
getch();
}
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h> output Dry run n=5
#include <conio.h>
void main() i J
12345
12345
{ 5 1,2,3,4,5
1234
1234
int i , j ,n=5; 4 1,2,3,4
123
123 3 1,2,3
clrscr();
12
12 2 1,2
for(i=n; i>=1; - -i)
11 1 1
{
0 Loop terminates
for(j=1; j<=i ;++j)

24
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

{
printf(“ %d ”,j);
}
printf(“\n”) ;
}
getch();
}

-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h> Dry run
n=5
#include<conio.h>
I K J
void main()
5 - 5,4,3,2,1
{
4 1 4,3,2,1
int i , j , k, n=5; 3 1,2 3,2,1
clrscr(); 2 1,2,3 2,1
for(i=n; i>=1 ; - -i) 1 1,2,3,4 1
{ 0 Loop terminates
for(k=1; k<=n-i ;++k)
{
printf(“ ”) ; Out put
}
for(j=i; j>=1 ; - -j) 54321
{ 4321
printf(“%d”,j) ; 321
} 21
printf(“\n”); 1
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main() output
{
1 Dry run
int i , j , ,n=5; n=5
21
clrscr();
321 I J
for(i=1; i<=n ; ++i) 1 1
4321
{ 2 2,1
54321
for(j=1; j>=1 ; - -j) 3 3,2,1
{ 4 4,3,2,1
printf(“ %d ”, j); 5 5,4,3,2,1
} 6 Loop terminates
printf(“\n”);
}
getch();

25
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

-------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include <conio.h>
void main()
{
int i , j ,k,n=5; Dry run n=5
clrscr(); I k J
for(i=n; i>=1; - -i) 1 1,2,3,4 5
{ 2 1,2,3 4,5
for(k=1; k<i ; ++k) 3 1,2 3,4,5
4 1 2,3,4,5
{
5 - 1,2,3,4,5
Printf(“ “) ; output
0 Loop terminates
}
for(j=n; j<=i ; --j) 5
{ 45
printf(“ %d”, j); 345
} 2345
printf(“ \n” ); 12345
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main() Dry run n=5
{ output
I j
int i , j , n=5;
Clrscr(); 12345 1 1,2,3,4,5
for(i=1; i<=n ; ++i) 2345 2 2,3,4,5
{ 345 3 3,4,5
for(j=i; j<=n ; ++j) 45 4 4,5
{ 5 5
5
printf(“ %d ”,j); 6 Loop terminates
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
output
#include <conio.h>
void main()
{ 54321
int i , j ,k,n=5; 5432
Clrscr();
543
for(i=1; i<=n ; ++i)
54
5
26
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

{
for(k=1 ; k<i ; ++k)
{
printf(“ ”);
}
for(j=n; j>=i; - -j)
{
printf(“%d”,j) ; Dry run n=5
} I k J
printf(“ \n” ); 1 - 5,4,3,2,1
}
2 1 5,4,3,2
getch();
3 1,2 5,4,3
}
4 1,2,3 5,4
5 1,2,3,4 5
6 Loop terminates

--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main()
Dry run n=5
{ Output
int i , j ,n=5; i J
clrscr(); 5 5 5
for(i=n; i>=1 ; - -i) 45 4 4,5
{ 3 3,4,5
345
for(j=i ; j<=n ; ++j) 2 2,3,4,5
{ 2345
12345 1 1,2,3,4,5
printf(“ %d”, j );
} 0 Loop terminates
printf(“ \n” );
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main() Dry run n=5
{ i k J
int i , j , k ,n=5;
5 1,2,3,4 5
clrscr();
4 1,2,3 5,4
for(i=n; i>=1 ; - -i)
{ output 3 1,2 5,4,3
for(k=1; k<i; ++k) 2 1 5,4,3,2
,printf(“ ”); 1 - 5,4,3,2,1
} 5
0 Loop terminates
for(j=n; j>=i; - -j) 54
{ 543
printf(“ %d” , j ); 5432
} 54321
printf(“\n”);

27
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

}
getch();
}
_________________________________________________________________________________

#include<stdio.h>
#include <conio.h> Output
void main()
{ 55555
int i , j , n=5; Dry run n=5
4444
clrscr(); I J
333
for(i=n; i>=1 ; - -i) 5 1,2,3,4,5
{ 22 4 2,3,4,5
for(j=1; j<=i; ++j) 1 3 3,4,5
{ 2 4,5
printf(“ %d” , i ); 1 5
} 6 Loop terminates
printf(“\n”);
}
getch();
}

--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> output
void main()
{ 55555
int i , j , k ,n=5; 4444
clrscr();
333
for(i=n; i>=1 ; - -i)
{ 22
for(k=1; k<=n-i ; ++k) 1
{
printf(“ ”);
} Dry run n=5
for(j=1; j<=i; ++ j) i k J
{ 5 - 1,2,3,4,5
printf(“%d”, i ); 4 1 1,2,3,4
} 3 1,2 1,2,3
printf(\n”) 2 1,2,3 1,2
} 1 1,2,3,4 1
getch(); 0 Loop terminates
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main()
{
int i , j ,n=5;

28
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

clrscr();
for(i=n; i>=1 ; - -i)
{
Dry run n=5
for(j=1; j<=i ; ++j) I j
{ output 1 1,2,3,4,5
printf(“ %d” , i ); 2 2,3,4,5
} 55555
3 3,4,5
4444 4 4,5
printf(“\n”); 333 5 5
} 22 6 Loop terminates
getch();
1
}

-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main() output
{
Dry run n=5
int i , j , k ,n=5;
11111 I K J
clrscr();
for(i=1; i<=n ; - ++i) 2222 5 1,2,3,4 5
{ 333 4 1,2,3 4,5
for(k=1; k<i ; ++k) 44 3 1,2, 3,4,5
{ 5 2 1 2,3,4,5
printf(“ ”); 1 - 1,2,3,4,5
} 0 Loop terminates
for(j=1; j<=n-i+1; ++ j)
{
printf(“%d”,i );
}
printf(\n”)
}
getch();
}

-----------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main() Output Dry run n=5
{
int i , j , n=5; i j
5
clrscr(); 1 5
44 2 4,5
for(i=n; i>=1; - -i)
{ 333 3 3,4,2
for(j=i; j<=n ; ++ j) 2222 4 2,3,4,5
{ 11111 5 1,2,3,4,5
printf(“%d”, i); 6 Loop terminates
}

29
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

printf(“\n”);
}
getch();
}
__________________________________________________________________________________

#include<stdio.h>
#include <conio.h> Dry run n=5
i J
void main() 1 1
{ Output 2 1,2
int i , j , k ,n=5; 3 1,2,3
clrscr(); 5 4 1,2,3,4
for(i=n; i>=1 ; - - i) 44 5 1,2,3,4,5
{
333 6 Loop terminates
for(k=1; k<i ; ++k)
{ 2222
printf(“ ”); 11111
}
for(j=i; j<=n; ++ j)
{
printf(“%d”,i );
}
printf(\n”)
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h> Output
#include <conio.h> Dry run n=5
void main() 1 i k J
{ 22 1 1,2,3,4, 1
int i , j , n=5; 2 1,2,3 1,2
333
clrscr(); 3 1,2 1,2,3
for(i=1; i<=n ; ++i) 4444
4 1 1,2,3,4
{ 55555
5 - 1,2,3,4,5
for(j=1; j<=i; ++ j) 6 Loop terminates
{
printf(“%d ”, i);
}

printf(“\n”);
}
getch();
}
_________________________________________________________________________________

30
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

#include<stdio.h>
#include <conio.h> Output
void main()
{
1
int i , j , k ,n=5;
clrscr(); 22
for(i=1; i<=n ; ++i) 333
{ 4444
for(k=1; k<=n-i ; ++k) 55555
{
Dry run n=5
printf(“ ”);
} i k J
for(j=1; j<=i; ++ j) 1 1,2,3,4, 1
{ 2 1,2,3 1,2
printf(“ %d” , i ); 3 1,2 1,2,3
} 4 1 1,2,3,4
printf(“\n”); 5 - 1,2,3,4,5
} 6 Loop terminates
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include <conio.h>
Output Dry run n=5
void main()
{ i j
int i , j , n=5; 1 1 1
clrscr(); 1 4 2 1,2
for(i=1; i<=n ; ++i) 1 4 9 3 1,2,3
{ 1 4 9 16 4 1,2,3,4
for(j=1; j<=i ; ++j) 5 1,2,3,4,5
1 4 9 16 25
{
6 Loop terminates
printf(“ %3d” , j* j );
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include <conio.h> Dry run n=5
void main()
I k J
{
1 1,2,3,4 1
int i , j , k ,n=5;
clrscr(); 2 1,2,3 1,2
for(i=1; i<=n ; ++i) 3 1,2 1,2,3
{ 4 1 1,2,3,4
for(k=1; k<=n-i ; ++k) 5 - 1,2,3,4,5
{ 6 Loop terminates

31
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

printf(“ ”); /* three (3) spaces*/


}

for(j=1; j<=i; ++j)


,printf(“ %3d” , j * j );
}
printf(“\n”);
}
getch(); Output
}
1
1 4
1 4 9
1 4 9 16
1 4 9 16 25

--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main() Output Dry run n=5
{
int i , j , k ,n=5; I k J
1
clrscr(); 1 1,2,3,4 1
2 2
for(i=1; i<=n ; ++i) 2 1,2,3 1,2
{ 3 3 3 3 1,2 1,2,3
for(k=1; k<=n-i ; ++k) 4 4 4 4 4 2 1,2,3,4
{ 5 5 5 5 5 5 - 1,2,3,4,5
printf(“ ”); 6 Loop terminates
}
for(j=1; j<=i; ++j)
{
printf(“ %d” , i ); /* one (1) spaces after %d*/
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h> Output
#include <conio.h>
Dry run n=5
void main() 5
4 4 I k J
{ 5 1,2,3,4 5
3 3 3
int i , j , k ,n=5; 4 1,2,3 5,4
2 2 2 2
clrscr(); 3 1,2 5,4,3
for(i=n; i>=1; - -i) 1 1 1 1 1 2 2 5,4,3,2
{ 1 - 5,4,3,2,1
for(k=1; k<i ; ++k) 0 Loop terminates
{

32
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

printf(“ ”);
}
for(j=n; j>=i; - -j)
{
printf(“ %d” ,i ); /* one (1) spaces after %d*/
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
include<stdio.h>
#include <conio.h>
void main() Output

{ a
Char ch =’a’ -1; a b
int i , j , k ,n=5; a b c
clrscr(); a b c d
for(i=1; i<=n ; ++i) a b c d e
{
for(k=1; k<=n-i ; ++k)
{
printf(“ ”); Dry run n=5
}
I k J
for(j=1; j<=i; ++j)
5 1,2,3,4 5
{
4 1,2,3 5,4
printf(“ %c” ,ch+j ); /* one (1) spaces after %c*/
} 3 1,2 5,4,3
printf(“\n”); 2 2 5,4,3,2
} 1 - 5,4,3,2,1
getch(); 0 Loop terminates
}
--------------------------------------------------------------------------------------------------------------------------------------
include<stdio.h>
#include <conio.h>
Dry run n=5
void main() Output
I k J
{ 1 1,2,3,4 5
*
int i , j , k ,n=5; 2 1,2,3 5,4
* * 3 1,2 5,4,3
clrscr();
for(i=1; i<=n ; ++i) * * * 4 2 5,4,3,2
{ * * * * 5 - 5,4,3,2,1
for(k=1; k<=n-i ; ++k) * * * * * 6 Loop terminates
{
printf(“ ”);
}
for(j=1; j<=i; ++j)
,printf(“ * ”); /* one (1) spaces after ‘*’ */
}
printf(“\n”);

33
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main()

{
Char ch =’a’-1
int i , j , k ,n=5;
for(i=n; i>=1 ; - -i) Output
{
for(k=1; k<=n-i ; ++k) a b c d e
{ a b c d
printf(“ ”);
a b c
}
a b Dry run n=5
for(j=1; j<=i; ++j)
{ a I k J
printf(“ %c” , ch+j ); /* one (1) . space after %d*/ 5 - 1,2,3,4,5
} 4 1 1,2,3,4
printf(“\n”); 3 1,2 1,2,3
getch(); 2 1,2,3 1,2
} 1 1,2,3,4 1
0 Loop terminates

--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
Dry run n=5
void main() Output
I k J
{ 5 - 1,2,3,4,5
int i , j , k ,n=5; * * * * * 4 1 1,2,3,4
clrscr(); * * * * 3 1,2 1,2,3
for(i=n; i>=1 ; - -i) * * * 2 1,2,3 1,2
{ * * 1 1,2,3,4 1
for(k=1; k<=n-i ; ++k) * 0 Loop terminates
{
printf(“ ”);
}
for(j=1; j<=i; ++j)
,printf(“ * ”); /* one (1) spaces after ‘*’ */
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main()

34
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

{
Char ch =’a’-1;
int i , j , n=5; Output
Dry run n=5
clrscr();
for(i=1; i<=n ; ++i) I J
AAAAA
{ 1 1,2,3,4,5
BBBB 2 1,2,3,4
for(j=1; j<=n-i+1; ++j)
,printf(“ %c” , ch+i ); CCC 3 1,2,3
} DD 4 1,2
printf(“\n”); E 5 1
} 6 Loop terminates
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
Dry run n=5
void main() Output
I k J
{ 1 - 1,2,3,4,5
AAAAA 2 1 1,2,3,4
Char ch =’a’-1;
int i , j , k ,n=5; BBBB 3 1,2 1,2,3
clrscr(); CCC 4 1,2,3 1,2
for(i=1; i<=n; ++i) DD 5 1,2,3,4 1
{ E 6 Loop terminates
for(k=1; k<i ; ++k)
{
printf(“ ”);
}
for(j=1; j<=n-i+1; ++j)
,printf(“ %c” , ch+i );
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> Output Dry run n=5
void main()
I k
{ E 5 -
Char ch =’A’-1; DD 4 1
int i , j , n=5; CCC 3 1,2
clrscr(); BBBB 2 1,2,3
for(i=n; i>=1 ; - -i) AAAA 1 1,2,3,4
{ 0 Loop terminates
for(j=1; j<=n-i+1; ++j)
{
printf(“ %c” , ch+i );
}

35
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
Dry run n=5
void main()
I k
{ Output 5 1,2,3,4,5
Char ch =’A’-1; 4 1,2,3,4,
int i , j , n=5; 3 1,2,3
ABCDE
clrscr(); 2 1,2
ABCD
for(i=n; i>=1 ; - -i) 1 1
{ ABC 0 Loop terminates
for(j=1; j<=i; ++j) AB
,printf(“ %c” , ch+j); A
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> Output Dry run n=5
void main()
I k
A 1 1
{
Char ch =’A’-1; AB 2 1,2
int i , j , n=5; ABC 3 1,2,3
clrscr(); ABCD 4 1,2,3,4
for(i=1; i<=n ; ++i) ABCDE 5 1,2,3,4,5
{ 6 Loop terminates
for(j=1; j<=i; ++j)
,printf(“ %c” , ch+j);
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h> Output
#include <conio.h>
void main()
{ A
Char ch =’A’-1; BA
int i , j ,K, n=5; CBA
clrscr(); DCBA
for(i=1; i<=n ; ++i) EDCBA
{

36
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

for(k=1; k<=n-i ; ++k) Dry run n=5


{ I K J
printf(“ ”); 1 1,2,3,4, 1
} 2 1,2,3 2,1
for(j=i; j>=1; - -j) 3 1,2 3,2,1
,printf(“ %c” , ch+j); 4 1 4,3,2,1
} 5 - 5,4,3,2,1
printf(“\n”); 6 Loop terminates
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> Output Dry run n=5
void main()
I k
{ EDCBA 5 5,4,3,2,1
Char ch =’A’-1; EDCB 4 4,3,2,1
int i , j , n=5; EDC 3 3,2,1
clrscr(); ED 2 2,1,
for(i=n; i>=1 ; - -i) E 1 1
{ 0 Loop terminates
for(j=i; j>=i; - -j)
{
printf(“ %c” , ch+j);
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> Output
void main()
Dry run n=5
ABCDE I K J
{
Char ch =’A’-1; BCDE 1 - 1,2,3,4,5
int i , j ,K, n=5; CDE 2 1 1,2,3,4
clrscr(); DE 3 1,2 1,2,3
for(i=1; i<=n ; ++i) E 4 1,2,3 1,2,
{ 5 1,2,3,4 1
for(k=1; k<i ; ++k) 6 Loop terminates
{
printf(“ ”);
}
for(j=i; j<=n; ++j)
,printf(“ %c” , ch+j);
}
printf(“\n”);
}

37
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> Output
void main()
{ E
Char ch =’A’-1;
ED
int i , j , n=5;
clrscr(); EDC
EDCB Dry run n=5
for(i=n; i>=1 ; - -i)
{ EDCBA I J
for(j=n; j>=i; - -j) 5 5
,printf(“ %c” , ch+j); 4 5,4
} 3 5,4,3
printf(“\n”); 2 5,4,3,2
} 1 5,4,3,2,1
getch(); 0 Loop terminates
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> Output Dry run n=5
void main()
{ I K J
Char ch =’A’-1; E 5 1,2,3,4,5 5
int i , j ,K, n=5; DE 4 1,2,3,4 4,5
clrscr(); CDE 3 1,2,3 3,4,5
for(i=n; i>=1 ; - -i) BCDE 2 1,2 2,3,4,5
{ ABCDE 1 1 1,2,3,4,5
for(k=1; k<i ; ++k) 0 Loop terminates
{
printf(“ ”);
}
for(j=i; j<=n; ++j)
,printf(“ %c” , ch+j);
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main() Dry run n=5
{ Output
I k J
Char ch =’A’-1;
1 1,2,3,4 1
int i , j , k ,n=5; A 2 1,2,3 1,2
clrscr();
A B 3 1,2 1,2,3
for(i=1; i<=n ; ++i)
A B C 4 1 1,2,3,4
{
for(k=1; k<=n-i ; ++k) A B C D 5 - 1,2,3,4,5
A B C D E 6 Loop terminates

38
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

{
printf(“ ”);
}
for(j=1; j<=i; ++j)
,printf(“ %c”,ch+j ); } /* one (1) spaces after %c */
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h> Output
void main() A
char ch; A C E
int i,j,s,k,n; A C E G I
clrscr(); A C E G I K M
A C E G I K M O Q
printf(“enter a character :”) ;
A C E G I K M
scanf(“%c” ,&ch) ; A C E G I
ch=ch-1 ; A C E
printf(“enter total number of lines : “); A
scanf(“%d” ,&n) ;
for(i=1; i<=n; ++i) /* upper half */
{ Dry un n=5
for(s=1 ; s<=n-i ; ++s)
I S J K
{
printf(“ “) ; /* Two space * / 1 1,2,3,4 1 1
} 2 1,2,3 1,2,3 1,3,5
for (j=1,k=1;j<=2*i-1; ++j, k=k+2) 3 1,2 1,2,3,4,5 1,3,5,7
{ 4 1 1,2,3,4,5,6,7 1,3,5,7,9
printf(“%2c” ,ch+k) ; 5 - 1,2,3,4,5,6,7,8,9 1,3,5,7…….17
6 Loop terminates
}
printf(“\n”) ;
Dry un n=5
}
for(i=n-1; i>=1; - -i); /*Lower half*/ I S J K
{ 4 1 1,2,3,4,5,6,7 1,3,5,7,9
for(s=1; s<=n-i;++s) 3 1,2 1,2,3,4,5 1,3,5,7
{ 2 1,2,3 1,2,3 1,3,5
printf(“ “) ; /* two spaces */ 1 1,2,3,4 1 1
} 0 Loop terminates
for(j=1 ,k=1 ; j<=2*i-1; ++j, k=k+2)
{
printf(“%2c” ,ch+k);
}
printf(“\n”);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

39
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

#include<stdio.h> Output
#include <conio.h>
void main()
{ Z
char ch =’Z’+1; Z Y
int i , j , k ,n=5; Z Y X
clrscr(); Z Y X W
for(i=1; i<=n ; ++i)
Z Y X W V
{
for(k=1; k<=n-i ; ++k)
{
Dry run n=5
printf(“ ”);
} I k J
for(j=1; j<=i; ++j) 1 1,2,3,4 1
{ 2 1,2,3 1,2
printf(“ %c”,ch-j); /* one (1) spaces after %C */ 3 1,2 1,2,3
} 4 1 1,2,3,4
printf(“\n”); 5 - 1,2,3,4,5
} 6 Loop terminates
getch();
}

--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
void main()
{
int i,j,k,n=5;
clrscr();
for(i=1,i<=n ; ++i)
{
for (k=1;k<=n-i; ++k)
printf(“ ”) ; Output
}
*
for(j=1; j<=i; ++j)
* *
{ * * *
printf(“ *”) ; /* one (1) spaces after ‘*’ */ * * * *
} * * * * *
printf(“\n”); * * * *
* * * n=5
} Dry un
* *
for(i=n-1 ; i>=i ; - -i) * i K j
{ 1 1,2,3,4 1
for(k=1; k<=n-i; ++k) 2 1,2,3 1,2
{ 3 1,2 1,2,3,
printf(“ “) ; 4 1 1,2,3,4,
} 5 - 1,2,3,4,5,
for(j=1 ; j<=i; ++j) 6 loop terminates
{

40
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Dry un n=5
printf(“ *”) ;/* one(1) space after ‘*’ * / I K J
} 4 1 1,2,3,4
printf(“\n”) ; 3 1,2 1,2,3
} 2 1,2,3 1,2
getch(); 1 1,2,3,4 1
} 0 Loop terminates

--------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <conio.h>
Dry un n=5
void main()
{ I k J S
int i , j , s ,k ,n=5; 1 1,2,3,4 1,2,3,4,5 -
clrcsr(); 2 1,2,3 - 1,2,3
for(i=1; i<=n ; ++i) 3 1,2 - 1,2,3
{ 4 1 - 1,2,3
if(i==1|| i==n) 5 - 1,2,3,4,5 -
{ 6 Loop terminates
for (j=1 ; j<=n ; ++j) printf(“ * ”) ;
}
else
,printf(“ * ”) ;
for (s=1 ; s<=n-2 ; ++s)
,printf(“ “) ; /* one spaces */}
printf(“*”) ; Output
} **************
printf(“\n”) ; * *
} * *
getch(); * *
} **************
**
--------------------------------------------------------------------------------------------------------------------------------------

41
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.6 : Array


Write a program to print sum of any 10 numbers using 1-D
array
#include <stdio.h>
#include<conio.h>
void main () output Dry run
{ Enter 10 numbers i Num(i) Sum=0
int num[10] ,i, sum=0; 11 0 11 0+11=12
clrscr(); 22 1 22 11+22=33
printf (“enter 10 numbers\n”) ; 33
2 33 33+33=66
for (i=0 ; i<10 ; ++i) 44
3 44 66+44=110
55
{ 4 55 110+55=165
66
scanf(“%d”, & num *i+); 5 66 165+66=231
77
sum+=num[i]; 6 77 231+77=308
88
} 99 7 88 308+88=396
printf(“sum=%d”,sum); 101 8 99 396+99=495
getch(); Sum=596 9 101 495+101=596
} 10 Loop terminates

--------------------------------------------------------------------------------------------------------------------------------------
Write a program to find maximum alement from 1-D array
#include<stdio.h>
#include<conio.h>
Void main () output
{ How many elements ? 5
Int sum[10],i,max=0, n; Enter 5 elements
Clrscr () ; 12
Printf(“how many elements ? “) ; 76
Scanf (“%d”,&n) ; 45
Printf(“Enter %d elements \n” ,n) ; 90
For (i=0 ; i<n; ++i) 66
{ Maximum =90
Scanf(“%d”, &sum [i]) ;
If (sum [i]>max)
{max =sum[i]; Dry run n=5
} i Num(i) Sum=0
} 0 12 12
Printf(“ Maximum =%d”,max) ; 1 76 76
getch(); 2 45 76
} 3 90 90
4 66 90
5 Loop terminates

--------------------------------------------------------------------------------------------------------------------------------------

42
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Write a program to add cprresponding elements of two 1-D array add


Store them in third 1-D array.
#include<stdio.h> output
#include<conio.h> Enter size of array : 5
void main () Entre 5 elements of array A
{ 1 2 3 4 5
int a [10] ,b [10],c[10],i,n ; Enter 5 elements of array B
clrscr(); 6 7 8 9 10
printf(“ enter size of array : ”) ; Third Array
scanf(“%d”,& n) ; 7 9 11 13 15
printf(“enter %d elements of array a\n”,n)
for(i=0 ; i<n ; ++i)
{
scanf (“%d” ,&a [i]) ; Dry run n=5
} i a[ i ] b[i ] C [i]
printf(“enter %d elements of array b\n”,n) ; 0 1 6 1+6=7
for (i=0 ; i<n; ++i) 1 2 7 2+7=9
,scanf (“%d”, &b*i+ ) ;} 2 3 8 3+8=11
for (i=0 ; i<n; ++i) 3 4 9 4+9=13
{c [i] =a [i] +b[i]; } 4 5 10 5+10=15
printf(“third array \n”) ; 5 Loop terminates
for (i=0 ;i<n ; ++i)
,printf (“%d”, c[i] ) ; }
getch();
}

--------------------------------------------------------------------------------------------------------------------------------------
Write a program to fine number of odd and even elements from the 1-D array
#include<stdio.h>
#include<conio.h>
void main() output
{
How many elements ? 5
int a[10] ,i,n,even=0 ,odd=0;
Enter 5 elements
clrscr():
1 2 3 4 5
printf(“how many elements ?”) ;
Even Numbers = 2
scanf(“ %d”, &n) ; Odd Number = 3
printf(“enter %d elements \n” ,n);
for (i=0 ; i<n ; ++i)
{
scanf(“%d”,&a *i+);
if(a[i] %2==0) {even ++ ;} Dry run n=5
else {odd ++;} 0 1 6 1+6=7
} 1 2 7 2+7=9
printf(“even numbers = %d \nodd number = %d\n”,even ,odd) ; 2 3 8 3+8=11
getch(); 3 4 9 4+9=13
} 4 5 10 5+10=15
5 Loop terminates

43
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Write a program to sort given array in ascending order.


#include<stdio.h>
#include<conio.h>
void main()
{
int a [10], n, i ,j ,temp;
clrscr();
printf (“ how many elements ?”);
scanf(“%d” ,&n) ;
printf(“enter %d elements below\n”,n);
output
for(i=0 ; i<n ; ++i)
,scanf(“%d” ,&a [i ]);} How many elements ? 5
for(i=0 ; i<n-1 ; ++i) Enter 5 elements below
{for(j=i+1 ; j<n ; ++j) 12
{if (a [ i ] >a [ j ] ) 45
-90
{
0
temp= a[ i ] ;
5
a[ i ]= a[ j ] ;
Sorted List
a[ j ]= temp ;
-90
} 0
} 5
} 12
Printf(“ sorted list\n”) ; 43
For(i=0; i<n; ++i)
{Printf (“%d\n”, a[ i ] );} Don’t forget to refer dryrun from
getch(); Class note book
}

--------------------------------------------------------------------------------------------------------------------------------------
Write a program to sort given array in descending order.

#include <stdio.h>
#include<conio.h>
void main()
{
int a[10 ] ,n,i,j temp ; output
clrscr(); How many elements ? 5
printf(“how many elements ? “) ; Enter 5 elements below
scanf(“%d”, &n); 12
printf(“enter %d elements below\n”,n) ; 43
for(i=0 ; i<n; ++i) -90
{scanf (“%d” ,&a [ i ] );} 0
for(i=0 ; i<n-1 ; ++i) 5
{ Sorted List
for (j=i+1 ; j<n ; ++j) 43
{ 12
if(a[i ]>a [ j]) 5
{ 0
-90
temp=a[ i ] ;
a[ i ]= a[ j ] ;

44
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

a[ j ]=temp ;
} Find the difference between previous
} And this program
}
printf(“sorted list\n”) ;
for(i=0 ; i<n ; ++i)
{printf(“%d\n” ,a * i +) ;-
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Bubble Sort
#include <stdio.h>
#include<conio.h>
Void main()
{
Int n=5,i ;
clrscr();
Int pass ,exchs,temp,last ;
int k [ 5] ={5, -9,36,12,18 };
last=n;
for (pass=0 ; pass<n-1; ++pass)
{
exchs= 0; output
for (i=0 ; i<last-1 ; ++i)
{if (k [ i ]> k [i+1]) -9
{ 5
temp =k [i]; 12
k[ i ] = k [i+1 ]; 18
k[i+1]=temp; 36
exchs++;
}
} Please refer your Class note for dry run
if(exchs ==0) {break;}
else{last- -;)
}
for (i=0 ; i<n ;++i)
,printf( “%d\n” ,k*i+ ;-
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a program to add two matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int a [ 3 ] [3],b[3] [3],c[3] [3] ,i,j;
clrscr();
printf(“Enter 3 x 3 matrix A\n”);
for(i=0; i<3; ++i)
{for(j=0; j<3; ++j)
,scanf(“%d”, &a *i+ *j+ );-

45
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

}
printf(“Enter 3 x 3 matrix B\n”);
for(i=0; i<3; ++i)
{for(j=0; j<3 ; ++j) output
,Scanf(“%d” , &b *i+ *j+ );}
}
for(i=0 ; i<3 ; ++i) Enter 3 x 3 matrix A
{for(j=0 ; j<3; ++j) 1 1 1
{c[i] [j] =a [i] [j] + b[i] [j];} 2 2 2
} 3 3 3
printf(“Resultant Matrix C\n”); Enter 3 x 3 matrix B
for(i=0 ; i<3 ; ++i)
4 4 4
{for(j=0; j<3; ++j)
,printf(“%d\t”, c *i+ *j+);- 5 5 5
printf(“\n”) ; 6 6 6
} Resultant Matrix C
getch(); 5 5 5
} 7 7 7
9 9 9

Please refer your Class note for dry run

--------------------------------------------------------------------------------------------------------------------------------------

Write a program to count number of positive , negative and zero


Elements from 3 x 3 matrix
#include<stdio.h>
#include<conio.h>
void main()
output
{
int a [ 3 ] [3],i,j,pos=0,neg=0,zero=0;
clrscr(); Entr 3 x 3 matrix A
printf(“enter 3 x 3 ,matrix a\n”); 1 0 -2
for(i=0; i<3 ; ++i ) 3 -3 3
{ 0 5 5
for(j=0 ; j<3 ;++j) Positive Number = 5
Negative Number = 2
,scanf (“%d” ,&a *i+ *j+);
Zero Number = 2
if(a [i] [j] >0) pos++;
else if (a [i] [j] <0)neg++;
else zero ++;
}
Please refer your Class note for dry run
}
printf(“Positive Number = %d\n” , pos);
printf(“Negative number =%d\n”,neg);
printf (“Zero number = %d\n” ;zero);
getch();
}

46
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Read n x n matrix .Print the original matrix and its tranapose


#include<stdio.h>
#include<conio.h>
void main()
output
{
int n,a [10] [10 ] , i,j;
clrscr(); Enter n : 3
printf(“enter n : “) ; Enter 3 x 3 matrix
scanf(“%d”, &n) ; 1 2 3
printf(“Enter %d x %d matrix\n” ,n,n); 4 5 6
fro(i=0; i<n ; ++j) 7 8 9
{ for( j=0 ; j<n ; ++j)
Original Matrix
,scanf (“%d” ,&a *i] [j]);
} 1 2 3
} 4 5 6
printf(“transpose matrix \n “) ; 7 8 9
for(i=0; i<n ; ++i) Transpose Matrix
{for( j=0; j<n ; ++j) 1 4 7
,printf(“%d\t “, a *i+ *j+) ; 2 5 8
}
3 6 9
printf(“\n”) ;
}
printf(“transpose matrix \n”);
for(i=0 ; i<n ; ++i)
{for (j=0 ; j<n ;++j)
,printf(“%d\t” ,a[j] [i]);
}
printf (“\n”) ;
}
getch();
}

--------------------------------------------------------------------------------------------------------------------------------------
Write a program to check for symmetrical square matrix
e .g 1 2 3
2 4 5
3 5 8
#include<stdio.h>
#include<conio.h>
void main() output
{
int n,a [10 ] [10] ,i, j,flag ;
clrscr(); Enter n : 3
Enter 3 x 3 matrix A
printf(“enter n : “);
1 2 3
scanf(“%d”, &n) ;
2 5 6
printf(“enter %d x %d matrix a \n” ,n,n);
3 6 8
for (i=0 ; i<n ; ++i) Symmetric matrix
{for (j=0 ; j<n ;++j)

47
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

{scanf(“%d”,&a *i+ *j +);

}
}
flag =0 ;
for ( i=0 ; i<n ; ++i) output
{for (j=0 ; j<n ; ++j)
{if (a [i] [j ] ! =a [j] [i] )
{flag=1; Enter n : 3
break; Enter 3 x 3 matrix A
}
1 2 3
}
2 3 4
}
if(flag==0) 4 5 6
,printf(“symmetric matrix”) ;- Not Symmetric matrix
else
,printf(“not symmetric matrix”) ;-
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

48
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.7 : string


DRY RUN

String Length Len= 0 S[len]


0 ‘H’
#include <stdio.h> Enter a string : Hi NICE 1 ‘I’
#include<conio.h> Length : 7
2 ‘‘
void main() 3 ‘N’
{ 4 ‘I’
int len =0; 5 ‘C’
char s [10 ]; 6 ‘E’
clrscr(); 7 ‘\0’
printf (“enter a string: ”) ;
scanf(“%*^\n +” ,s) ;
while(s*len+ ! =’ \0 ‘)
{len ++;}
printf(“length : %d “ , len) ;
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
WORD COUNT Dry run
#include <stdio.h>
Len= 0 S[len] C=0
#include<conio.h>
0 ‘h’ 1
void main() Enter a string : hello how are you 1 ‘e’ 1
{ Total word : 4 2 ‘I’ 1
int len =0, c=1 ; Length : 7 3 ‘I’ 1
char s [40 ]; 4 ‘o’ 1
clrscr(); 5 ‘‘ 2
printf (“Enter a string :”) ; 6 ‘h’ 2
scanf(“%*^\n +” ,s) ; 7 ‘o’ 3
while(s*len+ ! =’ \0 ‘) 8 ‘w’ 2
9 ‘‘ 3
{
10 ‘n’ 3
if(s[len] = = ‘ ‘)c++ ;
11 ‘i’ 3
len++ ; 12 ‘e’
} 13 ‘‘
printf(“total word : %d “ , c) ; 14 ‘ y’
getch(); 15 ‘o’
} 16 ‘u’
17 ‘\0’
-----------------------------------------------------------------------------------------------------------------------------
Count vowels
#include <stdio.h>
#include<ctype.h>
#include<conio.h>

49
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

void main()
{ Dry run
Char s[40] ,ch ;
Len= 0 S[len] C=0
int len=0, i,c=0 ;
0 ‘b’ 0
clrscr(); 1 ‘e’ 1
output
printf (“Enter a string:”) ; 2 ‘I’ 1
scanf(“%*^\n +” ,s) ; Enter original string : hello how are you 3 ‘j’ 1
Total count : 7
while(s*len+ ! =’ \0 ‘) len++; 4 ‘o’ 2
Length : 7
for (i=0 , i<len ; ++i) 5 ‘‘ 2
{ 6 ‘h’ 2
Ch=toupper(s [i] ); 7 ‘o’ 3
8 ‘w’ 3
if(ch==’A’ || ch==’E’ || ch==’I’ || ch==’O’ ||ch==’U’ )c++ ;
9 ‘ ‘ 3
}
10 ‘a’ 4
printf(“Total count : %d “ , c) ; 11 ‘r ‘ 4
getch(); 12 ‘e’ 5
} 13 ‘‘ 5
14 ‘y’ 5
15 ‘o’ 6
16 ‘u’ 7
17 ‘\0’ 7

--------------------------------------------------------------------------------------------------------------------------------------
Read a string through the keyboard, writte a program to print charac
ter by character with the ASCII code.
#include <stdio.h> output
#include<ctype.h>
#include<conio.h> Enter a string : nice
#includ<string.h> n 110
I 105
void main()
C 99
{ E 101
int i ;
char name[20] ;
clrscr();
printf (“Enter a string :”) ;
scanf(“%s” ,name) ;
for (i=0 ; i<strlen(name) ; ++i) note
{ Character ASCII Value
printf(“%c\t%d \n”,name *i+,name*i+ ); A………….Z 65………….90
} a………….z 97……….122
getch(); 0………….9 48…………57
}

--------------------------------------------------------------------------------------------------------------------------------------
Read a program to find out the number of times ‘a’
Is there in the string a that is entered through
Keyboard.
#include <stdio.h>

50
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

#include<ctype.h>
#include<conio.h>
#include<string.h>
void main()
{ Dry rrun
Char s[20] ;
Len= 0 S[len] C=0
int i , c=0 ;
output 0 ‘b’ 0
printf (“Enter a string :”) ; 1 ‘e’ 0
scanf(“%*^\n +” ,s) ; Enter a string : hello how are you
2 ‘I’ 0
A occure 1 times in above string
for (i=0 ; i<strlen(s) ; ++i) 3 ‘j’ 0
{ 4 ‘o’ 0
If(s*i+==’a’)c++; 5 ‘‘ 0
} 6 ‘h’ 0
printf(“a occurs %d times in above string”,c); 7 ‘o’ 0
getch(); 8 ‘w’ 0
} 9 ‘‘ 0
10 ‘a’ 1
11 ‘r ‘ 1
12 ‘e’ 1
13 ‘‘ 1
14 ‘y’ 1
15 ‘o’ 1
16 ‘u’ 1
17 ‘\0’ 1

--------------------------------------------------------------------------------------------------------------------------------------
Find Character Self Practice using pencil
#include <stdio.h>
#include<ctype.h>
#include<conio.h>
Void main()
{
Dry run
char s[40] ,c ;
int len =0, i,flag=0 ;
printf (“Enter original string:”) ;
scanf(“%*^\n +” ,s) ; output
while(s*len+ ! =’ \0 ‘) len++;
fflush (stdin) ; Enter original string : hello how are you
Enter character to scan : 0
printf(“Enter character to scan : “) ;
o occurs at postion : 5
scanf(“%c” ,&c ); o occurs at postion : 8
for (i=0 , i<len ; ++i) o occurs at postion : 16
{
If(s[i] = =c)
{
flag=1;
printf(“%c occurs at postion : %d\n” , c, i+1);
}
}

51
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

If(flag==0)
{
printf(“%c does not occur in %s “ , c,s) ;
}
getch();
}

--------------------------------------------------------------------------------------------------------------------------------------

Replace Character
#include <stdio.h>
#include<ctype.h>
#include<conio.h> output
void main()
{ Enter original string : hello how are you
char s[40] ,c , r ; Enter character to replace : 0
int len =0, i ; Enter new character : x
Hellx how are yxu
printf (“Enter original string:”) ;
scanf(“%*^\n +” ,s) ;
while(s*len+ ! =’ \0 ‘) len ++; Self Practice using pencil
fflush (stdin) ;
Dry run
printf(“Enter character to replace: “) ;
scanf(“%c” ,&c );
fflush (stdin) ;
printf(“Enter new character : “) ;
scanf( “%c”, &r);
for (i=0; i<len ; ++i)
{
If(s[i] ==c) { s[i] =r ;}
}
printf(“%s “ , s) ;
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

52
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.8 : Function


Write a Ffunction to add first a number
#include <stdio.h>
#include<ctype.h>
#include<conio.h>
Memory Diagram
void main()
{
clrscr( ) ; main
int addn ( int ) ; n
int n, ans ; 5
n
printf (“Enter n : “) ans 15
scanf(“%d”,&n ) ;
ans =addn (n) ;
Returing
printf(“Answer =%d”, ans); From Calling
Function
getch() ; Function

}
Int addn (int m)
5
{
int total =0 , i; 15
for(i=1 ; i<=m; ++i)
{ add
Total +=i;
}
return total ; output
} Enter n : 5
Answer = 15

--------------------------------------------------------------------------------------------------------------------------------------
Write a function to sum of odd numbers between 1 to answer
N Is entered through keybord .
#include <stdio.h>
#include<conio.h>
void main()
{ output
Int oddsum ( int ) ;
Enter n : 10
Int n, ans ;
Sum of odd numbers between 1 to 10 = 25
printf (“Enter n : “)
scanf(“%d”,&n ) ; For Memory diagram, refere your note book
ans =oddsum (n) ;
printf(“sum of odd numbers between 1 to %d =%d”, n,ans);
getch();
}
int odd (int m)

53
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

{
int total =0 , i ;
for(i=1 ; i<=m; i+=2)
{
total +=i;
}
return total ;
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a function program to sum of square of first n numbers , where r
Is entered through keyboard .
#include <stdio.h>
#include<conio.h>
void main()
{ output
int squaresum ( int ) ;
Enter n : 5
int n, ans ;
Sum of square numbers between 1 to 5 = 55
clrscr();
printf (“Enter n : “)
scanf(“%d”,&n ) ; For Memory diagram, refere your note book
ans =squaresum (n) ;
printf(“sum of square numbers from 1 to %d =%d”, n,ans);
getch();
}
Int squaresum (int m)
{
Int total =0 , i ;
for(i=1 ; i<=m; i++)
{
total +=(i*i);
}
return total ;
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a function program to find out average of first n num-
Bers, where n is entered through keyboard.
#include <stdio.h>
#include<conio.h>
void main()
{ output
float average ( int ) ;
Enter n : 10
Int n;
Average numbers between 1 to 10 = 5 . 500000
clrscr();

54
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

float ans ;
printf (“Enter n : “)
scanf(“%d”,&n ) ; For Memory diagram, refere your note book
ans = average(n) ;
printf(“average of numbers from 1 to %d =%f”, n,ans);
getch();
}
float average (int m)
{
Int total = 0 , i ;
for(i=1 ; i<=m; i++)
{
total +=i;
}
return total / (m*1.0);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a function program to find factore of given number n, where n is
entered through keyboard.
#include <stdio.h>
output
#include<conio.h>
void main()
{ Enter n : 20
2 4 5 10
void factor ( int ) ;
int n ;
clrscr(); For Memory diagram, refere your note book
printf (“Enter n : “);
scanf(“%d”,&n ) ;
factor(n) ;
getch();
}

void factor (int m)


{
int i ;
for(i=2 ; i<m; i++)
{
if (m%i==0 )printf(“%d\t” , i) ;
}
}
-------------------------------------------------------------------------------------------------------------------------------------
Write a function program to find out factorial of a given number

55
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

#include <stdio.h>
#include<conio.h>
void main()
{
int n ;
clrcsr();
long ans ;
long factorial (int) ;
printf (“Enter a number : “)
scanf(“%d”,&n) ;
ans = factorial (n) ;
printf(“factorial =%ld\ n”,ans);
getch():
} output
long facorial (int m)
Enter a number : 6
{ Factorial = 720
int i ;
long f=1 ; For Memory diagram, refere your note book
for(i=1 ; i<=m; ++i) f*=i ;
return f;
}
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a function program to interchange values of two variable .
(Using global variable)
#include <stdio.h>
#include<conio.h>
Int a,b;
void main()
{
void interchange() ;
printf (“Enter two number : “)
scanf(“%d%d”,&a,&b ) ;
interchange();
printf(“a=%d\tb=%d\n”;a,b);
} output
void interchange( )
Enter two number : 10 20
{ a = 20 b=10
int temp ;
Temp=a ; For Memory diagram, refere your note book
a=b;
b=temp;
}

56
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

--------------------------------------------------------------------------------------------------------------------------------------
Write a function space (x) that can be used to provide a space a space
Of x positions between two output numbers.
#include <stdio.h>
#include<conio.h>
void main()
{
void space(int);
printf(“Line1”); output
space(10);
printf(“Line2”); Line1 Line2
getch();
}

void space (int m) For Memory diagram, refere your note book
{
int i;
for (i =1; i<=m ; ++i) printf(“ “);
}

--------------------------------------------------------------------------------------------------------------------------------------
Write a function to return 1 if the number is prime otherwise return 0
#include <stdio.h>
#include<conio.h>
Void main()
{
int isprime(int);
int n;
printf(“Enter number to check : “);
scanf(“%d”,&n);
if(isprime ( n) ==1)
{
printf(“%d is prime “, n) ;
}
else
output
{
printf(“%d is not prime “ , n) ; Enter number to check : 15
} 15 is not prime
getch();
} output
int isprime (int m)
Enter number to check : 17
{ 17 is prime
int i ;
for(i=2 ; i<m ; ++i)
{
if(m%i = =0 ) return 0 ; For Memory diagram, refere your note book
}

57
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

return 1 ;
getch();
}
__________________________________________________________________________________
Write a function power(x,y)
#include <stdio.h>
#include<conio.h>
void main()
{
float x, ans;
int y; output
float power (float,int); Enter x : 1 . 2
printf(“Enter x : “); Enter y : 5
scanf(“%f”,&x); 2 . 488320
printf(“Enter y : “);
scanf(“%d ,&y“);
For Memory diagram, refere your note book
ans=power(x,y);
printf(“%f”,ans);
getch();
}
float power (float a, int b)
{
int i;
float p=1 ;
for (i=1; i<=b ; ++i)
{
p*=a;
}
Return p;
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Write a function to find out maximum out of three number
#include <stdio.h>
#include<conio.h>
Void main()
{
output
Int x,y,z,max;
Int maxthree (int,int,int); Enter three number : 5 19 2
Maximum=19
printf(“Enter three number :”);
scanf(“%d%d%d”,&x,&y,&z);
max=maxthree (x,y,z);
printf(“Maximum=%d’, max) ; For Memory diagram, refere your note book
getch();
}

58
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Int maxthree(int a, int b, intc)


{
return((a>b) ? ((a>c)?a:c) : (b>c) ? b:c);
}

--------------------------------------------------------------------------------------------------------------------------------------

59
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.9 : Structure


Initialization of structure

#include <stdio.h>
#include<conio.h>
struct student
{
int rollno: output
char name[20];
45 Bhavesh
};
void main()
{
struct student s=,45 , “Bhavesh”} ;
clrscr();
printf(“%d\t%s\n” , s.rollno ,s .name) ;
getch() ;
}
--------------------------------------------------------------------------------------------------------------------------------------
Initialization of multiple structure
#include<stdio.h>
#include<conio.h>
struct student
{
output
int rollno;
char name[20]; 45 Bhavesh
}; 65 Chirag
void main()
{
Struct student s1 ={45,”Bhavesh”-,s2 = ,65 ,”chirag”-;
printf(“%d\t%s\n”, s1 . rollno ,s1 . name);
printf(“%d\t%s\n “, s2 . rollno ,s2 . name);
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Reading structure elements from keyboard output
#include <stdio.h>
#include<conio.h> Enter rollno and name for first student :
struct student 11 Akash
{ Enter rollno and name for second studee :
int rollno; 22 Bhavin
char name[20];
};
student # s 1 : 11 Akash
student # 2 : 22 Bhavin 60
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

void main()
{
student s1 ,s2 ;
clrcsr();
printf(“Enter rollno and name for first student :”);
scanf(“%d%s”, &s1 . rollno,s1 .name);
printf(“Enter rollno and name for second student : “);
scanf(“%d%s”, &s2 . rollno ,s2 . name)
printf(“Student # 1 : %d\t%s\n”,s1 . rollno, s1.name);
printf(“Student # 2 : %d\t%s\n”,s2 . rollno, s2.name);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Array of structure
#include<stdio.h> output
#include<conio.h>
struct student
How many student ? 4
{
Student # 0 : rollno and name ? 11 aman
int rollno;
Student # 1 : rollno and name ? 22 Bindu
char name[20] ;
Student # 2 : rollno and name ? 33 Chotan
};
Student # 3 : rollno and name ? 44 Deepkunj
void main() You have entered
{ Student # 0 : rollno : 11 Name : Aman
student s[10]; Student # 1 : rollno : 22 Name : Bindu
int i,n; Student # 2 : rollno : 33 Name : Chotan
clrscr(); Student # 3 : rollno : 44 Name : Deepkunj
printf(“how many students ?”);
scanf(“%d” , &n);
for (i= 0 ; i<n : ++i)
{
printf(“student # %d : Rollno and name ? “ ,i);
sacnf (“%d%s” , &s [i] . rollno , s[i] . name);
}
printf(“you have entered\n”)
for(i=0 ; i<n; ++i)
{
printf(“student # %d : rollno : %d name :
%s\n”,i,s*i+ .rollno,s*i+ .name );
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Searching an item from array of structure output
#include<stdio.h>
#include<conio.h> How any items ? 4
Item # 0
struct item
Code , name , Quantity : 101 Salt 45
{
Item # 1
int code;
Code , name , Quantity : 201 tea 20
char name;
Item # 2
int qty ; Code , name , Quantity : 301 soap 340
}; Item # 3
Code , name , Quantity : 401 toothpaste 20
Enter item code for the item you want to view : 61
301
Name : soap Quantity : 340
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

void main()
{
item it[10];
int n,i,icode,flag=0;
printf(“How many items ? “);
scanf(“%d”, &n) ;
for (i=0 ; i<n; ++i)
{
printf(“Item # %d\n “ , i);
printf(“code , Name , Quantity : “ );
scanf(“%d%s%d”, &it*i+ . code , it [i] . name, & it[i] .qty );
}
printf(“enter item code for the item you want to view : “);
scanf(“%d”, &icode) ;
for(i=0 ; i<n ; ++i)
{
If (it [i] . code ==icode)
{
flag =1 ;
printf(“Name : %s \t Quantity :
%d\n”,it*i+ . name, it [i] .qty) ;
break;
}
}
if(flag==0)printf(“Item Not found . . . “);
getch() ;
}
--------------------------------------------------------------------------------------------------------------------------------------
Sorting elements of array of structure
#include<stdio.h>
#include <conio.h> output
struct item
{ How many items ? 4
int code; Item # 0
Code , name , Quantity : 201 soap 187
char name[20];
Item # 1
int qty ; Code , name , Quantity : 401 Toothpaste 35
} Item # 2
void main() Code , name , Quantity : 101 salt 20
{ Item # 3
Code , name , Quantity : 301 coffee 50
item it [10] ,t;
After sorting on code
int n , i,j ; 101 salt 203
printf(“how many items ? “) 201 soap 187
scanf(“%d “, &n); 301 coffee 50
401 Toothpaste 35
for(i=0 ; i<n; ++i)
,printf(“item # %d\n” ,i) ;
printf (“code , name , quantity : “) ;
scanf(“%d%s%d,&it[i] . code,it[i] . name, &it[i] .qty);
}

62
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

for (i=0 ; i<n-1 : ++i)


{for (j=i+1 ; j<n; ++j)
{
if(it[i] . code>it[j] .code )
{
t=it[i] ; it [i]=it [j] ; it[j] =t ;
}
}
}
printf(“After Sorting on code\n”);
for(i=0 ; i<n ; ++i)
{
printf(%d\t%s\t%d\n” , it*i+ .code , it*i+ . name, it [i] .qty)
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

63
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Module 2.10 : Pointer


Printing Addresses of Variable
#include<stdio.h>
#include<conio.h>
void main() output
{
Address of a = 65524
Int a=5;
Address of b = 65520
float b=10, 20; Address of c = 65519
char c =’A’;
printf(“Address of a = %u\n “ ,&a) ;
printf(“Address of b = %u\n “ ,&b) ;
printf(“Address of c = %u\n “ ,&c) ;
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Accessing elements using pointer
#include<stdio.h>
#include<conio.h> output
Void main()
{ Address of a = 65524
Int a =5 ; Contents of p = 65524
Int *p ; Value of a = 5
p=&a; Value being pointed by =5
printf(“Address of a = %u \n “, &a);
printf(“Contents of p = %u\n”, p);
printf(“Value of a = %d\a ,a);
printf(“Value being pointed by = %d\n “ , *p);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Changing elements using pointed
#include<stdio.h>
#include<conio.h>
output
void main()
{
a=5
int a =5 ;
After *p ++; a=6
int *p ; After a++ , *p =7
p=&a;
printf(“a=%d\n” , a);
(*p) ++;
printf(“After *p++ , a=%d\n” , a);
a++;
printf(“After a++ , *p =%d\n” , *p);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

64
Prepare By Velocis Education & Spoken English C – LANGUAGE BOOKS

Pointer and expression


#include<stdio.h>
#include<conio.h>
output
void main()
{
int a,b, *p1 , *p2 ,x,y,z; a=10 b=20 X=194 Y=2
a=10,b=20; a=30 b=10 z=294
p1=&a;
p2=&b;
x=*p1**p2-6;
y=4*-*p2/ *p1+10 ;
printf(“a =%d\tb=%d\tx=%d\ty=%d\n” ,a,b,x,y);
*P1* =*p1+20;
*P2*=*p2 -10 ;
z=*p1**p2-6;
printf(“a=%d\tb=%d\tz=%d\n”,a,b,z);
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
Pointer to return “multiple” values from function
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
void exchange (int* ,int*) ;
printf(“before calling function : a=%d , b=%d\n” , a,b);
exchange (&a,&b);
printf (“after calling function : a=%d b=%d\n” , a,b);
exchange (&b ,& a);
getch();
}
void exchange (int *p1, int *p2) output
{
Before Calling Function : a=10 b=20
int temp;
After Calling function : a=20 b=10
temp=*p1;
*p1=*p2;
*p2=temp;
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------

65

You might also like