Você está na página 1de 29

PROGRAM:

\* x to the power of y*\

#include<stdio.h>
void main()
{
int x,y;
float power(int, int);

printf(" enter x and y values..");


scanf("%d%d",&x,&y);
printf("%d to the power of %d is..%f",x,y,power(x,y));
}
float power(int x, int y)
{
float p=1.0;
if(y>=0)
while(y--)
p*=x;
else
while(y++)
p/=x;
return(p);
}
OUTPUT:

enter x and y values..3 6


3 to the power of 6 is..729.000000
PROGRAM:

\* global variable *\

#include<stdio.h>
int a;
void main()
{
a=39;
int f1();
int f2();
int f3();
printf("a...%d\n",a);
printf("a...%d\n",f1());
printf("a...%d\n",f2());
printf("a...%d\n",f3());
}
f1()
{
a=a+10;
return(a);
}
f2()
{
a=a+20;
return(a);
}
f3()
{
a=a+30;
return(a);
}
OUTPUT:

a...39
a...49
a...69
a...99
PROGARM:

\* transfer structure to a function *\

#include<stdio.h>
typedef struct
{
char* name;
int no;
int age;
}record;
void main()
{
void fun(record *pt);
static record std={"LAK",15, 25};
printf("%s\t%d\t%d",std.name,std.no,std.age);
fun(&std);
printf("\n%s\t%d\t%d",std.name,std.no,std.age);
}
void fun(record *pt)
{
pt->name ="MUNI";
pt->no =16;
pt->age =26;
}
OUTPUT:

LAK 15 25
MUNI 16 26
PROGRAM:

\* transfer structure to array function *\

#include<stdio.h>
#define N 3
#define Null 0
typedef struct
{
int sno;
char* name;
float age;
} record;
void main()
{
static record std[N]={ {15,"muni", 28},
{16,"lak",27},
{17,"raja",31}
};
int stdno;
record *pt;
record *search( record table[], int stdno);
printf("student number locater");
printf("\n to exit the program enter 0 for student number");
printf("\n enter student number : ");
scanf("%d",&stdno);
while(stdno!=0)
{
pt = search(std,stdno);
if (pt!=NULL)
{
printf("\n number...%d",pt->sno);
printf("\n name...%s",pt->name);
printf("\nage...%f",pt->age);
}
else
printf("\n not found..try again");
printf("\nenter student number : ");
scanf("%d",&stdno);
}
}
record *search(record table[], int stdno)
{
int count;
for (count=0;count<N; ++count)
if(table[count].sno==stdno)
return(&table[count]);
return(NULL);
}
OUTPUT:

student number locater


to exit the program enter 0 for student number
enter student number : 15

number...15
name...muni
age...28.000000
enter student number : 0
PROGRAM:

\* swapping of two numbers *\

#include<stdio.h>
void interchange(int *a,int *b);
void main()
{
int i=5,j=10;
printf("\n i and j values before interchange :%d%d",i,j);
interchange(&i,&j);
printf("\n i and j value after %d%d",i,j);
}
void interchange(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
OUTPUT:

i and j values before interchange : 5 10


i and j value after 10 5
PROGRAM:

\* cube of a number using call by value *\

#include<stdio.h>
int cube(int x);
void main()
{
int n;
printf("\n enter the number");
scanf("%d",&n);
printf("\n");
printf("\n cube of %d is..%d",n,cube(n));
}
int cube(int x)
{
return(x*x*x);
}
OUTPUT:

enter the number 5


cube of 5 is…..125
PROGRAM:

\* factorial without recursion *\

#include<stdio.h>
void main()
{
int i,n,fact=1;
printf("\n enter the number..");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\n the factorial of %d...",fact);
}
OUTPUT:

enter the number..5


the factorial of ……120
PROGRAM:

\* factorial using recursion *\

#include<stdio.h>
main()
{
int a,num;
printf("\n enter the number..");
scanf("%d",&num);
a=recur(num);
printf("\n the factorial of the number %d is %d",num,a);
}
recur(int no)
{
int fact=1;
if(no==1)
return(1);
else
fact=no* recur(no-1);
return(fact);
}
OUTPUT:

enter the number..5


the factorial of the number 5 is 120
PROGRAM:

\* program using ftell fseek and rewind function *\

#include<stdio.h>
main()
{
char c;
long int x=0,m;
FILE *fptr=fopen("vrb.dat","r");
if(fptr!=NULL)
{
fseek(fptr,0L,0);
while(x=getc(fptr)!=EOF)
putchar(c);
fseek(fptr,0L,2);
x=ftell(fptr);
printf("\n the size of thefile is %d",x);
rewind(fptr);
x=ftell(fptr);
printf("\n the size of file is %d", x);
}
else
printf("\n the file does not exists");
fclose(fptr);
}
PROGRAM:

the size of the file is 12


the size of file is 0
PROGRAM:

\* allocated memory *\

#include<stdio.h>
#include<stdlib.h>
main()
{
char *p;
p=(char *)malloc(6);
strcpy(p,"MADRAS");
printf("\n memory contains:%s",p);
p=(char *)realloc(p,7);
strcpy(p,"CHENNAI");
printf("\n memory now contains:%s",p);
free(p);
}
OUTPUT:

memory contains : MADRAS


memory now contains : CHENNAI
PROGRAM:

\* reverse the character *\

#include<stdio.h>
main()
{
void reverse();
printf("\n enter line of text..... ln");
reverse();
}
void reverse()
{
char c;
if((c=getchar())!='\n')
reverse();
putchar(c);
}
OUTPUT:

enter line of text..... ln karthik

kihtrak
PROGRAM:

\* count the number of words using pointer *\

#include<stdio.h>
main()
{
char str[100],*ps;
int word=0,null=0;
printf("\n enter a string");
ps=str;
gets(ps);
while(*ps==32&&ps!=null)
ps++;
if(*ps!=null)
word++;
for(ps=str;*ps!=null;ps++)
{
if(*ps!=32&&*(ps-1)==32)word++;
}
printf("\n %d words in the given string",word);
}
OUTPUT:

enter a string: how are you

4 words in the given string


PROGRAM:

\* pointer to structure *\

#include<stdio.h>
main()
{
struct account
{
char name[20];
char place[25];
int acctno;
};
static struct account m4={"venkat","arcot",24201};
struct account *ptr;
ptr=&m4;
printf("%s\t%s\t%d\n",m4.name,m4.place,m4.acctno);
printf("%s\t%s\t%d\n",ptr->name,ptr->place,ptr->acctno);
}
OUTPUT:

venkat arcot 24201


venkat arcot 24201
PROGRAM:

\* sum of numbers using pointers *\

#include<stdio.h>
main()
{
int b,total=0;
int a[5];
int *c;
for(b=0;b<5;b++)
{
printf("\n enter the number %d",b+1);
scanf("%d",&a[b]);
}
c=a;
for(b=0;b<5;b++)
{
printf("\n %d",*c);
total=total+*c;
c=c++;
}
printf("\n total =%d",total);
}
OUTPUT:

enter the number 1


5

enter the number 2


5

enter the number 3


6

enter the number 4


8

enter the number 5


4

5
5
6
8
4
total =28

Você também pode gostar