C Sample Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 61

If statement

Nested if
Loop
For loop
Nested For loop
Pointer
structure
/*A program that convert

Small letter into large and

Large into small */

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

char a;

printf("inter a world ");

a=getche();

if(a>=65&&a<=97)

printf("\n\n Small = %c",a+32);

else

printf("\n\n Capital = %c",a-32);

getch();

// to find Odd and even number

#include<conio.h>

#include<stdio.h>

void main()

{
clrscr();

int a;

printf("\n\ninter your digit = ");

scanf("%d",&a);

if(a%2==0)

printf("\n\neven number");

else

printf("\n\nOdd number");

getch();

/////////////////////////////////////////////////////////////////////
/

// to give Bonus for an employ

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int hdate,cdate,tenure;

printf("enter your h date= ");

scanf("%d",&hdate);

printf("\nenter current date= ");

scanf("%d",&cdate);

tenure=cdate-hdate;

printf("\ntenure = %d ",tenure);

if (tenure>5)
{

printf("\ncongrate");

printf(" you got bonus of 500");

if(tenure<5)

printf("\nsorry no discount");

getch();

//////////////////////////////////////////////////////////////////////

// discount for customer

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

float amount,discount;

printf(" enter amount = ");

scanf("%f",&amount);

if(amount>2000.0)

float t;

t=(10.0/100.0);

discount=amount*t;

amount-=discount;

printf("amount payable = %f",amount);

else
printf("sorrry no discount");

getch();

//////////////////////////////////////////////////////////////////////

//to tell the character is small or capital

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

char a;

printf("enter a character ");

scanf("%c",&a);

if(a>=65&&a<=91)

printf("\nyou enter a capital letter");

if(a>=97&&a<123)

printf("\nyou enter a small letter");

getch();

//////////////////////////////////////////////////////////////////////

//to tell the alphabet is vowel or consonant

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

char a;
printf("enter a cheracter ");

a=getche();

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

printf("\nyou enter a vowel");

else

printf("\nyou enter a consonant");

getch();

//////////////////////////////////////////////////////////////////////

//to print digit in reverse order

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int z,a,b,c,d,e,f;

printf("enter a 4 digit number");

scanf("%d",&z);

a=z/1000;

b=z%1000;

c=b/100;

d=b%100;

e=d/10;

f=d%10;
printf("%d%d%d%d",f,e,c,a);

getch();

//////////////////////////////////////////////////////////////////////

//to find area of circle

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

float pi,r,a;

pi=3.14;

printf("enter value for radius = ");

scanf("%f",&r);

a=pi*r*r;

printf("\narea of the circle having radius %.2f\n is= %.2f",r,a);

getch();

//////////////////////////////////////////////////////////////////////

//to print you are pass are fail

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int marks;

printf("enter your marks ");


scanf("%d",&marks);

if (marks>=50)

printf("\ncongratulation you are pass");

else

printf("\nYou are fail\nBest of luck next time");

getch();

//////////////////////////////////////////////////////////////////////

//to print you are young or old

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a;

printf("enter your age in years ");

scanf("%d",&a);

if (a<=40)

printf("\n\nyou are pretty Young");

else

printf("\n\nYou are old ");

printf("but you are realy an experienced person");

getch();

//////////////////////////////////////////////////////////////////////
//to find the percentage of your marks

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

float a,b,c;

//int a,b,c;

printf("enter your marks ");

scanf("%f",&a);

printf("\nenter total marks ");

scanf("%f",&b);

c=(a/b)*100.0;

printf("\npercentage =%f",c);

getch();

//////////////////////////////////////////////////////////////////////

//to find force

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

float f,m,a;

printf("enter value for mass = ");

scanf("%f",&m);

printf("enter value for acceleration = ");


scanf("%f",&a);

f=m*a;

printf("force =%f ",f);

getch();

//////////////////////////////////////////////////////////////////////

//calculater

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a,b;

printf("\nenter a number = ");

scanf("%d",&a);

char op;

printf("\nenter operater ");

op=getche();

printf("\n\nenter another number = ");

scanf("%d",&b);

if(op=='+')

printf("\nAns= %d",a+b);

if(op=='-')

printf("\nAns= %d",a-b);

if(op=='*')

printf("\nAns= %d",a*b);

if(op=='/')
printf("\nAns %d",a/b);

getch();

//////////////////////////////////////////////////////////////////////

//to find large number By nested if

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a,b,c;

printf("\nenter 1st no = ");

scanf("%d",&a);

printf("\nenter 2nd no = ");

scanf("%d",&b);

printf("\nenter 3rd no = ");

scanf("%d",&c);

if(a>b)

if(a>c)

printf("\n large num = %d",a);

else

printf("\n largr num = %d",c);

else

if(b>a)

if(b>c)

printf("\n large num = %d",b);

else

printf("\n large num = %d",c);


else

printf("\n large num = %d",c);

getch();

//////////////////////////////////////////////////////////////////////

//to give insurance in a company by "NESTED IF"

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

char s,ms;

int age;

printf("\nenter sex 'm' or 'f' ");

s=getche();

printf("\nenter your merital status 'm' or 'u' ");

ms=getche();

printf("\nenter your age ");

scanf("%d",&age);

if(s=='m')

if(age>30)

if(ms=='m')

printf("\nYou are insured");

else

printf("\nNot insured");

else

printf("\nnot insured");
else

if(age>20)

if(ms=='m')

printf("\nYou are insured");

else

printf("\nnot insured");

else

printf("\nNot insured");

getche();

//////////////////////////////////////////////////////////////////////

//TO PRINT THE CHERACTER IS SMALL/CAPITALOR SPECIAL BY NASTED IF

#include<conio.h>

#include<stdio.h>

void main()

char ch;

printf("enter cheracter");

ch=getche();

if(ch>=65)

if(ch<=91)

printf("capital letter");

else

if(ch>=97)
if(ch<=123)

printf("small cheracter");

else

printff("special cheracter");

else

printf("special cheracter");

else

printf("special cheracter");

getch();

**********************************************************************
***************

//calculator by else if

#include<conio.h>

#include<stdio.h>

void main()

int n1,n2;

char op;

printf("Enter two numbers");

scanf("%d%d",&n1,&n2);

printf("\nenter operater ");

op=getche();

if(op=='+')

printf("\nsum= %d",n1+n2);

else if(op=='-')
printf("\nsubtriction = %d",n1-n2);

else if(op=='*')

printf("\nmultiplication = %d",n1*n2);

else printf("\ndevision =%d",n1/n2);

getch();

**********************************************************************
***************

//to show the number is devisable by 4 and 2

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a;

printf("Enter a number = ");

scanf("%d",&a);

if(a%2==0)

if(a%4==0)

printf("the number is devisible by 2 and 4 ");

else

printf("the number is not devisible by 2and 4");

else

printf("the number is not devisible by 2and 4");

getch();

}
**********************************************************************
***************

//to change currency by (else if)

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a;

char type;

printf("ENTRE THE AMOUNT OF CURRENCY = ");

scanf("%d",&a);

printf("ENTRE THE TYPE OF CURRENCY \n'd' FOR DOLLERS \n'r' FOR


RYALS \n'p' FOR POUND \n'D' FOR DARHAM");

type=getche();

if(type=='d')

printf("\nAMOUNT IN Rs = %d",a*78);

else if(type=='r')

printf("\nAMOUNT IN Rs = %d",a*22);

else if (type=='p')

printf("\nAMOUNT IN Rs = %d",a*15);

else if(type=='D')

printf("\nAMOUNT IN Rs = %d",a*24);

getch();

**********************************************************************
***************
SWITCH STATMENT
**********************************************************************
***************

// SIMPLE PROGRAM ON SWITCH STATEMENT

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

char ch;

printf("enter first cheracter of your star");

ch=getche();

switch(ch)

case 'a':

printf("\nyou will got a car today");

printf("\ncongratulation");

break;

case 'c' :

printf("\nyou will become a king today");

break;

case 'v':

printf("\nyou will become the king of india");

break;

defult:

printf("\nyou not know your star");

getch();
}

**********************************************************************
***************

//calculator by switch statement

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int n1,n2;

char op;

printf("Enter to numbers \n");

scanf("%d%d",&n1,&n2);

printf("enrter operater\n");

op=getche();

switch(op)

case '+':

printf("\naddation= %d",n1+n2);

break;

case '-':

printf("\nsubtriction = %d",n1-n2);

break;

case '*':

printf("\nmultiplication = %d",n1*n2);

break;

case '/':
if(n2==0)

printf("\ndevision not possible");

else

printf("\ndevision = %d", n1/n2);

getch();

**********************************************************************
***************

//to print color name by switch statement

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int c;

printf("enter value from 1-15");

scanf("%d",&c);

switch(c)

case 0:

textcolor(c);

cprintf("Tariq dirvi");

break;

case 1:

textcolor(c);

cprintf("Tariq");
break;

getch();

**********************************************************************
**

LOOP (REPETATIVE STRUCTURE)


**********************************************************************
**

//to print name in color form

#include<conio.h>

#include<stdio.h>

#include<ctype.h>

void main()

clrscr();

int i;

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

textcolor(i);

cprintf("Tariq\n");

getch();

**********************************************************************
***************

// color print of name

#include<conio.h>
#include<stdio.h>

void main()

clrscr();

int i;

for(i=0;i<=15;++i)

textcolor(i);

cprintf("* ");

printf(" \n\n * Tariq *\n\n");

for(i=0;i<=15;++i)

textcolor(i);

cprintf("* ");

}getch();

**********************************************************************
***************

//to print Even number up to 200 BY FOR LOOP;

#include<conio.h>

#include<stdio.h>

void main()

{
clrscr();

int i;

for(i=0;i<=200;++i)

if(i%2==0)

printf("%d ",i);

getch();

**********************************************************************
***************

//To show the table of a number

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a;

printf("enter a number");

scanf("%d",&a);

for(int i=0;i<=16;i++)

printf("%d*%d= %d\n",a,i,a*i);

getch();

**********************************************************************
***************

// to show the aski codes by For loop

#include<conio.h>

#include<stdio.h>
void main()

clrscr();

for(int i=0;i<=256;i++)

printf("%c = %d ",i,i);

getch();

**********************************************************************
**************

// to print the name in different color and delay after one

#include<conio.h>

#include<stdio.h>

#include<dos.h>

void main()

clrscr();

for(int i =1;i<=15;++i)

textcolor(i);

cprintf("\nTariq Dirvi");

delay(500);

getch();

**********************************************************************
***************

// to find factorial of a number


#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int n,i;

long fact=1;

printf("enter a number ");

scanf("%d",&n);

if(n==0)

fact*=1;

else

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

fact*=i;

printf(" Factorial =%d\t",fact);

getch();

**********************************************************************
***************

// to find odd number and then add these umbers

#include<conio.h>

#include<stdio.h>

void main()

{
int sum=1;

clrscr();

for(int i=21;i<=40;++i)

if(i%2==1)

printf("%d\n",i);

sum+=i;

printf("\n%d\n",sum);

getch();

**********************************************************************
***************

// to print numbers by Do while loop

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i=1;

do

printf("%d\t",i);

i++;

}while(i<=15);

getch();
}

**********************************************************************
***************

// nested for loop

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j;

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

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

printf("%d\t %d\n",i,j);

getch();

**********************************************************************
***************

//to calculate the length of string By while loop;

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int count =0;

printf("type some text\n");

while(getche()!='\r')

count++;
printf("\ncharacters =%d",count);

getch();

**********************************************************************
***************

// While loop Post tested loop

#include<conio.h>

#include<stdio.h>

void main()

int i=1;

clrscr();

while(i<=10)

printf("\n%d",i);

i++;

getch();

**********************************************************************
***************

//program to find arm strong numbers

#include<conio.h>

#include<stdio.h>

void main()

clrscr();
int i,a,b,c,d,e;

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

a=i/100;

b=i%100;

c=b/10;

d=b%10;

e=(a*a*a)+(c*c*c)+(d*d*d);

if(e==i)

printf("%d\t",i);

getch();

**********************************************************************
***************

//do while loop

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

char ch;

do

printf("enter a cheracter\n");

scanf("%c",&ch);

if(ch>=65&&ch<=91)
printf("%c\n",ch+3);

else if (ch>=97&&ch<123)

printf("%c\n",ch-32);

else

printf("%c\n",ch);

printf("enter another character(y/n)\n");

}while((ch=getchar())!='n') ;

getch();

**********************************************************************
***************

//display a result

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j;

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

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

printf("*");

printf("\n");

getch();

}
**********************************************************************
**************

// TO FIND PRIME NUMBER

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,value,p=0;

printf("enter a number = ");

scanf(" %d",&value);

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

if(value%i==0)

p=p+1;

if(p==0)

printf("prime number");

else

printf("not a prime number");

getch();

**********************************************************************
***************

FUNCTION
**********************************************************************
***************

// Function
#include<conio.h>

#include<stdio.h>

void name(void);

void main()

clrscr();

printf("**********\n");

printf("Tariq\n");

name();

getch();

void name()

for(int i=1;i<=10;++i)

printf("*");

**********************************************************************
***************

// to find area by function;

#define pi 3.1416

#include<conio.h>

#include<stdio.h>

float area(float r);

void main()

clrscr();

float a;
a=34.0;

float d;

d=area(a);

printf("Area is =%.3f",d);

getch();

float area(float r)

float c;

c=pi*r*r;

return c;

**********************************************************************
***************

// to find mul. of two numbers by function

#include<conio.h>

#include<stdio.h>

mul(int x,int y)

int a;

a=x*y;

return a;

void main()

int a,b,c;

printf("Enter two values");


scanf("%d%d",&a,&b);

c=mul(a,b);

printf("%d",c);

getch();

**********************************************************************
***************

// FUNCTION

#include<conio.h>

#include<stdio.h>

void line(int);

void num(int);

void main()

clrscr();

int a=50,b;

line(a);

printf("\nEnter a number\n");

line(a);

printf("\n");

scanf("%d",&b);

line(a);

printf("\n");

num(b);

printf("\n");

line(a);

getch();
}

void line(int x)

int a;

a=x;

for(int i=1;i<=x;i++)

printf("_");

void num(int x)

int a;

a=x;

if(a>0)

printf("Positive Number");

else

printf("Negative number");

**********************************************************************
***************

// Program that find odd or even number by Function

#include<conio.h>

#include<stdio.h>

void num(int a)

if(a%2==0)

printf("Even number");
else

printf("Odd");

void main()

clrscr();

int a;

printf("Enter a number");

scanf("%d",&a);

num(a);

getch();

**********************************************************************
***************

// to print you are fail or pass by Function

#include<conio.h>

#include<stdio.h>

void marks(int a)

if(a>=50)

printf("congrulation you are pass");

else

printf("You are fail Bestluck next time");

void main()

clrscr();
int a;

printf("Enter your marks");

scanf("%d",&a);

marks(a);

getch();

**********************************************************************
***************

//multiplication of two numbers by Function

#include<conio.h>

#include<stdio.h>

void mul();

void main()

clrscr();

mul();

getch();

void mul()

int a,b,c;

printf("ENTER TWO NUMBERS ");

scanf("%d%d",&a,&b);

c=a*b;

printf("%d",c);

}
**********************************************************************
***************

//TO FIND FACTORIAL OF A NUMBER BY FUNCTION(recursion)

#include<conio.h>

#include<stdio.h>

int fact(int n);

void main()

int a,b;

clrscr();

printf("Enter a number = ");

scanf("%d",&a);

b=fact(a);

printf("Factorial = %d",b);

getch();

int fact(int n)

if(n==0)

return(1);

else

return(n*fact(n-1));

**********************************************************************
***************

//TO PRINT NAME BY FUNCTION


#include<conio.h>

#include<stdio.h>

void line(void);

void main()

clrscr();

printf("\t\t\tTARIQ\n");

line();

printf("\t\t\tTARIQ\n");

line();

printf("\t\t\tTARIQ\n");

line();

printf("\t\t\tTARIQ\n");

line();

getch();

void line()

for(int i=1;i<=60;++i)

printf("-");

printf("\n");

**********************************************************************
***************

// calculator by Function

#include<conio.h>

#include<stdio.h>
float calc(float a,float b,char c);

void main()

clrscr();

float a,b,c,e;

printf("Enter Two numbers = \n");

scanf("%f%f",&a,&b);

printf("Enter Operater = ");

c=getche();

e=calc(a,b,c);

printf("\n answer =%.3f",e);

getch();

float calc( float a, float b, char c)

float d;

if(c=='+')

d=a+b;

return(d);

else if(c=='-')

d=a-b;

return(d);

else if(c=='*')
{

d=a*b;

return(d);

else if(c=='/')

d=a/b;

return(d);

**********************************************************************
***************

//ELECTRICITY BILL

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a,b,c;

printf("Enter your previous units = ");

scanf("%d",&a);

printf("enter your current units = ");

scanf("%d",&b);

printf("total units consume = %d ",c=b-a);

if(c>=100)

if (c<=200)

printf("\nprise = %d",c*2-100);
else

if(c>200)

printf("\nprice = %d ",c*3-300);

else

printf("\nprice = %d",c);

getch();

**********************************************************************
***************

// color full double line

#include<conio.h>

#include<stdio.h>

#include<dos.h>

void main()

int a,b;

clrscr();

for(int i=1;i<=80;++i)

textcolor(i);

cprintf("\xdb",i);
delay(200);

getch();

**********************************************************************
***************

//to produces sound with typing

#include<conio.h>

#include<stdio.h>

#include<dos.h>

void main()

char ch;

while((ch=getche())!='\r')

sound(ch*20);

delay(200);

nosound();

getch();

**********************************************************************
***************

//to finds prime numbers

#include<conio.h>

#include<stdio.h>

void main()
{

clrscr();

int i,value,p=0;

printf("enter a number = ");

scanf(" %d",&value);

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

if(value%i==0)

p=p+1;

if(p==0)

printf("prime number");

else

printf("not a prime number");

getch();

**********************************************************************
***************

// A FUNCTION FOR ENCODING AND DECODING

#include<conio.h>

#include<stdio.h>

char encode(char);

char decode(char);

void main()

clrscr();

char x,y;
x=encode('A');

printf("%c\n",x);

y=decode('c');

printf("%c",y);

getch();

char encode( char ch)

return(((ch+32)*2)/5);

char decode( char a)

return(((a-32)/2)*5);

**********************************************************************
***************

//A FUNCTION WHICH TAKE 5 DIGIT NUMBER AND FIND THE SUM

#include<conio.h>

#include<stdio.h>

int sum(int);

void main()

int x,y;

clrscr();

printf("enter the 5 digit number");

scanf("%d",&x);

y=sum(x);
printf("%d",y);

getch();

int sum(int x)

int a,b,c,d,e,f,g,h,i;

a=x/10000;

b=x%10000;

c=b/1000;

d=b%1000;

e=d/100;

f=d%100;

g=f/10;

h=f%10;

return(a+c+e+g+h);

**********************************************************************
***************

//TO FIND POWER OF A NUNBER BY FUNCTION(recursion)

#include<conio.h>

#include<stdio.h>

int power(int,int);

void main()

clrscr();

printf("%d",power(2,5));

getch();
}

int power(int x,int y)

if(y==1)

return(x);

else if(y==0)

return(1);

else

return(x*power(y-1));

**********************************************************************
***************

//A PROGRAM THAT CONVERT A CHARACTER FUNCTION

#include<conio.h>

#include<stdio.h>

char convert(char x)

if(x>=65&&x<90)

return(x+32);

else if(x>=97&&x<122)

return(x-32);

else

return(x);

void main()

clrscr();
char ch;

printf("enter a cheracter");

scanf("%c",&ch);

char a;

a=convert(ch) ;

printf("%c",a);

getch();

**********************************************************************
***************

ARRAY
**********************************************************************
***************

//a simple program on array 1

#include<conio.h>

#include<stdio.h>

void main()

int marks[4];

marks[0]= 46;

marks[1]=909;

marks[2]=45;

marks[3]=56;

printf("\n%d",marks[1]);

getch();

**********************************************************************
***************
// simple program on array 2

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int b[]={23,54,754,3246,452,466};

int a[4]={12,24,45,65};

printf("%d\n",a[2]);

printf("%d",b[3]);

getch();

**********************************************************************
***************

//assigning value to Array with the help of for loop

#include<conio.h>

#include<stdio.h>

void main()

int i;

int a[5];

for(i=0;i<6;i++)

printf("enter a number ");

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

clrscr();
for(i=0;i<6;i++)

printf("\n%d",a[i]);

getch();

**********************************************************************
***************

//To find the Average in array

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,a[10];

float sum=0.0;

float avg;

for(i=0;i<10;i++)

printf("enter a number");

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

for(i=0;i<10;i++)

sum+=a[i];

avg=(sum/10.0);

printf("average = %f",avg);

getch();

}
**********************************************************************
***************

// LINEAR SEARCH; TO FIND A NUMBER IN ARRY

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a[]={1,2,3,4,60,6,7,8,9,0};

int item,i,flog=-1;

printf("item to be search");

scanf("%d",&item);

for(i=0;i<10;i++)

if(item==a[i])

flog=i;

break;

if(flog==-1)

printf("item not found");

else

printf("item found at location %d",flog+1);

getch();

**********************************************************************
***************

//TO FIND MAXIMUM NUMBER IN ARRAY


#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int max;

int a[]={30,20,60,90,800,80,900,8000,4000,7000};

max=a[0];

int i;

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

if(a[i]>max)

max=a[i];

printf("MAXIMUM NUMBER = %d",max);

getch();

**********************************************************************
***************

//to arrange in descending order

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j,t,a[5]={10,2,5,3,1};

for(i=0;i<5;i++) // SORTING

for(j=0;j<5;j++)

if(a[i]>a[j])
{

t=a[j]; // SWAPING

a[j]=a[i];

a[i]=t;

for(i=0;i<5;i++)

printf("%d ",a[i]);

getch();

**********************************************************************
***************

//sorting an array ASSENDING

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j,t,a[5]={10,2,5,3,1};

for(i=0;i<5;i++) // SORTING

for(j=0;j<5;j++)

if(a[i]<a[j])

t=a[j]; // SWAPING

a[j]=a[i];

a[i]=t;

for(i=0;i<5;i++)
printf("%d ",a[i]);

getch();

**********************************************************************
***************

//BINARY SEARCH

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int lb,ub,item,mid;

int a[]={10,50,90,200,205,1000};

lb=0;

ub=5;

printf("enter item to be search");

scanf("%d",&item);

do

mid=(ub+lb)/2;

if(item>a[mid])

lb=mid+1;

if(item<a[mid])

ub=mid-1;

while(item!=a[mid]&&lb<=ub) ;

if(item==a[mid])
printf("item found it location of %d",mid+1);

else

printf("item not found");

getch();

**********************************************************************
***************

TWO DIMENTIONAL
ARRAY
//assign value to two dimensional array

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int a[2][2];

int i,j;

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

for(j=0;j<2;j++)

printf("enter value");

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

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

for(j=0;j<2;j++)

printf("%d\n",a[i][j]);

getch();
}

**********************************************************************
***************

//Addition of two matrix into a third one

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j;

int a[2][2]={{2,4},{4,3}};

int b[2][2]={{3,1},{5,4}};

int c[2][2];

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

for(j=0;j<2;j++)

c[i][j]=a[i][j]+b[i][j];

printf("Sum of the matrix");

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

for(j=0;j<2;j++)

printf("\n%d",c[i][j]);

getch();

**********************************************************************
***************

//MULTIPLICATION OF TWO MATRIX

#include<conio.h>

#include<stdio.h>
void main()

clrscr();

int i,j;

int a[2][2]={{2,1},{2,2}};

int b[2][2]={{3,1},{1,1}};

int c[2][2];

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

for(j=0;j<2;j++)

c[i][j]=0;

for(int k=0;k<2;k++)

c[i][j]+=a[i][k]*b[k][j];

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

for(j=0;j<2;j++)

printf("\n%d",c[i][j]);

getch();

**********************************************************************
***************

ARRAY OF STRING
// DELATION OF CHERACTER

#include<conio.h>

#include<stdio.h>

#include<string.h>
void main()

clrscr();

char str[]="asad ali khan";

int p;

printf("enter position of char");

scanf("%d",&p);

strcpy(&str[p],&str[p+1]);

puts(str);

getch();

**********************************************************************
***************

//TO PRINT NAME IN REVERSE ORDER

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j;

char str[5];

printf("enter your name\n");

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

scanf("%c",&str[i]);

for(j=i;j>=1;--j) // i will start from 7

printf("%c",str[j]);

getch();
}

**********************************************************************
***************

//STRING COPY

#include<conio.h>

#include<stdio.h>

#include<string.h>

void main()

char a[]="tariq";

char b[]="only";

strcpy(&a[],&b[]);

puts(a);

getch();

**********************************************************************
***************

TWO DIMENTTIONAL STRING(2D string)


//scan name from user

//and gives permission...............

#include<conio.h>

#include<stdio.h>

#include<string.h>

#define max 5

#define len 40

void main()

{
clrscr();

int i;

int enter=0;

char name[len];

char list[max][len]={"tariq only", "ali" ,"kahn" ,"jan" ,"murad "};

puts("Enter your full name");

gets(name);

for(i=0;i<max;i++)

if(strcmp(&list[i][0],name)==0) // string compare

enter=1;

break;

if (enter ==1)

printf(" Hello! %s welcome",name) ;

else

printf("you are not allowed");

getch();

**********************************************************************
***************

//CANCATINATION OF STRING

#include<conio.h>

#include<stdio.h>

#include<string.h>

void main()

{
char str1[10],str2[10];

puts("enter your first name ");

gets(str1);

puts("enter your second name");

gets(str2);

strcat(str1,str2);

puts(str1);

getch();

**********************************************************************
***************

//A PROGRAM THAT PRINT NAME WITH ITS ADDRESS

//STRLEN() IS ALSO USED.

#include<conio.h>

#include<stdio.h>

#include<string.h>

void main()

clrscr();

int i;

char name[80];

printf("enter your name");

gets(name);

for (i=0;i<strlen(name);i++)

printf("%u\t %c\t",&name,name[i]);

getch();

}
**********************************************************************
***************

You might also like