40 Examples of C Programming
40 Examples of C Programming
ﮐ ی
ا ﺎ ﺪھ
ﯽﺟﺮاﯾ
ﻣ
ﯽ ﺑﺎ
ﺷﻨﺪ
m
o.
c.
a.
gf
lo
s .b
ba
ab
6-
.6
.w
.w
w
#include<stdio.h>
int main()
m
o.
{
c.
a.
printf("Hello World \n ");
return 0; gf
lo
.b
}
s
ba
ab
6-
.6
.w
.w
w
www.66-abbas.blogfa.com
2
ﺟﻤﻊ دو ﻋﺪد ﺻﺤﯿﺢ
#include<stdio.h>
int main()
m
o.
{
c.
int a,b,sum;
a.
gf
printf("enter a \n"); lo
scanf("%d",&a);
s .b
printf("enter b \n");
ba
scanf("%d",&b);
ab
sum=a+b;
6-
printf("\"sum=%d\" \a \n",sum);
.6
.w
return 0;
.w
}
w
www.66-abbas.blogfa.com
3
Enterﺗﺸﺨﯿﺺ دھﻨﺪه ی دﮐﻤﮫ ی
#include<stdio.h>
#include<conio.h>
m
int main ()
o.
{
c.
char ch;
a.
printf("Enter Your Character\t");
gf
ch=getche();
lo
s .b
if(ch=='\r'){
ba
printf("\n'OK' \n%d",ch); }
ab
else{
6-
printf("\nError"); }
.6
getchar();
.w
return 0;
.w
w
www.66-abbas.blogfa.com
4
اﺳﺘﻔﺎده از دﺳﺘﻮرات ﺷﺮﻃﯽ
#include<stdio.h>
#include<conio.h>
int main(){
m
float f;
o.
clrscr();
c.
scanf("%f",&f);
a.
if(f>=0.&&f<5)
gf
printf("\"a\"");
else if(f>=5.&&f<10)
lo
printf("\"B\"");
.b
else if(f>=10.&&f<15)
s
ba
printf("\"c\"");
else if(f>=15)
ab
printf("Danger");
else
6-
printf("Error");
.6
return 0;
.w
}
.w
w
www.66-abbas.blogfa.com
5
ﺗﺸﺨﯿﺺ ﺣﺮوف ﺻﺪا دار
#include<stdio
#include<stdio..h>
#include<conio
#include<conio..h>
#include<dos.
#include<dos.h>
m
int main()
main(){ {
char ch;
ch;
o.
ch=
ch=getchar()
getchar();;
c.
switch(
switch(ch)
ch){
a.
case'a'
case'a':
case'A'
case'A':
gf
case'i'
case'i':
case'I'
case'I':
lo
case'u'
case'u':
.b
case'U'
case'U':
s
case'e'
case'e':
ba
case'E'
case'E':
case'o'
case'o':
ab
case'O'
case'O':
printf("
printf("\\"Vowel\
Vowel\"");
"");
6-
break;
default:
default:
.6
printf("
printf("Is
Is not a vowel")
vowel");;
.w
}
return 0;
.w
}
w
www.66-abbas.blogfa.com
6
ﺑﺮﻧﺎﻣﮫ ای ﮐﮫ ﺑﺮای ﺣﺮوف ﺑﺰرگ و ﺑﺮای اﻋﺪاد ﺧﻮد ﮐﺎراﮐﺘﺮ و ﺑﺮای ﺣﺮوف
.ﮐﻮﭼﮏ ﻣﻌﺎدل ﺣﺮف ﺑﺰرگ آن را ﺑﺮ ﻣﯿﮕﺮداﻧﺪ
#include<stdio.h>
#include<conio.h>
m
int main(){
o.
char ch;
c.
printf("\"Enter your character:\"");
a.
ch=getchar();
gf
clrscr();
lo
.b
if(ch>='0'&& ch<='9')
s
printf("Adade moadel=%d",ch-48);
ba
else if(ch>='a'&&ch<='z')
ab
printf("Harfe Bozorg=%c",ch-32);
6-
else
.6
printf("Khode character=%c",ch);
.w
return 0;
.w
}
w
www.66-abbas.blogfa.com
7
R^N
#include<stdio.h>
int main()
m
o.
{
c.
a.
long int i,r,n;
long int s=1; gf
lo
.b
scanf("%li%li",&r,&n);
s
ba
for(i=1;i<=n;i++)
ab
s=s*r;
6-
.6
printf("%li",s);
.w
return 0;
.w
}
w
www.66-abbas.blogfa.com
8
ﻓﺎﮐﺘﻮرﯾﻞ
#include<stdio.h>
int main()
m
o.
{
c.
a.
int i,n,s=1;
scanf("%d",&n); gf
lo
.b
for(i=1;i<=n;i++)
s
ba
s=s*i;
ab
printf("%d",s);
6-
.6
return 0;
.w
}
.w
w
www.66-abbas.blogfa.com
9
:ﺑﺮﻧﺎﻣﮫ ای ﺑﺮای ﻣﺤﺎﺳﺒﮫ ﺳﺮی
#include<stdio.h>
#include<conio.h>
void main(){
m
int i,n,j;
o.
float s;
c.
float sum=0;
a.
clrscr();
gf
scanf("%d",&n);
for(i=1;i<=n;i++)
lo
{
.b
s=1;
s
ba
for(j=1;j<=i;j++)
s=s*j;
ab
sum=sum+1/s;
}
6-
printf("%f",sum);
.6
getchar();
.w
getchar();
.w
}
w
10
www.66-abbas.blogfa.com
10
:ﺑﺮﻧﺎﻣﮫ ای ﮐﮫ ﮐﺎراﮐﺘﺮ ﺳﺘﺎره را ﺑﮫ ﺷﮑﻞ زﯾﺮ ﭼﺎپ ﻣﯿﮑﻨﺪ
*
**
***
****
m
*****
o.
#include<stdio.h>
c.
#include<conio.h>
a.
#include<dos.h>
int main()
gf
lo
{
.b
int i,j,n;
s
ba
clrscr();
ab
scanf("%d",&n);
for(i=1;i<=n;i++)
6-
{
.6
for(j=1;j<=i;j++)
.w
printf("*");
.w
printf("\n");
w
}
getch();
return 0;
} 11
www.66-abbas.blogfa.com
11
ﻣﻌﮑﻮس ﮐﺮدن ﻋﺪد وارد ﺷﺪه
#include<stdio.h>
#include<conio.h>
m
int main()
o.
{
c.
clrscr();
a.
long int num,digit=0;
gf
scanf("%ld",&num); lo
Do
.b
{
s
digit=num%10;
ba
printf("%ld",digit);
ab
num/=10;
6-
}
.6
while(num!=0);
.w
getch();
.w
return 0;
}
w
12
www.66-abbas.blogfa.com
12
ﺟﻤﻊ دو ﻋﺪد ﺻﺤﯿﺢ ﺑﺎ اﺳﺘﻔﺎده از ﻓﺮاﺧﻮاﻧﯽ ﺗﺎﺑﻊ
#include<stdio.h>
#include<conio.h>
int sum(int x,int y); //Olgooye tabe//
m
int main()
o.
{
c.
int x,y,z;
a.
scanf("%d%d",&x,&y);
gf
z=sum(x,y); //Farakhanie tabe//
printf("%d",z);
lo
getch();
.b
return 0;
s
ba
}
ab
//Tarife tabe//
int sum(int a,int b){
6-
int c;
.6
c=a+b;
.w
return c;
}
.w
w
13
www.66-abbas.blogfa.com
13
ﺷﻤﺎرش ﺗﻌﺪاد ﮐﺎراﮐﺘﺮ ﻓﺎﺻﻠﮫ در رﺷﺘﮫ ی ورودی
#include<stdio.h>
#include<conio.h>
m
int main()
o.
{
c.
clrscr();
a.
int i=0;
gf
char ch; lo
ch=getchar();
.b
while(ch!='\n')
s
{
ba
if(ch==' ')
ab
i++;
6-
ch=getchar();
.6
}
.w
printf("%d",i);
.w
return 0;
}
w
14
www.66-abbas.blogfa.com
14
:ﺑﺮﻧﺎﻣﮫ ای ﺑﺮای ﻣﺤﺎﺳﺒﮫ ی ﺳﺮی
m
int i,n,j,x,t=1;
o.
float s;
c.
float sum=0;
a.
clrscr();
gf
scanf("%d%d",&x,&n);
for(i=1;i<=n;i++)
lo
{
.b
s=1;
s
ba
for(j=1;j<=i;j++)
s=s*j;
ab
t=t*x;
sum=sum+t/s;
6-
.6
}
.w
printf("%f",sum);
.w
getchar();
getchar();
w
15
www.66-abbas.blogfa.com
15
ﺷﻤﺎرش ﺗﻌﺪاد ﮐﻠﻤﺎت رﺷﺘﮫ ی ورودی
# include <conio.h>
# include <stdio.h>
main(){
m
int num=0;
o.
clrscr();
c.
char ch;
a.
char chn;
gf
chn=' ';
ch=getche();
lo
while (ch!='\r')
.b
{
s
ba
chn=ch;
ch=getche();
6-
}
.6
num++;
.w
if (ch!='\r')
ch=getche();
w
chn=ch;
}
printf("\n%d",num);
getch();
} 16
www.66-abbas.blogfa.com
16
ﺷﻤﺎرش ﺗﻌﺪاد ﮐﻠﻤﺎت ﺳﮫ ﺣﺮﻓﯽ
#include<stdio.h
#include<stdio.h> >
#include<conio.h
#include<conio.h> >
int main(){
clrscr();
clrscr();
int count=0,num=0;
char chn='
chn=' ',chp
',chp='
=' ';
char ch;
ch;
ch=
ch=getche();
getche();
m
while(ch!='
while(ch!='\\r')
{
o.
while(ch=='
while(ch==' ')
ch=
ch=getche();
getche();
c.
while(ch>='0'&&ch<='9'){
while(ch>='0'&&ch<='9'){
count=0;
a.
ch=
ch=getche();
getche();
chn=
chn ch;
= ch;
gf
}
while((chn>='
while((chn>='A'&&chn
A'&&chn<='
<='Z')||(chn
Z')||(chn>='
>='a'&&chn
loa'&&chn<='z')){
<='z')){
count=0;
ch=
ch=getche();
getche();
.b
chn=
chn ch;
= ch;
}
s
while((ch>='
while((ch>='A'&&ch
A'&&ch<='
<='Z')||(ch
Z')||(ch>='
>='a'&&ch
a'&&ch<='z')){
<='z')){
ba
count++;
ch=
ch=getche();
getche();
ab
chp=
chp=ch;ch;
}
if(chp>='0'&&chp<='9')
if(chp>='0'&&chp<='9')
6-
count=0;
if(count==3)
if(count==3)
.6
num++;
else
.w
num=num
num=num;;
count=0;
.w
if(ch=='
if(ch=='\\r')
ch='
ch='\\r';
else
w
ch='
ch=' ';
}
printf("\
printf("\n%d",num);
n%d",num);
getche();
getche();
return 0;
}
17
www.66-abbas.blogfa.com
17 ﺷﮑﻞ زﯾﺮ،ﺑﺮﻧﺎﻣﮫ ای ﮐﮫ ﺑﺎ درﯾﺎﻓﺖ ﯾﮏ ﻋﺪد
:را ﭼﺎپ ﮐﻨﺪ
m
#include<stdio.h
#include<stdio.h> >
o.
#include<conio.h
#include<conio.h> >
int main()
c.
{
clrscr();
clrscr();
a.
int j,i,n,y,num=0;
j,i,n,y,num=0;
scanf("%d",&n);
scanf("%d",&n);
gf
for(j=1;j<=
for(j=1;j<=n;j n;j++)
++)
{ lo
y=j;
if(num==2&&j<=n)
if(num==2&&j<=n)
.b
{
num=0;
s
for(i=1;i<=
for(i=1;i<=y;iy;i++)
++)
ba
{
printf("+");
printf("+");
}
ab
printf("\
printf("\n");
if(num==0&&y<n)
if(num==0&&y<n)
6-
{
y++;
.6
for(i=1;i<=
for(i=1;i<=y;i
y;i++){
++){
printf("+");
printf("+");
.w
}
}
printf("\
printf("\n");
.w
y++;
j=y;
w
}
if(num==0&&y<=
if(num==0&&y<=n||num n||num!=0)
!=0) {
for(i=1;i<=
for(i=1;i<=y;i y;i++){
++){
printf("*");
printf ("*");
}
printf("\
printf("\n");
num++;
}
} 18
getch();
getch();
return 0;
}
www.66-abbas.blogfa.com
18
ﺑﺮﻧﺎﻣﮫ ای ﮐﮫ ﮐﺎراﮐﺘﺮ ﺳﺘﺎره را ﺑﮫ ﺷﮑﻞ زﯾﺮ ﭼﺎپ
:ﻣﯿﮑﻨﺪ
*
**
***
m
****
o.
*****
c.
#include<stdio.h>
a.
#include<conio.h>
gf
int main() lo
{
.b
clrscr();
s
int i,j,n,p=0,y;
ba
scanf("%d",&n);
ab
y=n-1;
for(i=1;i<=n;i++){
6-
while(p!=y){
.6
p=p+1;
.w
printf(" ");}
y=p-1;
.w
p=0;
w
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch(); 19
return 0;
}
www.66-abbas.blogfa.com
19
#include<stdio.h
#include<stdio.h> >
#include<conio.h
#include<conio.h> >
int main()
m
{
clrscr();
clrscr();
o.
int i,j,k,x,y,z,num=0,count=0;
i,j,k,x,y,z,num=0,count=0;
for(i=1;i<=9;i++){
for(i=1;i<=9;i++){
c.
x=i;
a.
for(j=0;j<=9;j++){
for(j=0;j<=9;j++){
y=j;
gf
for(k=0;k<=9;k++){
for(k=0;k<=9;k++){
z=k; lo
if(x!=
if(x!=y&&y
y&&y!=
!=z&&x
z&&x!=z){
!=z){
.b
printf("%d%d%d ",x,y,z
",x,y,z);
);
num++;
s
if(num%15==0)
ba
printf("\
printf("\n");
}
ab
else
printf("");
printf("");
}}}
6-
printf("\
printf("\n***num=%d***",num);
.6
count=900-
count=900-num;
printf("\
printf("\n***count=%d***",count);
.w
getch();
getch();
return 0;
.w
}
w
20
www.66-abbas.blogfa.com
20
:ﻓﺮاﺧﻮاﻧﯽ دو ﺗﺎﺑﻊ ﻓﺎﮐﺘﻮرﯾﻞ وﺗﻮان ﺑﺮای ﻣﺤﺎﺳﺒﮫ ی ﺳﺮی زﯾﺮ
m
float fact(long n);
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
o.
int main()
c.
{
a.
clrscr();
clrscr();
long n,x,i;
n,x,i;
gf
float s=0;
scanf("%ld%ld",&x,&n);
scanf("%ld%ld",&x,&n);
lo
for(i=1;i<=
for(i=1;i<=n;in;i++)
++)
.b
s+=(pow(x,i)/fact(i
s+=(pow(x,i)/fact(i));));
s
printf("%f",s);
printf("%f",s);
ba
return 0;
}
ab
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
float fact(long n){
6-
int i;
float s=1;
.6
for(i=1;i<=
for(i=1;i<=n;in;i++)
++)
.w
s=s*i;
return s;
.w
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
w
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
www.66-abbas.blogfa.com
21
ﻓﺮاﺧﻮاﻧﯽ ﺗﺎﺑﻊ ﺑﺎزﮔﺸﺘﯽ ﻓﯿﺒﻮﻧﺎﭼﯽ
#include<stdio.h>
#include<conio.h>
long fibonachi(long);
m
int main()
o.
{
c.
clrscr();
a.
long result,number;
gf
printf("Enter an integer;");
scanf("%ld",&number);
lo
result=fibonachi(number);
.b
printf("fibonachi(%ld)=%ld\n",number,result);
s
ba
getch();
return 0;
ab
}
////////////////////////////////////////////////////
6-
if(n==0||n==1)
.w
return n;
.w
else
return (fibonachi(n-1)+fibonachi(n-2));
w
22
www.66-abbas.blogfa.com
22
ﮐﮫ ﺑﺎ ﺻﻔﺮ ﭘﺮ ﻣﯽ ﺷﻮد10 آراﯾﮫ ای ﺑﺎ اﻧﺪازه ی
#include<stdio.h>
#include<conio.h>
m
int main()
o.
{
c.
clrscr();
a.
int a[10],i;
gf
for(i=0;i<10;i++)
lo
.b
a[i]=0;
s
for(i=0;i<10;i++)
ba
{
ab
printf("%d:",i);
6-
printf("[%d]\n",a[i]);
.6
}
.w
getch();
.w
return 0;
w
23
www.66-abbas.blogfa.com
23
#include<stdio.h
#include<stdio.h> >
m
#include<conio.h
#include<conio.h> >
int search(int b[],int len,int x);
o.
int main()
c.
{
int a[10],x,i,r;
a.
clrscr();
clrscr();
printf("Enter your number for search:\
search:\n");
gf
scanf("%d",&x);
scanf("%d",&x);
for(i=0;i<10;i++)
for(i=0;i<10;i++)
lo
{
.b
printf("Enter a[%d]:
a[%d]: ",i);
scanf("%d",&a[i]);
scanf("%d",&a[i]);
s
}
ba
r=search(a,10,x);
if(r!=
if(r!=--1)
ab
printf("found");
printf("found");
else
6-
printf("not found");
getch();return 0;
.6
}
////////////////////////////////////////////////////////////
.w
for(i=0;i<
for(i=0;i<len;i
len;i++)
++)
if(b[i]==x)
if(b[i]==x)
w
{
printf("The number is in:%d \n",i);
n",i);
return i;
}
return -1;
}
24
www.66-abbas.blogfa.com
24
ﻣﺮﺗﺐ ﺳﺎزی ﺣﺒﺎﺑﯽ
#include<stdio.h
#include<stdio.h> >
#include<conio.h
#include<conio.h> >
#define len 10
void bubblesort(int a[]);
void print(int a[]);
m
////////////////////////////////////////////////////////
o.
int main()
{
c.
clrscr();
clrscr();
int a[len],i;
a[len],i;
a.
for(i=0;i<
for(i=0;i<len;i
len;i++){
++){
gf
printf("Please Enter The %dth
%dth Number: ",i+1);
scanf("%d",&a[i]);
scanf("%d",&a[i]); lo
}
.b
bubblesort(a);
bubblesort(a);
print(a);
print(a);
s
getch();
getch();
ba
return 0;
}
ab
/////////////////////////////////////////////////////////
void bubblesort(int a[])
{
6-
int i,j,item;
i,j,item;
.6
for(i=len
for(i=len--1;i>0;i--
1;i>0;i--))
for(j=0;j<
for(j=0;j<i;j i;j++)
++)
.w
if(a[j]>a[j+1])
if(a[j]>a[j+1])
{
.w
item=a[j
item=a[j]; ];
a[j]=a[j+1];
a[j]=a[j+1];
w
a[j+1]=item;
}
}
//////////////////////////////////////////////////////////
void print(int a[]){
int i;
for(i=0;i<
for(i=0;i<len;i
len;i++)
++)
25
printf("%d\
printf("%d\n",a[i]);
n",a[i]);
}
www.66-abbas.blogfa.com
25 ﻣﺮﺗﺐ ﺳﺎزی ﺣﺒﺎﺑﯽ و ﺟﺴﺘﺠﻮی دودوﯾﯽ در ﯾﮏ آراﯾﮫ
#include <stdio.h
<stdio.h> >
#include <conio.h
<conio.h> >
#include <iostream.h
<iostream.h> >
void bubblesort(int a[],int len);len);
int binarysearch(int st[],int len,int x);
void print(int st[],int len);
len);
int main(){
clrscr();
clrscr();
int st[10],i,x,me;
printf("Enter your number for search=");
scanf("%d",&x);
scanf("%d",&x);
m
///////////////////////////////////////////////////por
///////////////////////////////////////////////////por kardane array
for(i=0;i<=10;i++)
for(i=0;i<=10;i++)
o.
{
printf("Enter The %dth %dth number=",i+1);
c.
scanf("%d",&
scanf("%d",& st[i]);
st[i]);
}
///////////////////////////////////////////////////farakhanie
///////////////////////////////////////////////////farakhanie tabeha
a.
bubblesort(st,10);
print(st,10);
gf
me=binarysearch(st,10,x);
//////////////////////////////////////////////////Print result
printf(“
printf(“The Place of Number is=%d",me+1);
getch();
getch();
lo
.b
return 0;
}
///////////////////////////////////////////////////
s
int mid,low=0,high=len
mid,low=0,high=len--1;
while (low<=high)
{
ab
mid=(low+high)/2;
if (x<st[mid
(x<st[mid]) ])
high=mid-
high=mid-1;
6-
else if(x>
if(x>st[mid])
st[mid])
low=mid+1;
else
.6
return(mid);
return(mid);
}
.w
return -1;
}
////////////////////////////////////////////////////2
.w
for(i=len
for(i=len--1;i>0;i--
1;i>0;i--))
for(j=0;j<
for(j=0;j<i;j i;j++)
++)
if(st[j]>st[j+1]){
if(st[j]>st[j+1]){
item=st[j
item=st[j]; ];
st[j]=st[j+1];
st[j]=st[j+1];
st[j+1]=item;
}
}
////////////////////////////////////////////////////3
void print(int st[],int len){
len){ 26
int i;
for(i=0;i<
for(i=0;i<len;i
len;i++)
++)
printf("%d\
printf("%d\t",st[i]);
t",st[i]);
}
www.66-abbas.blogfa.com
26
ﻧﺤﻮه ی ارﺳﺎل ﯾﮏ ﻣﺎﺗﺮﯾﺲ ﺑﮫ ﯾﮏ ﺗﺎﺑﻊ
#include<stdio.h
#include<stdio.h> >
#include<conio.h
#include<conio.h> >
void printArray(int a[][3]);
int main()
m
{
/*3 matrise 2*3 ke ba halate mokhtalef por shodeand */
o.
int array1[2][3]={{1,2,3},{4,5,6}},
c.
array2[2][3]={1,2,3,4,5,6},
array3[2][3]={{1,2},{4}};
a.
printf("values in Array1 by row are;\
are;\n");
gf
////////////////////////////////////////////////////////////
/*Farakhani
/*Farakhani e tabe*///
tabe*///fagat
fagat esme matris ra miferestim//
lo miferestim//
////////////////////////////////////////////////////////////
.b
printArray(array1);
s
/*Farakhani
/*Farakhani e tabe*/
tabe*/
printArray(array2);
ab
printArray(array3);
return 0;
.6
}
.w
////////////////////////////////////////////////////////////
void printArray(int a[][3]){
.w
int i,j;
i,j;
for(i=0;i<2;i++)
for(i=0;i<2;i++)
w
{
for(j=0;j<3;j++)
for(j=0;j<3;j++)
printf("%d",a[i][j]);
printf("%d",a[i][j]);
printf("\
printf("\n");
}
} 27
www.66-abbas.blogfa.com
27
ﺗﺒﺪﯾﻞ ﺣﺮوف ﮐﻮﭼﮏ ﯾﮏ رﺷﺘﮫ ﺑﮫ ﺣﺮوف ﺑﺰرگ
#include<conio.h>
#include<stdio.h>
void upper(char string[]);
m
int main()
o.
{
c.
clrscr();
a.
printf("Enter Your text:\n");
gf
char string[30];
gets(string);
lo
upper(string);
.b
puts(string);
s
ba
return 0;
}
ab
///////////////////////////////////////////////////
void upper(char string[])
6-
{
.6
int i;
.w
for(i=0;string[i]!='\0';i++)
.w
if(string[i]>='a'&&string[i]<='z')
string[i]=string[i]-32;
w
28
www.66-abbas.blogfa.com
28
ﺟﺴﺘﺠﻮی ﯾﮏ رﺷﺘﮫ در رﺷﺘﮫ ی دﯾﮕﺮ
#include<conio.h
#include<conio.h> >
#include<stdio.h
#include<stdio.h> >
int match(char string1[],char string2[]);
int main(){
clrscr();
clrscr();
m
int z;
o.
char string1[30];
printf("Enter your string:");
c.
gets(string1);
char string2[15];
a.
printf("Enter your string for search:");
gf
gets(string2);
z=match(string1,string2); lo
printf("Result:%d",z);
printf("Result:%d",z);
.b
getch();
getch();
return 0;
s
}
ba
//////////////////////////////////////////////////////////
int match(char string1[],char string2[]){
ab
int i=0,j=0;
while(string1[i]&&string2[j])
if(string1[i]==string2[j])
6-
{
.6
i++;
j++;
.w
}
else{
.w
i++;
j=0;
w
}
if(string2[j]=='\
if(string2[j]=='\0')
return 1;
else
return -1;
}
29
www.66-abbas.blogfa.com
29
آدرس اﺷﺎره ﮔﺮ و ﻣﺤﺘﻮای آدرس،ﻧﺤﻮه ی ﻧﻤﺎﯾﺶ آدرس
#include<stdio.h
#include<stdio.h> >
#include<conio.h
#include<conio.h> >
int main()
{
clrscr();
clrscr();
m
int a;
int *aptr;
aptr;
o.
a=7;
c.
aptr=&a;
aptr=&a;
printf("The adress of a is : %p \n The value of aptr is :
a.
%p",&a,&aptr);
p",&a,&aptr);
gf
/////////////////////////////////////////////////////////////
printf("\
printf("\n\nThe value of a is : %d \n The value of *aptr
lo *aptr is :
%d",a,*
d",a,*aptr
aptr);
);
.b
/////////////////////////////////////////////////////////////
printf("\
printf("\n\nShowing that (*) and (&) are inverses of each other
s
\n &*aptr
&*aptr = %p""
%p""\\n *&aptr
*&aptr = %p%p\n",&*aptr
",&*aptr,*&
,*&aptr
aptr);
);
ba
/////////////////////////////////////////////////////////////
getch();
getch();
ab
return 0;
}
6-
.6
.w
.w
w
30
www.66-abbas.blogfa.com
30
ﭘﺎک ﮐﺮدن ﺟﺎھﺎی ﺧﺎﻟﯽ در رﺷﺘﮫ ورودی
#include<stdio.h
#include<stdio.h> >
#include<conio.h
#include<conio.h> >
void deleter(char st[]);
st[]);
int main()
{
char st[40];
gets(st);
gets(st);
m
deleter(st);
deleter(st);
printf("%s",st);
printf("%s",st);
o.
return 0;
}
c.
/////////////////////////////////////////
void deleter(char st[])
st[])
a.
{
int i=0,j=0;
gf
while(st[j]!='
while(st[j]!='\\0')
{ lo
while(st[j]!='
while(st[j]!=' ')
.b
{
i++;
s
j++;
}
ba
while(st[j]=='
while(st[j]==' '&&st[j
'&&st[j]!='
]!='\\0')
j++;
ab
st[i]=
st[i]=st[j
st[j];
];
st[j]='
st[j]=' ';
j=i;
6-
}
}
.6
/////////////////////////////////////////
.w
.w
w
31
www.66-abbas.blogfa.com
31
Palindrome
#include<stdio.h
#include<stdio.h> >
#include<conio.h
#include<conio.h> >
#include<string.h
#include<string.h> >
int palindrome(char st[]);st[]);
int main()
{
int result;
m
char st[20];
gets(st);
gets(st);
o.
result=palindrome(st
result=palindrome(st); );
printf("%d",result);
printf("%d",result);
c.
return 0;
}
a.
/////////////////////////////////////////////////////
int palindrome(char st[]) st[])
gf
{
int len;
len; lo
len=
len=strlen(st);
strlen(st);
.b
int i=0,j=len-
i=0,j=len-1;
if(len%2==0)
s
{
while(st[i]==
while(st[i]==st[j]&&j
st[j]&&j>0&&i<
>0&&i<len
len))
ba
{
i++;
ab
j--;
--;
}
if(i<
if(i<j&&st[i]!=
j&&st[i]!=st[j
st[j])
])
6-
return -1;
else
.6
return 1;
}
.w
if((len%2)!=0)
{
.w
while(st[i]==
while(st[i]==st[j]&&j
st[j]&&j>0&&i<
>0&&i<len
len))
{
i++;
w
j--;
--;
}
if(st[i]!=
if(st[i]!=st[j
st[j])
])
return -1;
else
return 1;
}
} 32
www.66-abbas.blogfa.com
32
اﺿﺎﻓﮫ ﮐﺮدن ﯾﮏ رﺷﺘﮫ ﺑﮫ ﯾﮏ ﻧﻘﻄﮫ ی ﺧﺎص از رﺷﺘﮫ دﯾﮕﺮ
#include<stdio.h
#include<stdio.h> >
#include<conio.h
#include<conio.h> >
#include<string.h
#include<string.h> >
void replace(char st1[],char st2[],int k);
int main()
{
m
int k;
char st1[100];
o.
char st2[100];
c.
printf("Enter first string:\
string:\n");
gets(st1);
a.
printf("Enter second string to attaching:\
attaching:\n");
gets(st2);
gf
printf("Enter the char number:\
number:\n");
scanf("%d",&k);
scanf("%d",&k);
lo
replace(st1,st2,k);
.b
printf("%s",st1);
return 0;
s
}
ba
////////////////////////////////////////////////////////////
void replace(char st1[],char st2[],int k)
ab
{
int len1,len2,i,j;
6-
len1=strlen(st1);
len2=strlen(st2);
.6
st1[len1+len2]='\
st1[len1+len2]='\0';
while(len1>k)
.w
{
st1[(len1-
st1[(len1-1)+len2]=st1[len1-
1)+len2]=st1[len1-1];
.w
st1[len1-
st1[len1-1]=' ';
len1-
len1-=1;
w
}
for(i=
for(i=k,j=0;st2[j]!='
k,j=0;st2[j]!='\\0';i++,j++)
st1[i]=st2[j];
}
33
www.66-abbas.blogfa.com
33
ﻣﺜﺎﻟﯽ از ﻣﻘﺎﯾﺴﮫ ی دو رﺷﺘﮫ و اﯾﻨﮑﮫ رﺷﺘﮫ ھﺎ آراﯾﮫ ای از ﮐﺎراﮐﺘﺮ
.ھﺎ ھﺴﺘﻨﺪ و ﻣﺎﺗﺮﯾﺴﯽ از ﮐﺎراﮐﺘﺮھﺎ ﺑﺮاﺑﺮ آراﯾﮫ ای از رﺷﺘﮫ ھﺎﺳﺖ
#include<stdio.h>
#include<conio.h>
m
#include<string.h>
o.
int main()
c.
{
a.
int i,n=0;
char x[10][11];
gf
lo
do{
.b
printf("string %d :",n+1);
s
ba
scanf("%s",x[n]);
}while(strcmpi(x[n++],"end"));
ab
n--;
6-
for(i=0;i<n;i++)
.6
}
.w
w
34
www.66-abbas.blogfa.com
34
آن ﮐﺎراﮐﺘﺮ را در، ﺑﺮﻧﺎﻣﮫ ای ﮐﮫ ﺑﺎ ﮔﺮﻓﺘﻦ ﯾﮏ رﺷﺘﮫ و ﯾﮏ ﮐﺎراﮐﺘﺮ
ﻣﺜﺎﻟﯽ از-.رﺷﺘﮫ ﭘﯿﺪا ﮐﺮده و ھﺮ ﭼﮫ ﺑﻌﺪ از آن اﺳﺖ را ﭼﺎپ ﻣﯽ ﮐﻨﺪ
.اﺷﺎره ﮔﺮ ھﺎ
#include<stdio.h>
#include<conio.h>
m
char *match(char ch,char *s);
int main()
o.
{
c.
char s[80],*p,ch;
a.
printf("Enter your string:\n");
gf
gets(s); lo
printf("Enter the character:\n");
.b
ch=getche();
s
p=match(ch,s);
ba
if(*p)
printf("\nString is:\n%s",p);
ab
else
6-
printf("Error");
return 0;
.6
}
.w
////////////////////////////////////////
.w
int i=0;
while(ch!=s[i]&&s[i]!='\0')
i++;
return (&s[i]);
} 35
www.66-abbas.blogfa.com
35
Structure :ﻣﺜﺎﻟﯽ از ﺗﻌﺮﯾﻒ
#include<stdio.h>
#include<string.h>
struct student{
int id;
m
char name[20];
o.
};
c.
a.
int main()
gf
{
struct student a={2325,"ali"};
lo
.b
strcpy(a.name,"ali");
s
printf("_________\n");
ba
printf("%s|%d|\n",a.name,a.id);
ab
printf("---------");
return 0;
6-
}
.6
.w
.w
w
36
www.66-abbas.blogfa.com
36
Structure دﻓﺘﺮﭼﮫ ﺗﻠﻔﻦ ﺑﺎ اﺳﺘﻔﺎده از
m
#include<conio.h
#include<conio.h> >
o.
#include<string.h
#include<string.h> >
#define size 10 // zarfiate daftarche
c.
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
typedef struct{
struct{ // "struct
"struct define"
a.
long no; //
gf
char name[20]; //
}phone; // lo
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
.b
int main()
{
s
int i;
ba
phone a[size];
a[size];
for(i=0;i<
for(i=0;i<size;i
size;i++)
++)
ab
{
printf("%dth contact: ",i+1);
scanf("%s",a[i].name);
scanf("%s",a[i].name);
6-
scanf("%ld",&a[i].no);
scanf("%ld",&a[i].no);
.6
}
char st[20];
.w
for(i=0;i<
for(i=0;i<size;i
size;i++)
++)
if(!strcmpi(a[i].name,st))
if(!strcmpi(a[i].name,st))
w
printf("%s:%ld",a[i].name,a[i].no);
printf("%s:%ld",a[i].name,a[i].no);
getche();
getche();
return 0;
}
37
www.66-abbas.blogfa.com
37
.ﺑﺮﻧﺎﻣﮫ ای ﮐﮫ ﮐﺪ اﺳﮑﯽ ﮐﺎراﮐﺘﺮ ورودی را ﺑﺮ ﻣﯿﮕﺮداﻧﺪ
#include<stdio.h>
#include<conio.h>
int main ()
m
{
o.
char ch;
c.
a.
printf("Enter Your Character:\n");
gf
ch=getchar(); lo
.b
printf("\n%d",ch);
s
ba
ab
getchar();
6-
return 0; }
.6
.w
.w
w
38
www.66-abbas.blogfa.com
38 . اﻣﯿﻦ ﻋﺪد ﻓﯿﺒﻮﻧﺎﭼﯽ را ﺑﺮﻣﯽ ﮔﺮداﻧﺪn ﺑﺮﻧﺎﻣﮫ ای ﮐﮫ
#include <stdio.h
<stdio.h>>
long fibonacci(
fibonacci( long n ); /* function prototype */
/* function main begins program execution */
int main( void )
{
long result; /* fibonacci value */
m
long number; /* number input by user */
o.
/* obtain integer from user */
c.
printf(
printf( "Enter an integer: " );
a.
scanf(
scanf( "%ld", &number );
gf
/* calculate fibonacci value for number input by user */
lo
result = fibonacci(
fibonacci( number );
.b
/* display result */
s
printf(
printf( "Fibonacci( %ld ) = %ld\
%ld\n", number, result );
ba
} /* end main */
6-
.6
{
.w
/* base case */
if ( n == 0 || n == 1 ) {
w
return n;
} /* end if */
else { /* recursive step */
return fibonacci(
fibonacci( n - 1 ) + fibonacci(
fibonacci( n - 2 );
} /* end else */
39
} /* end function fibonacci */
www.66-abbas.blogfa.com
39 ﺑﺮای ﺣﻞ ﻣﺴﺎﻟﮫ ی ﺑﺮج ھﺎﻧﻮی1 ﺑﺮﻧﺎﻣﮫ ی ﺷﻤﺎره
() ﻧﻮﺷﺘﮫ ﺷﺪه ﺗﻮﺳﻂ ﻣﮭﺮداد ﻧﯿﺮﭘﻮر
(C++)
# include <iostream.h>
# include <conio.h>
/*
*****************************
******
Hanoi Tower:
Created By Mehrdad Nayyerpoor And Hosein Seddigy
m
With Special Thaks To Professor Babak Anary
//********************Function of Hanoi Tower For Odd
o.
Numbers***********
hanoi_o(int n){
c.
int k,i,j,x,y,l,p=0,count=0,m[21][3];
for (i=0;i<21;i++)
a.
for (j=0;j<3;j++)
m[i][j]=0;
gf
m[20][0]=n-1;
j=2; lo
for (i=n-1;i>=0;i--)
.b
m[i][0]=n-i;
i=0;
s
gotoxy(0,0);
cout<<"Press Any Key To Continue...";
ba
gotoxy(10+(n/2)-1,22);
cout<<"A";
ab
gotoxy(35+(n/2)-1,22);
cout<<"B";
gotoxy(58+(n/2),22);
6-
cout<<"C";
for (x=10;x<=60;x=x+25){
.6
for (y=20;y>=20-n;y--){
gotoxy(x,y);
.w
for
(l=1;l<=m[20-y][p];l++)
.w
cout<<"*";
}
p++;
w
}
while (m[n-1][2]!=1){
p=0;
if (m[m[20][j]][j]==0){
m[0][j]=m[m[20][i]][i];
k=m[m[20][i]][i];
m[m[20][i]][i]=0;
40
www.66-abbas.blogfa.com
ﺑﺮای ﺣﻞ ﻣﺴﺎﻟﮫ ی ﺑﺮج ھﺎﻧﻮی1 ﺑﺮﻧﺎﻣﮫ ی ﺷﻤﺎره
m[20][i]--;
getch();
count++;
clrscr();
gotoxy(10+(n/2)-1,22);
cout<<"A";
gotoxy(35+(n/2)-1,22);
cout<<"B";
m
gotoxy(58+(n/2),22);
cout<<"C";
o.
gotoxy(0,0);
cout<<"The "<<count<<"th Move!"<<endl;
cout<<"Press Any Key To Continue...";
c.
for (x=10;x<=60;x=x+25){
for (y=20;y>=20-n;y--){
a.
gotoxy(x,y);
for
gf
(l=1;l<=m[20-y][p];l++)
cout<<"*";
} lo
p++;
}
.b
}
else if (m[m[20][i]][i]<m[m[20][j]][j] &&
s
m[m[20][i]][i]!=0){
m[20][j]++;
ba
m[m[20][j]][j]=m[m[20][i]][i];
k=m[m[20][i]][i];
m[m[20][i]][i]=0;
ab
m[20][i]--;
getch();
clrscr();
6-
gotoxy(10+(n/2)-1,22);
cout<<"A";
gotoxy(35+(n/2)-1,22);
.6
cout<<"B";
gotoxy(58+(n/2),22);
.w
cout<<"C";
count++;
gotoxy(0,0);
.w
for (y=20;y>=20-n;y--){
gotoxy(x,y);
for
(l=1;l<=m[20-y][p];l++)
cout<<"*";
41
www.66-abbas.blogfa.com
ﺑﺮای ﺣﻞ ﻣﺴﺎﻟﮫ ی ﺑﺮج ھﺎﻧﻮی1 ﺑﺮﻧﺎﻣﮫ ی ﺷﻤﺎره
}
p++;
}
}
else if (m[m[20][i]][i]>m[m[20][j]][j]){
j--;
}
if (m[20][i]==0 && m[m[20][i]][i]==0)
m
i--;
if (i<0)
o.
i=2;
if (j<0)
c.
j=2;
a.
if (j==i){
i--;
gf
j=i-1;
}
if (i<0 && j<0){
i=2;
lo
.b
j=1;
}
s
if (m[20][i]<0)
ba
m[20][i]=0;
if (m[20][j]<0)
m[20][j]=0;
ab
if (m[m[20][i]][i]==k){
i--;
j--;
6-
}
if (i<0 && j<0){
.6
i=2;
j=1;
.w
}
if (j<0)
.w
j=2;
}
w
gotoxy(0,0);
cout<<"It Complated In "<<count<<" Moves!\n";
cout<<" ";
}
//*******************Function of Hanoi Tower For Even
Numbers*******************
hanoi_e(int n){
int k,i,j,x,y,l,p=0,count=0,m[21][3];
42
www.66-abbas.blogfa.com
ﺑﺮای ﺣﻞ ﻣﺴﺎﻟﮫ ی ﺑﺮج ھﺎﻧﻮی1 ﺑﺮﻧﺎﻣﮫ ی ﺷﻤﺎره
for (i=0;i<21;i++)
for (j=0;j<3;j++)
m[i][j]=0;
m[20][0]=n-1;
j=1;
for (i=n-1;i>=0;i--)
m[i][0]=n-i;
i=0;
m
gotoxy(0,0);
cout<<"Press Any Key To Continue...";
o.
gotoxy(10+(n/2)-1,22);
cout<<"A";
c.
gotoxy(35+(n/2)-1,22);
cout<<"B";
a.
gotoxy(58+(n/2),22);
cout<<"C";
gf
for (x=10;x<=60;x=x+25){
for (y=20;y>=20-n;y--){ lo
gotoxy(x,y);
for
.b
(l=1;l<=m[20-y][p];l++)
cout<<"*";
s
}
ba
p++;
}
ab
while (m[n-1][2]!=1){
p=0;
if (m[m[20][j]][j]==0){
6-
m[0][j]=m[m[20][i]][i];
k=m[m[20][i]][i];
.6
m[m[20][i]][i]=0;
m[20][i]--;
.w
getch();
count++;
.w
clrscr();
gotoxy(10+(n/2)-1,22);
cout<<"A";
w
gotoxy(35+(n/2)-1,22);
cout<<"B";
gotoxy(58+(n/2),22);
cout<<"C";
gotoxy(0,0);
cout<<"The "<<count<<"th Move!"<<endl;
cout<<"Press Any Key To Continue...";
for (x=10;x<=60;x=x+25){
43
for (y=20;y>=20-n;y--){
gotoxy(x,y);
for
www.66-abbas.blogfa.com
ﺑﺮای ﺣﻞ ﻣﺴﺎﻟﮫ ی ﺑﺮج ھﺎﻧﻮی1 ﺑﺮﻧﺎﻣﮫ ی ﺷﻤﺎره
(l=1;l<=m[20-y][p];l++)
cout<<"*";
}
p++;
}
}
else if (m[m[20][i]][i]<m[m[20][j]][j] &&
m[m[20][i]][i]!=0){
m
m[20][j]++;
m[m[20][j]][j]=m[m[20][i]][i];
o.
k=m[m[20][i]][i];
c.
m[m[20][i]][i]=0;
m[20][i]--;
a.
getch();
clrscr();
gf
gotoxy(10+(n/2)-1,22);
cout<<"A";
gotoxy(35+(n/2)-1,22);
cout<<"B";
lo
.b
gotoxy(58+(n/2),22);
cout<<"C";
s
count++;
ba
gotoxy(0,0);
cout<<"The "<<count<<"th Move!"<<endl;
cout<<"Press Any Key To Continue...";
ab
for (x=10;x<=60;x=x+25){
for (y=20;y>=20-n;y--){
6-
gotoxy(x,y);
for
(l=1;l<=m[20-y][p];l++)
.6
cout<<"*";
}
.w
p++;
}
.w
}
else if (m[m[20][i]][i]>m[m[20][j]][j]){
w
j++;
}
if (m[20][i]==0 && m[m[20][i]][i]==0)
i++;
if (i>2)
i=0;
if (j>2)
j=0;
if (j==i){ 44
i++;
j=i+1;
www.66-abbas.blogfa.com
ﺑﺮای ﺣﻞ ﻣﺴﺎﻟﮫ ی ﺑﺮج ھﺎﻧﻮی1 ﺑﺮﻧﺎﻣﮫ ی ﺷﻤﺎره
}
if (i>2 && j>2){
i=0;
j=1;
}
if (m[20][i]<0)
m
m[20][i]=0;
if (m[20][j]<0)
o.
m[20][j]=0;
c.
if (m[m[20][i]][i]==k){
i++;
a.
j++;
}
gf
if (i>2 && j>2){
i=0;
lo
j=1;
.b
}
if (j>2)
s
j=0;
ba
}
gotoxy(0,0);
ab
}
//************************Main Function of Hanoi
.6
Tower**************************
void main(){
.w
int n;
clrscr();
.w
clrscr();
if (n%2==0)
hanoi_e(n);
else
hanoi_o(n);
getch();
} 45
/////////////////////////////////////////////////////////////////////////
www.66-abbas.blogfa.com
40 ﺑﺮای ﺣﻞ ﻣﺴﺎﻟﮫ ی ﺑﺮج ھﺎﻧﻮی2 ﺑﺮﻧﺎﻣﮫ ی ﺷﻤﺎره
(C++)
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
typedef char* Peg;
void move(Peg A, Peg B);
void transfer(size_t N, Peg A, Peg B, Peg C);
m
void get_disk_num(int &iNum);
//////////////////////////////////////////////////////////
o.
///////////////////////////////////////
int main()
c.
{
while(1)
a.
{
cout << "\t\t\tHanoi Towers
gf
Puzzle Solver" << endl << endl;
cout << "Enter number of disc on lo
the first peg (enter -1 to quit): ";
.b
int iDiskNum, iStepNum;
get_disk_num(iDiskNum);
s
if(iDiskNum == -1)
{
ba
break;
}
else
6-
{
iStepNum = pow(2,
.6
iDiskNum) - 1;
cout << "the shortest
.w
solution can be reach in " << iStepNum << " steps" <<
endl;
.w
www.66-abbas.blogfa.com
ﺑﺮای ﺣﻞ ﻣﺴﺎﻟﮫ ی ﺑﺮج ھﺎﻧﻮی2 ﺑﺮﻧﺎﻣﮫ ی ﺷﻤﺎره
//////////////////////////////////////////////////////////
//////////////////////
void move(Peg A, Peg B)
{
cout << "move top most disc from " << A << " to " << B
<< endl;
}
m
//////////////////////////////////////////////////////////
//////////////////////
o.
///////// recursive algorithm for solving the hanoi
towers puzzle /////////
c.
//////////////////////////////////////////////////////////
//////////////////////
a.
void transfer(size_t N, Peg A, Peg B, Peg C)
{
gf
if(N > 0)
{ lo
// moves the first N - 1 disk from the
.b
first peg to the intermediate one "B"
transfer(N - 1, A, C, B);
s
move(A, C);
// moves the disk that are stacked on peg
ab
}
//////////////////////////////////////////////////////////
.6
//////////////////////
////////gets the number of disk to be stacked on the first
.w
peg ///////////////
//////////////////////////////////////////////////////////
.w
//////////////////////
void get_disk_num(int &iNum)
{
w
www.66-abbas.blogfa.com
m
o.
c.
a.
gf
lo
s .b
ba
ab
6-
.6
.w
.w
w
www.66-abbas.blogfa.com
48
www.66-abbas.blogfa.com