Programming Through 'C' Language
Programming Through 'C' Language
Paritosh Mohapatra
Roll No.-9
A-Level
OCAC
paritoshohapatra!"ahoo.#o
CONTENT$
Sl. No. Subjects Page No.
1. Conceptual 4
2. Control Statements 7
3. Triangles 23
4. Arrays 26
. !atri" 3#
6. String or Array o$ C%aracters 32
7. Number System 37
&. 'unction 4#
(. )ecursion 4
1#. Sorting 4&
11. Searc%ing #
12. Pointers 2
13. Structure (
14. *in+ *ist 6#
1. 'ile 74
16. ,-.ACC ."amination /uestions 0 Ans1ers 76
2
%&i#' Re(ere)#e
Sl. No. Programs Page No.
1. Area -$ Circle. 6
2. C%ec+ing 'or *eap 2ear. 11
3. Printing T%e -33 0 .4en Numbers. 16
4. Sum -$ ,igits. 1
. )e4ersing A Number. 15 46
6. .4aluating "
n
. 1&5 425 475 &1
7. Calculating 'actorial. 1(5 45 77
&. 6enerating 'ibonacci Series. 1(5 4
(. 6enerating ASC77 C%art. 1&
1#. Prime Numbers. 2#5 215 43
11. Armstrong Numbers. 23
12. Palin3rome Numbers. 225 7
13. Printing T%e 'loy38s Triangles. 26
14. Computing !ean5 9ariance 0 Stan3ar3 ,e4iation. 2(
1. Searc% 'or T%e :ig%est 0 *o1est Number 7n An Array. 27
16. Con4erting A String To *o1er Case. 36
17. Con4erting A String To ;pper Case. 36
1&. Con4erting A String To 'irst Case. 37
1(. )e4ersing A String. 335 7
2#. C%ec+ing 'or String Palin3rome. 34
21. Counting Number -$ 9o1els 7n A String. 3
22. Counting Number -$ <or3s 7n A String. 345 3
23. Con4ersion -$ ,ecimal To =inary Number. 37
24. Con4ersion -$ =inary To ,ecimal Number. 3(
2. Con4ersion -$ ,ecimal To -ctal Number. 3&
26. Con4ersion -$ ,ecimal To :e"a3ecimal Number. 3&
27. 6enerating Number System. &(
2&. T%e To1er -$ :anoi. 47
2(. Sorting An Array ;sing =ubble Sort Algorit%m. 4&5 &4
3#. Sorting An Array ;sing /uic+ Sort Algorit%m. 4(
31. Searc%ing A >ey 9alue 7n An Array ;sing Se?uential Searc%. #
32. Searc%ing A >ey 9alue 7n An Array ;sing ,ictionary Searc%. 1
33. Concatenating T1o Strings.
34. Processing A *in+ *ist. 6#5 715 73
3. 7nserting A Ne1 No3e 7n A *in+ *ist. 625 635 64
36. ,eleting A No3e 'rom A *in+ *ist. 675 6&5 7#
37. Counting T%e No3es -$ A *in+ *ist. 61
3&. 6enerating A =oo+ Structure. (
3(. )ea3ing T%e Contents -$ A 'ile. 74
4#. <riting To A 'ile. 74
3
Conceptual
1) Write a program to print HELLO WORLD.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("\nHELLO WORLD");
getch();
}
2) Write a program to compute the addition, subtraction, product, quotient and
remainder of two given numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,add,sub,prod,quot,remain;
clrscr();
printf("\nENTER NUMBER-1: " );
scanf("%d",&n1);
printf("\nENTER NUMBER-2: " );
scanf("%d",&n2);
add=n1+n2;
sub=n1-n2;
prod=n1*n2;
quot=n1/n2;
remain=n1%n2;
printf("\nADDTON OF THE NUMBERS: %d",add);
printf("\nSUBSTRACTON OF THE NUMBERS: %d",sub);
printf("\nPRODUCTON OF THE NUMBERS: %d",prod);
printf("\nQUOTENT OF THE NUMBERS: %d",quot);
printf("\nREMANDER OF THE NUMBERS: %d",remain);
getch();
}
3) Write a program to compute the average of three given numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,n3;
4
float avg;
clrscr();
printf("\nENTER THREE NUMBERS: " );
scanf("%d %d %d",&n1,&n2,&n3);
avg=(n1+n2+n3)/3;
printf("\nAVERAGE: %0.2f",avg);
getch();
}
4) Write a program to compute the seconds from a given age.
#include <stdio.h>
#include <conio.h>
void main()
{
long unsigned age,sec;
clrscr();
printf("\nENTER YOUR AGE: ");
scanf("%lu",&age);
sec=age*365*24*60*60;
printf("\nAGE N SECONDS: %lu",sec);
getch();
}
5) Write a program to compute simple interest and compound interest from the
given principal, time period and interest rate.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int p,t;
float si,ci,r,i;
clrscr( );
printf("\nENTER PRNCPAL: ");
scanf("%d",&p);
printf("\nENTER TME PEROD: ");
scanf("%d",&t);
printf("\nENTER RATE OF NTEREST: ");
scanf("%f",&r);
si=(p*t*r)/100;
i=r/100;
ci=p*pow((1+i),t);
printf("\nSMPLE NTEREST: %0.2f",si);
printf("\n\nCOMPOUND NTEREST: %0.2f",ci);
getch();
}
Note: - To use the pow() function <math.h> header file must be included.
6) Write a program to compute the area of a circle from the given radius.
Hint: - Area of the circle= x (radius)
2
.
#include <stdio.h>
#include <conio.h>
void main()
{
float pi=3.14,rad,area;
clrscr();
printf("\nENTER THE RADUS OF THE CRCLE: ");
scanf("%f",&rad);
area=pi*rad*rad;
printf("\nTHE AREA OF THE CRCLE S: %0.2f",area);
getch();
}
7) Write a program to swap the values of two variables.
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nENTER TWO NUMBERS FOR a AND b:\n");
scanf("%d %d",&a,&b);
printf("\nBEFORE SWAPNG THE VALUE OF a=%d AND b=%d",a,b);
c=a;
a=b;
b=c;
printf("\nAFTER SWAPNG THE VALUE OF a=%d AND b=%d",a,b);
getch();
}
8) Write a program to swap the values of two variables without using a third
variable.
#include <stdio.h>
#include <conio.h>
void main()
{
6
int a,b;
clrscr();
printf("\nENTER TWO NUMBERS FOR a AND b:\n");
scanf("%d %d",&a,&b);
printf("\nBEFORE SWAPNG THE VALUE OF a=%d AND b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAFTER SWAPNG THE VALUE OF a=%d AND b=%d",a,b);
getch();
}
Control Statements
A) Conditional Control Statements
1) If Conditions
1) Write a program to compute net amount from the given quantity purchased
and rate per quantity. Discount @10% is allowed if quantity purchased exceeds
100.
Net Amount = (Quantity Purchased x Rate Per Quantity) Discount.
#include <stdio.h>
#include <conio.h>
void main()
{
int qty,rate;
float disc=0.0,net;
clrscr();
printf("\nENTER QUANTTY: ");
scanf("%d",&qty);
printf("\nENTER RATE: ");
scanf("%d",&rate);
if (qty>100)
disc=qty*rate*10/100;
net=(qty*rate)-disc;
printf("\nNET AMOUNT: %0.2f",net);
getch();
}
2) Find out the highest number from three given numbers.
#include <stdio.h>
#include <conio.h>
7
void main()
{
int a,b,c,h;
clrscr();
printf("\nENTER THREE NUMBERS:\n");
scanf("%d %d %d",&a,&b,&c);
h=a;
if(b>h)
h=b;
if(c>h)
h=c;
printf("\nHGHEST NUMBER S %d",h);
getch();
}
Alternative Method
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nENTER THREE NUMBERS:\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("\nHGHEST NUMBER S %d",a);
if(b>c && b>a)
printf("\nHGHEST NUMBER S %d",b);
if(c>a && c>b)
printf("\nHGHEST NUMBER S %d",c);
getch();
}
3) Find out the lowest number from three given numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,l;
clrscr();
printf("\nENTER THREE NUMBERS:\n");
scanf("%d %d %d",&a,&b,&c);
l=a;
if(b<l)
&
l=b;
if(c<l)
l=c;
printf("\nLOWEST NUMBER S %d",l);
getch();
}
Alternative Method
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nENTER THREE NUMBERS:\n");
scanf("%d %d %d",&a,&b,&c);
if(a<b && a<c)
printf("\nLOWEST NUMBER S %d",a);
if(b<c && b<a)
printf("\nLOWEST NUMBER S %d",b);
if(c<a && c<b)
printf("\nLOWEST NUMBER S %d",c);
getch();
}
4) Write a program to check a given number is odd or even.
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&a);
if(a%2==0)
printf("\nTHE NUMBER S AN EVEN NUMBER");
else
printf("\nTHE NUMBER S AN ODD NUMBER");
getch();
}
5) Write a program to find out a person is insured or not. The person is insured if
following conditions are satisfied: -
(
i) f the person is married.
ii) f the person is unmarried, male & above 30 years of age.
iii) f the person is unmarried, female & above 25 years of age.
n all other cases the person is not insured.
#include <stdio.h>
#include <conio.h>
void main()
{
char ms,sex;
int age;
clrscr();
printf("\nEnter Marital Status [M/U],Sex [M/F],Age: ");
scanf("%c %c %d",&ms,&sex,&age);
if((ms=='M') || (ms=='U' && age>30 && sex=='M') || (ms=='U' && age>25
&& sex=='F'))
printf("\nTHE EMPLOYEE S NSURED");
else
printf("\nTHE EMPLOYEE S NOT NSURED");
getch();
}
6) Write a program to find out the gross amount from the given basic pay.
Gross = Basic + DA + HRA
DA & HRA can be calculated as follows: -
f Basic Pay is greater than or equal to 8000 DA is 20% of Basic Pay & HRA is
25% of Basic Pay, otherwise DA is 15% of Basic Pay & HRA is 20% of Basic
Pay.
#include <stdio.h>
#include <conio.h>
void main()
{
float basic,da,hra,gross;
clrscr();
printf("\nENTER BASC PAY: ");
scanf("%f",&basic);
if(basic>=8000)
{
da=basic*20/100;
hra=basic*25/100;
}
1#
else
{
da=basic*15/100;
hra=basic*20/100;
}
gross=basic+da+hra;
printf("\nGROSS AMOUNT: %0.2f",gross);
getch();
}
7) Write a program to compute the division from the given marks of 5 subjects.
The division can be calculated as follows: -
Average Mark Division
>=60 First
>=50 Second
>=40 Third
<40 Fail
#include <stdio.h>
#include <conio.h>
void main()
{
int m1,m2,m3,m4,m5,per;
clrscr();
printf("\nENTER THE MARKS OF THE SUBJECTS:\n");
scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);
per=(m1+m2+m3+m4+m5)/5;
if(per>=60)
printf("\nFRST DVSON");
else
{
if(per>=50)
printf("\nSECOND DVSON");
else
{
if(per>=40)
printf("\nTHRD DVSON");
else
printf("\nFAL");
}
}
getch();
}
8) Write a program to check whether a given year is leap year or not.
11
#include <stdio.h>
#include <conio.h>
void main()
{
int year,n;
clrscr();
printf("\nENTER A YEAR: ");
scanf("%d",&year);
if(year%4==0 && year%100 !=0 || year%400==0)
printf("\n%d S A LEAP YEAR",year);
else
printf("\n%d S NOT A LEAP YEAR",year);
getch();
}
2) Switch Case
1) Write a program to ask the user to enter a number. f the number is 1 print
One, if the number is 2 print Two, if the number is 3 print Three, otherwise
print Other than One, Two or Three.
#include <conio.h>
#include <stdio.h>
void main()
{
int i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&i);
switch(i)
{
case 1:
printf("\nNUMBER S ONE");
break;
case 2:
printf("\nNUMBER S TWO");
break;
case 3:
printf("\nNUMBER S THREE");
break;
default:
printf("\nNUMBER S OTHER THAN ONE, TWO OR
THREE");
}
getch();
12
}
2) Write a program to ask the user to enter a character. f the user enter 'a' or 'A'
print The Character is a or A, if the user enter 'b' or 'B' print The Character is b
or B, if the user enter 'c' or 'C' print The Character is c or C, otherwise print
Other than a, b, c, A, B, C.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch;
clrscr();
printf("\nEnter A Character: ");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A':
printf("\nCharacter s a Or A");
break;
case 'b':
case 'B':
printf("\nCharacter s b Or B");
break;
case 'c':
case 'C':
printf("\nCharacter s c or C");
break;
default:
printf("\nCharacter s Other Than a,b,c,A,B,C");
}
getch();
}
B) Loop Control Statements
1) While Loop
1) Write a program to print natural numbers up to a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1,n;
13
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nNATURAL NUMBERS UPTO %d ARE:\n",n);
while(i<=n)
{
printf("%d ",i);
i=i+1;
}
getch();
}
2) Write a program to ask the user to enter a series of marks of a student. f the
user enters 1, come out of the loop and print the average mark.
#include <stdio.h>
#include <conio.h>
void main()
{
int mark,num=0;
float sum,avg=0.0;
clrscr();
printf("\nEnter Mark Or -1 To Quit: \n");
scanf("%d",&mark);
while(mark!=-1)
{
sum+=mark;
num+=1;
scanf("%d",&mark);
}
avg=sum/num;
printf("\nAverage Mark: %0.2f",avg);
getch();
}
3) Write a program to ask the user for a number. Print the square of the number
and ask a confirmation from the user whether the user want to continue or not.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch='y';
int num;
clrscr();
while (ch=='y'|| ch=='Y')
14
{
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nTS SQUARE S: %d\n",num*num);
printf("\nDO YOU WANT TO CONTNUE [Y/N]: ");
ch=getche();
}
getch();
}
2) Do While Loop
1) Write a program to print the sum of digit of a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int digit;
unsigned long num,sum=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
do
{
digit=num%10;
num=num/10;
sum=sum+digit;
}
while(num!=0);
printf("\nSUM OF THE DGTS S %lu",sum);
getch();
}
2) Write a program to print the reverse of a given number.
Example: - Reverse of 2565 is 5652.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num,rev;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
1
printf("\nTHE REVERSE OF THE NUMBER %lu S ",num);
do
{
rev=num%10;
num=num/10;
printf("%lu",rev);
}
while(num!=0);
getch();
}
3) For Loop
1) Write a program to print the odd numbers within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nODD NUMBERS BETWEEN 1 AND %d ARE: \n",n);
for(i=1;i<=n;i+=2)
{
printf("%d ",i);
}
getch();
}
2) Write a program to print the even numbers within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nEVEN NUMBERS BETWEEN 1 AND %d ARE: \n",n);
for(i=2;i<=n;i+=2)
{
printf("%d ",i);
}
16
getch();
}
3) Write a program to add the odd numbers within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
unsigned long sum=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=1;i<=n;i+=2)
{
sum=sum+i;
}
printf("\nSUM OF THE ODD NUMBERS BETWEEN 1 TO %d S
%lu",n,sum);
getch();
}
4) Write a program to add the even numbers within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
unsigned long sum=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=2;i<=n;i+=2)
{
sum=sum+i;
}
printf("\nSUM OF THE EVEN NUMBERS BETWEEN 1 TO %d S
%lu",n,sum);
getch();
}
5) Write a program to print the multiplication table of a given number.
#include <stdio.h>
17
#include <conio.h>
void main()
{
int n,i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nMULTPLCATON TABLE OF %d\n",n);
for(i=1;i<=10;i++)
{
printf("\n%2d x %2d = %3d",n,i,n*i);
}
getch();
}
6) Write a program to print the ASC chart within a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
printf("%d=%c ",i,i);
getch();
}
7) Write a program to evaluate x
n
.
#include <stdio.h>
#include <conio.h>
void main()
{
int num,temp,power,i;
unsigned long res=1;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nENTER THE POWER: ");
scanf("%d",&power);
temp=num;
for(i=1;i<=power;i++)
{
1&
res=res*num;
}
printf("\nTHE %d POWER OF %d S %lu",power,temp,res);
getch();
}
8) Write a program to evaluate
1
/3 +
2
/5 +
3
/7 .. +
n
/((n*2) + 1).
#include <stdio.h>
#include <conio.h>
void main()
{
float n,i,sum=0.0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
sum=sum+(i/((i*2)+1));
printf("\nTHE SUMMATON S: %0.2f",sum);
getch();
}
9) Write a program to compute the factorial of a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1,n;
unsigned long fact=1;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=1;i<=n;i+=1)
{
fact=fact*i;
}
printf("\nFACTORAL OF %d S %lu",n,fact);
getch();
}
10) Write program to print the fibonacci series up to a given number.
#include <stdio.h>
#include <conio.h>
void main()
1(
{
int num,i;
unsigned long n1=0,n2=1,s;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
printf("\nFBONACC SERES UPTO %d NUMBERS S: \n",num);
printf("%lu %lu",n1,n2);
for(i=1;i<=num-2;i++)
{
s=n1+n2;
printf(" %lu",s);
n1=n2;
n2=s;
}
getch();
}
11) Write a program to evaluate
1
/1 Factorial +
2
/2 Factorial +
n
/n Factorial.
#include <stdio.h>
#include <conio.h>
void main()
{
float n,i,j,sum=0.0,fact=1.0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
{
fact=1.0;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
sum=sum+(i/fact);
}
printf("\nTHE SUMMATON S: %0.2f",sum);
getch();
}
C) Combination of Loop Conditional Statements
1) Write a program to print the prime numbers within a given number.
#include <stdio.h>
2#
#include <conio.h>
main()
{
int n,i,j,c;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nPRME NUMBERS WTHN %d\ ARE:\n",n);
for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
printf("%d ",i);
}
getch();
}
2) Write a program to check a given number is prime or not.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i,c=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf("\n%d S A PRME NUMBER",n);
else
printf("\n%d S NOT A PRME NUMBER",n);
getch();
}
Alternative Method
21
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i,flag=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i!=0)
flag=1;
}
if(flag==0)
printf("\n%d S A PRME NUMBER",n);
else
printf("\n%d S NOT A PRME NUMBER",n);
getch();
}
3) Write a program to check a given number is Palindrome or not. A number is
said to be Palindrome if the reverse of the number is equal to the number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num,temp,pal,sum=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
temp=num;
do
{
pal=num%10;
num=num/10;
sum=(sum*10)+pal;
}
while(num!=0);
if(temp==sum)
printf("\n%lu S A PALNDROM NUMBER",temp);
else
printf("\n%lu S NOT A PALNDROM NUMBER",temp);
getch();
}
22
4) Write a program to check a given number is Armstrong or not. A number is
said to be Armstrong if sum of the cube of the individual digit is equal to the
number.
Example: - 153 = (1)
3
+ (5)
3
+ (3)
3
.
#include <stdio.h>
#include <conio.h>
void main()
{
int num,num1,digit,arm=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
num1=num;
do
{
digit=num%10;
num=num/10;
arm=arm+(digit*digit*digit);
}
while(num!=0);
if(arm==num1)
printf("\n%d S AN AMSTORNG NUMBER",num1);
else
printf("\n%d S NOT AN AMSTRONG NUMBER",num1);
getch();
}
Triangles
1) Write a program to print a triangle like the following: -
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
23
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("1 ");
}
printf("\n");
}
getch();
}
2) Write a program to print a triangle like the following: -
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}
3) Write a program to print a triangle like the following: -
1
2 3
4 5 6
7 8 9 10
11 12 13 14
24
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j,k=1;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k);
k++;
}
printf("\n");
}
getch();
}
4) Write a program to print a triangle like the following: -
1
1 1 1
1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j,k,s=40;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=s;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("1");
for(k=i-1;k>0;k--)
printf("1");
printf("\n");
2
s--;
}
getch();
}
5) Write a program to print a triangle (Floyd's Triangle) like the following: -
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
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,j,k,s=40;
clrscr();
printf("\nENTER THE NUMBER OF LNES: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=s;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("%d",k);
for(k=i-1;k>0;k--)
printf("%d",k);
printf("\n");
s--;
}
getch();
}
Arrays
1) Write a program to create an array. Print the values and addresses of each
elements of the array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5];
int i,n=5;
26
clrscr();
for(i=0;i<n;i++)
{
printf("\nENTER THE NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
printf("ARRAY ELEMENT-%d: VALUE=%d, ADDRESS=
%u\n",i+1,a[i],&a[i]);
getch();
}
2) Write a program to create an array of student's ages. Print the average age.
#include <stdio.h>
#include <conio.h>
void main()
{
int age[10],i ;
float avg,sum=0.0;
clrscr();
for(i=0;i<10;i++)
{
printf("\nENTER AGE-%d: ",i+1);
scanf("%d",&age[i]);
sum=sum+age[i];
}
printf("\nNPUT AGES ARE: ");
for(i=0;i<10;i++)
printf("%d ",age[i]);
avg=sum/10;
printf("\nTHE AVERAGE AGE S: %0.2f",avg);
getch();
}
3) Write a program to create an array. Print the highest and lowest number in the
array.
#include <stdio.h>
#include <conio.h>
void main()
{
int val[5],h,l,i;
clrscr();
for(i=0;i<5;i++)
{
27
printf("\nENTER VALUE-%d: ",i+1);
scanf("%d",&val[i]);
}
l=val[0];
h=val[0];
for(i=0;i<5;i++)
{
if(val[i]>h)
h=val[i];
else
{
if(val[i]<l)
l=val[i];
}
}
printf("\nHGHEST VALUE S %d",h);
printf("\nLOWEST VALUE S %d",l);
getch();
}
4) Write a program to insert a given number in the array in a given position.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5],i,j=5,k,val,pos;
clrscr();
for(i=0;i<5;i++)
{
printf("\nENTER NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
}
printf("\nENTER VALUE TO NSERT: ");
scanf("%d",&val);
printf("\nENTER POSTON TO NSERT: ");
scanf("%d",&pos);
j++;
for(k=j;k>=pos;k--)
{
a[k]=a[k-1];
}
a[--pos]=val;
printf("\nTHE ARRAY S: ");
for(i=0;i<j;i++)
{
2&
printf("\n%d",a[i]);
}
getch();
}
5) Write a program to create an array. Compute the Mean, Variance & Standard
Deviation of the array.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int a[10],i,n=5;
float mean,temp,var,sd,sum=0.0;
clrscr();
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
sum=sum+a[i];
}
mean=sum/n;
sum=0.0;
for(i=0;i<n;i++)
{
temp=a[i]-mean;
sum=sum+(temp*temp);
}
var=sum/n;
sd=sqrt(var);
printf("\nMEAN = %0.2f",mean);
printf("\nVARANCE = %0.2f",var);
printf("\nSTANDARD DAVATON = %0.2f",sd);
getch();
}
Note: - To use the sqrt() function <math.h> header file must be included.
6) There are 5 groups of employees in an organization. Write a program to draw
a histogram showing given number of employees in each group.
#include <stdio.h>
#include <conio.h>
#define n 5
void main()
2(
{
int g[n],i,j;
clrscr();
for(i=0;i<n;i++)
{
printf("\nENTER HOW MANY EMPLOYEES ARE N GROUP-%d:
",i+1);
scanf("%d",&g[i]);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("GROUP-%d",i+1);
printf("%c",221);
for(j=0;j<=g[i];j++)
{
printf("%c",220);
}
printf(" %d",g[i]);
printf("\n");
}
getch();
}
Matrix
1) Write a program to create a matrix and display a matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],i,j;
clrscr();
printf("\nENTER VALUES FOR THE MATRX:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nTHE VALUES OF THE MATRX ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",a[i][j]);
printf("\n");
}
getch();
3#
}
2) Write a program to create two matrixes. Add the values of the two matrixes
and store it in another matrix. Display the new matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3],i,j;
clrscr();
printf("\nENTER VALUES FOR MATRX A:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRX B:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nTHE VALUES OF MATRX C ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("\n");
}
getch();
}
3) Write a program to create two matrixes. Multiply the values of the two matrixes
and store it in another matrix. Display the new matrix.
#include <stdio.h>
#include <conio.h>
#define max 3
void main()
{
int a[max][max],b[max][max],c[max][max],i,j,k;
printf("\nENTER VALUES FOR MATRX-A\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
31
printf("ENTER VALUE FOR A(%d,%d): ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nENTER VALUES FOR MATRX-B\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("ENTER VALUE FOR B(%d,%d): ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
c[i][j]=0;
for(k=0;k<max;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("\nVALUES OF MATRX-A\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nVALUES OF MATRX-B\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
printf("\nVALUES OF MATRX-C\n");
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
32
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
4) Write a program to create a matrix. Add the diagonal elements of the matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3],trace=0,i,j;
clrscr();
printf("\nENTER VALUES OF THE MATRX:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nTHE VALUES OF THE MATRX ARE:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",a[i][j]);
printf("\n");
}
for(i=0;i<3;i++)
trace+=a[i][i];
printf("THE SUMMATON OF THE DAGONAL ELEMENTS OF THE
MATRX S %d",trace);
getch();
}
String or Array of Characters
1) Write a program which will accept Name, Age, Sex and Qualification and
display the same.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i,j;
char label[5][20]={"NAME","AGE","SEX","QUALFCATON"},a[5][10];
33
clrscr();
for(i=0;i<4;i++)
{
printf("\nENTER YOUR %s: ",label[i]);
gets(a[i]);
}
for(i=0;i<4;i++)
printf("\n%s S YOUR %s\n",a[i],label[i]);
getch();
}
2) Write a program to find out the length of a given string without using the library
function strlen().
#include <stdio.h>
#include <conio.h>
void main()
{
char str[50];
int len;
clrscr();
printf("\nENTER A STRNG: ");
gets(str);
for(len=0;str[len]!=NULL;len++);
printf("\nTHE LENGTH OF THE STRNG S %d",len);
getch();
}
3) Write a program to print the reverse of a given string.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch[100];
int i,len;
clrscr();
printf("\nENTER A STRNG: ");
gets(ch);
len=strlen(ch);
printf("\nTHE STRNG N THE REVERSE ORDER: ");
for(i=len-1;i>=0;i--)
printf("%c",ch[i]);
getch();
}
34
4) Write a program to check if a given string is palindrome or not. A string is said
to be palindrome if the reverse of the string is equal to the string.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int i,len,flag=0;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]==a[len-i-1])
flag=flag+1;
}
if(flag==len)
printf("\nTHE STRNG S PALNDROM");
else
printf("\nTHE STRNG S NOT PALNDROM");
getch();
}
5) Write a program to count the number of words including spaces and excluding
spaces in a given string. Two words are separated by single space.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,sp=0;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
for(len=0;len<=a[len];len++);
for(i=0;i<len;i++)
{
if(a[i]==' ')
sp=sp+1;
}
printf("\nNUMBER OF WORDS NCLUDNG SPACES: %d",len);
printf("\nNUMBER OF WORDS EXCLUDNG SPACES: %d",len-sp);
getch();
3
}
6) Write a program to count the number of vowels in a given string.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,vow=0;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' || a[i]=='' ||
a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U')
vow=vow+1;
}
printf("\nTHERE ARE %d VOWELS N THE STRNG",vow);
getch();
}
7) Write a program to count the number of words in a given string. Two words are
separated by one or more blank spaces.
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,word=1;
clrscr();
printf("\nENTER A STRNG: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]!=' ' && a[i+1]==' ')
word=word+1;
}
printf("\nTHERE ARE %d WORDS N THE STRNG",word);
getch();
}
36
8) Write a program to print a given string in lower case.
#include <stdio.h>
#include <conio.h>
void main()
{
char s[100];
int len,i;
clrscr();
printf("\nENTER A SENTENSE: ");
gets(s);
len=strlen(s);
printf("\n");
for(i=0;i<len;i++)
{
if(s[i]>=65 && s[i]<=90)
printf("%c",s[i]+32);
else
printf("%c",s[i]);
}
getch();
}
9) Write a program to print a given string in upper case.
#include <stdio.h>
#include <conio.h>
void main()
{
char s[100];
int len,i;
clrscr();
printf("\nENTER A SENTENSE: ");
gets(s);
len=strlen(s);
printf("\n");
for(i=0;i<len;i++)
{
if(s[i]>=97 && s[i]<=122)
printf("%c",s[i]-32);
else
printf("%c",s[i]);
}
getch();
}
37
10) Write a program to print the string in first case.
#include <stdio.h>
#include <conio.h>
void main()
{
char s[100];
int len,i;
clrscr();
printf("\nENTER A SENTENSE: ");
gets(s);
len=strlen(s);
printf("\n");
for(i=0;i<len;i++)
{
if((i==0 && s[i]>=97 && s[i]<=122) || (s[i-1]==32 && s[i]>=97 &&
s[i]<=122))
printf("%c",s[i]-32);
else
{
if(i!=0 && s[i-1]!=32 && s[i]>=65 && s[i]<=90)
printf("%c",s[i]+32);
else
printf("%c",s[i]);
}
}
getch();
}
Number System
1) Write a program to convert a decimal number to its equivalent binary number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A DECMAL NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu N BNARY FORMAT: ",dec);
while(dec>0)
{
a[c]=dec%2;
3&
dec=dec/2;
c++;
}
for(i=c-1;i>=0;i--)
printf("%d",a[i]);
getch();
}
2) Write a program to convert a decimal number to its equivalent octal number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu N OCTAL FORMAT: ",dec);
while(dec>0)
{
a[c]=dec%8;
dec=dec/8;
c++;
}
for(i=c-1;i>=0;i--)
printf("%d",a[i]);
getch();
}
3) Write a program to convert a decimal number to its equivalent hexadecimal
number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long dec;
int a[25],c=0,i;
clrscr();
printf("\nENTER A DECMAL NUMBER: ");
scanf("%lu",&dec);
printf("\n%lu N HEXADECMAL FORMAT: ",dec);
while(dec>0)
{
3(
a[c]=dec%16;
dec=dec/16;
c++;
}
for(i=c-1;i>=0;i--)
{
if(a[i]>=10)
printf("%c",a[i]+55);
else
printf("%d",a[i]);
}
getch();
}
4) Write a program to convert a binary number to its equivalent decimal number.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned long num;
int digit,i,pos=0,pow=1,dec=0;
clrscr();
printf("\nENTER A BNARY NUMBER: ");
scanf("%lu",&num);
printf("\nDECMAL EQUVALANT OF %lu S ",num);
while(num!=0)
{
pow=1;
digit=num%10;
num=num/10;
for(i=1;i<=pos;i++)
pow=pow*2;
pos++;
dec=dec+(pow*digit);
}
printf("%d",dec);
getch();
}
Function
1) Write a program to print Hello World in the main function and Welcome To
C in another function.
#include <stdio.h>
4#
#include <conio.h>
void message(void);
main()
{
clrscr();
printf("\nHELLO WORLD");
message();
getch();
}
void message()
{
printf("\nWELCOME TO C");
}
2) Write a program to add three given numbers using function.
#include <stdio.h>
#include <conio.h>
sum(int,int,int);
void main()
{
int a,b,c,d;
clrscr();
printf("\nACCEPT VALUE FOR a,b,c:\n");
scanf("%d %d %d",&a,&b,&c);
d=sum(a,b,c);
printf("\nSUM OF %d,%d and %d S %d",a,b,c,d);
getch();
}
sum(int x,int y,int z)
{
int temp;
temp=x+y+z;
return(temp);
}
3) Write a program to calculate the tax of n given number of employees. Use a
separate function to calculate the tax. Tax is 20% of basic if basic is less than
9000 otherwise tax is 25% of basic.
#include <stdio.h>
#include <conio.h>
void cal(void);
void main()
{
int i,n;
41
clrscr();
printf("\nENTER THE NUMBER OF THE EMPLOYEES: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
cal();
getch();
}
void cal()
{
int basic;
float tax;
printf("\nENTER THE AMOUNT OF BASC: ");
scanf("%d",&basic);
if(basic<9000)
tax=basic*20/100;
else
tax=basic*25/100;
printf("\nTHE AMOUNT OF TAX S %0.2f\n",tax);
}
4) Write a program to compute the root of a number using function.
#include <stdio.h>
#include <conio.h>
unsigned long root(int);
void main()
{
int x;
unsigned long res;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&x);
res=root(x);
printf("\nROOT OF %d S %lu",x,res);
getch();
}
unsigned long root(int a)
{
unsigned long b;
b=a*a;
return(b);
}
5) Write a program to evaluate a
b
using function.
#include <stdio.h>
42
#include <conio.h>
unsigned long power(int,int);
void main()
{
int num,pow;
unsigned long res;
clrscr();
printf("\nENTER THE NUMBER: ");
scanf("%d",&num);
printf("\nENTER THE POWER: ");
scanf("%d",&pow);
res=power(num,pow);
printf("\n%d TO THE POWER %d S %lu",num,pow,res);
getch();
}
unsigned long power(int a,int b)
{
int i;
unsigned long prod=1;
for(i=1;i<=b;i++)
prod=prod*a;
return(prod);
}
6) Write a program to print the following series using function.
9 25 57 121 249 505 1017 2041.
#include <stdio.h>
#include <conio.h>
void main()
{
int num=9,i;
clrscr();
printf("%d ",num);
for(i=4;i<=10;i++)
{
num=num+pow(2,i);
printf("%d ",num);
}
getch();
}
pow(int a,int b)
{
int prod=1,j;
for(j=1;j<=b;j++)
43
prod=prod*a;
return(prod);
}
7) Write a program to check a number is prime or not using function.
#include <stdio.h>
#include <conio.h>
void main()
{
int num,res=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
res=prime(num);
if(res==0)
printf("\n%d S A PRME NUMBER",num);
else
printf("\n%d S NOT A PRME NUMBER",num);
getch();
}
int prime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i!=0)
continue;
else
return 1;
}
return 0;
}
8) Write a program to find out the maximum number in an array using function.
#include <stdio.h>
#include <conio.h>
max(int [],int);
void main()
{
int a[]={10,5,45,12,19};
int n=5,m;
clrscr();
m=max(a,n);
printf("\nMAXMUM NUMBER S %d",m);
44
getch();
}
max(int x[],int k)
{
int t,i;
t=x[0];
for(i=1;i<k;i++)
{
if(x[i]>t)
t=x[i];
}
return(t);
}
9) Write a program to evaluate 1/Factorial of 1 + 2/Factorial of 2 . + n/Factorial
of n using function.
#include <stdio.h>
#include <conio.h>
fact(float);
void main()
{
float i,sum=0.0,n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
sum=sum+(i/fact(i));
printf("\nTHE SUMMATON S: %0.2f",sum);
getch();
}
fact(float x)
{
if(x==1)
return(1);
else
return(x*fact(x-1));
}
Recursion
1) Write a recursive function to print the factorial of a number.
#include <stdio.h>
#include <conio.h>
unsigned long fact(int);
4
void main()
{
unsigned long f;
int n;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
f=fact(n);
printf("\nFACTORAL OF %d S %ld",n,f);
getch();
}
unsigned long fact(int a)
{
unsigned long fac;
if(a==1)
return(1);
else
fac=a*fact(a-1);
return(fac);
}
2) Write a recursive function to print the fibonacci series up to a given number.
#include <stdio.h>
#include <conio.h>
unsigned long fib(int);
void main()
{
int n,i;
unsigned long f;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
printf("\nTHE FBONNAC SERES UPTO %d NUMBERS S:\n",n);
for(i=0;i<n;i++)
{
f=fib(i);
printf("%lu ",f);
}
getch();
}
unsigned long fib(int x)
{
unsigned long res;
if(x==0)
return(0);
46
else
if(x==1)
return(1);
else
{
res=fib(x-1)+fib(x-2);
return(res);
}
}
3) Write a recursive function to print a given number in reverse order.
#include <stdio.h>
#include <conio.h>
int reverse(unsigned long);
void main()
{
unsigned long num;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%lu",&num);
printf("\nREVERSE OF %lu S ",num);
reverse(num);
getch();
}
int reverse(unsigned long n)
{
int dig;
if(n==0)
return 1;
else
{
dig=n%10;
n=n/10;
printf("%d",dig);
reverse(n);
}
}
4) Write a recursive function to evaluate x
y
.
#include <stdio.h>
#include <conio.h>
pow(int,int);
void main()
{
47
int x,y,pow;
clrscr();
printf("\nENTER A NUMBER FOR x: ");
scanf("%d",&x);
printf("\nENTER A NUMBER FOR y: ");
scanf("%d",&y);
pow=power(x,y);
printf("\nx TO THE POWER y S: %d",pow);
getch();
}
power(int a,int b)
{
int prod;
if(b==0)
return(1);
else
{
prod=a*power(a,b-1);
return(prod);
}
}
5) Write a program to implement Tower Of Hanoi.
#include <stdio.h>
#include <conio.h>
void hanoi(char,char,char,int);
void main()
{
int num;
clrscr();
printf("\nENTER NUMBER OF DSKS: ");
scanf("%d",&num);
printf("\nTOWER OF HANO FOR %d NUMBER OF DSKS:\n", num);
hanoi('A','B','C',num);
getch();
}
void hanoi(char from,char to,char other,int n)
{
if(n<=0)
printf("\nLLEGAL NUMBER OF DSKS");
if(n==1)
printf("\nMOVE DSK FROM %c TO %c",from,other);
if(n>1)
{
hanoi(from,other,to,n-1);
4&
hanoi(from,to,other,1);
hanoi(to,from,other,n-1);
}
}
Sorting
1) Write a program to sort an array using Bubble Sort Algorithm.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[15],i,j,n=10,temp;
clrscr();
printf("\nENTER VALUES FOR THE ARRAY:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nTHE SORTED ARRAY S:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
2) Write a program to sort an array using Quick Sort Algorithm.
#include <stdio.h>
#include <conio.h>
void qsort();
int n;
void main()
{
int a[100],i,l,r;
clrscr();
4(
printf("\nENTER THE SZE OF THE ARRAY: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
}
printf("\nTHE ARRAY ELEMENTS BEFORE SORTNG: \n");
for(i=0;i<n;i++)
{
printf("%5d",a[i]);
}
l=0;
r=n-1;
qsort(a,l,r);
printf("\nTHE ARRAY ELEMENTS AFTER SORTNG: \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
void qsort(int b[],int left,int right)
{
int i,j,p,tmp,finished,k;
if(right>left)
{
i=left;
j=right;
p=b[left];
finished=0;
while (!finished)
{
do
{
++i;
}
while ((b[i]<=p) && (i<=right));
while ((b[j]>=p) && (j>left))
{
--j;
}
if(j<i)
finished=1;
else
{
tmp=b[i];
b[i]=b[j];
#
b[j]=tmp;
}
}
tmp=b[left];
b[left]=b[j];
b[j]=tmp;
qsort(b,left,j-1);
qsort(b,i,right);
}
return;
}
Searching
1) Write a program to search a key number in an array using Sequential Search
Method.
#include <stdio.h>
#include <conio.h>
main()
{
int arr[]={12,23,78,98,67,56,45,19,65,9},key,i,flag=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&key);
for(i=0;i<10;i++)
{
if(key==arr[i])
flag=1;
}
if(flag==1)
printf("\nTHE NUMBER %d EXSTS N THE ARRAY",key);
else
printf("\nTHE NUMBER %d DOES NOT EXST N THE
ARRAY",key);
getch();
}
2) Write a program to search a key number in an array using Binary Search
Method or Dictionary Search Method.
#include <stdio.h>
#include <conio.h>
void sort(int *);
int search(int *,int);
void main()
1
{
int a[10],i,j,temp,key,flag;
clrscr();
for(i=0;i<10;i++)
{
printf("\nENTER NUMBER-%d: ",i+1);
scanf("%d",&a[i]);
}
sort(a);
printf("\nTHE SORTED ARRAY S: ");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\nENTER A NUMBER TO SEARCH: ");
scanf("%d",&key);
flag=search(a,key);
if(flag==1)
printf("\nTHE NUMBER %d EXSTS",key);
else
printf("\nTHE NUMBER %d DOES NOT EXST ARRAY",key);
getch();
}
void sort(int *x)
{
int i,j,temp;
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}
int search(int *x,int k)
{
int low=0,high=10,mid,res=0;
while(high>=low)
{
mid=(high+low)/2;
if(k==x[mid])
{
res=1;
2
break;
}
else
{
if(k>x[mid])
low=mid+1;
else
high=mid-1;
}
}
return res;
}
Pointers
1) Write a program to give examples & (Address Of) and * (Value At Address)
operator.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=3,*j;
clrscr();
j=&i;
printf("\nVALUE OF i S [i]: %d",i);
printf("\nVALUE OF i S [*(&i)]: %d",*(&i));
printf("\nADDRESS OF i S [&i]: %u",&i);
printf("\nVALUE OF j S [*(&j)]: %u",*(&j));
printf("\nADDRESS OF j S [&j]: %u",&j);
printf("\nADDRESS OF i S [j]: %u",j);
getch();
}
2) Write a program to swap the address of two variables.
#include <stdio.h>
#include <conio.h>
void swap(int *,int *);
void main()
{
int a=10,b=20;
clrscr();
printf("\nVALUES OF a AND b BEFORE SWAPNG ARE %d %d",a,b);
swap(&a,&b);
printf("\nVALUES OF a AND b AFTER SWAPNG ARE %d %d",a,b);
3
getch();
}
void swap(x,y)
int *x,*y;
{
int t;
t=*x;
*x=*y;
*y=t;
}
3) Write a program to compute the average of n given numbers using pointers.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,*p,sum=0,i;
float avg;
clrscr();
printf("\nHOW MANY NUMBERS: ");
scanf("%d",&n);
p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMEMORY ALLOCATON UNSUCCCESSFUL");
exit();
}
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER %d: ",i+1);
scanf("%d",(p+i));
}
for(i=0;i<n;i++)
sum=sum+*(p+i);
avg=(float)sum/n;
printf("\nTHE AVERAGE OF THE NUMBERS S %0.2f",avg);
getch();
}
4) Write a program to sort n given numbers using pointers.
Hint: - Use Bubble Sort Algorithm.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
4
void main()
{
int n,*p,i,j,temp;
clrscr();
printf("\nHOW MANY NUMBER: ");
scanf("%d",&n);
p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMEMORY ALLOCATON UNSUCCESSFUL");
exit();
}
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER %d: ",i+1);
scanf("%d",p+i);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(*(p+i)<*(p+j))
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
}
}
printf("\nTHE SORTED NUMBERS ARE:\n");
for(i=0;i<n;i++)
printf("%d ",*(p+i));
getch();
}
5) Write a program to concatenate two given string without using the library
function strcat().
#include <stdio.h>
#include <conio.h>
void strconc(char *,char *);
char *s3;
void main()
{
char *str1,*str2;
clrscr();