Upload
gokul-g-factor-kumar
View
230
Download
0
Embed Size (px)
Citation preview
8/7/2019 fundamental of computing(FOC)
1/29
PROGRAM:
\* x to the power of y*\
#includevoid 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;elsewhile(y++)
p/=x;return(p);
}
8/7/2019 fundamental of computing(FOC)
2/29
OUTPUT:
enter x and y values..3 63 to the power of 6 is..729.000000
8/7/2019 fundamental of computing(FOC)
3/29
PROGRAM:
\* global variable *\
#includeint 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);
}
8/7/2019 fundamental of computing(FOC)
4/29
OUTPUT:
a...39a...49a...69a...99
8/7/2019 fundamental of computing(FOC)
5/29
PROGARM:
\* transfer structure to a function *\
#includetypedef 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;
}
8/7/2019 fundamental of computing(FOC)
6/29
OUTPUT:
LAK 15 25MUNI 16 26
8/7/2019 fundamental of computing(FOC)
7/29
PROGRAM:
\* transfer structure to array function *\
#include#define N 3#define Null 0typedef 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);
8/7/2019 fundamental of computing(FOC)
8/29
}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
8/7/2019 fundamental of computing(FOC)
9/29
OUTPUT:
student number locater to exit the program enter 0 for student number enter student number : 15
number...15name...muni
age...28.000000enter student number : 0
8/7/2019 fundamental of computing(FOC)
10/29
PROGRAM:
\* swapping of two numbers *\
#includevoid 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;
}
8/7/2019 fundamental of computing(FOC)
11/29
OUTPUT:
i and j values before interchange : 5 10i and j value after 10 5
8/7/2019 fundamental of computing(FOC)
12/29
PROGRAM:
\* cube of a number using call by value *\
#includeint 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);}
8/7/2019 fundamental of computing(FOC)
13/29
OUTPUT:
enter the number 5cube of 5 is..125
8/7/2019 fundamental of computing(FOC)
14/29
PROGRAM:
\* factorial without recursion *\
#includevoid main(){
int i,n,fact=1;printf("\n enter the number..");
scanf("%d",&n);for(i=1;i
8/7/2019 fundamental of computing(FOC)
15/29
OUTPUT:
enter the number..5the factorial of 120
8/7/2019 fundamental of computing(FOC)
16/29
PROGRAM:
\* factorial using recursion *\
#includemain(){
int a,num;printf("\n enter the number..");scanf("%d",#);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);}
8/7/2019 fundamental of computing(FOC)
17/29
OUTPUT:
enter the number..5the factorial of the number 5 is 120
8/7/2019 fundamental of computing(FOC)
18/29
8/7/2019 fundamental of computing(FOC)
19/29
PROGRAM:
the size of the file is 12the size of file is 0
8/7/2019 fundamental of computing(FOC)
20/29
PROGRAM:
\* allocated memory *\
#include#includemain(){
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);
}
8/7/2019 fundamental of computing(FOC)
21/29
OUTPUT:
memory contains : MADRASmemory now contains : CHENNAI
8/7/2019 fundamental of computing(FOC)
22/29
PROGRAM:
\* reverse the character *\
#includemain(){
void reverse();printf("\n enter line of text..... ln");
reverse();}
void reverse(){char c;if((c=getchar())!='\n')
reverse();putchar(c);
}
8/7/2019 fundamental of computing(FOC)
23/29
OUTPUT:
enter line of text..... ln karthik
kihtrak
8/7/2019 fundamental of computing(FOC)
24/29
PROGRAM:
\* count the number of words using pointer *\
#includemain(){
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);}
8/7/2019 fundamental of computing(FOC)
25/29
OUTPUT:
enter a string: how are you
4 words in the given string
8/7/2019 fundamental of computing(FOC)
26/29
PROGRAM:
\* pointer to structure *\
#includemain(){
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);}
8/7/2019 fundamental of computing(FOC)
27/29
OUTPUT:
venkat arcot 24201venkat arcot 24201
8/7/2019 fundamental of computing(FOC)
28/29
PROGRAM:
\* sum of numbers using pointers *\
#includemain(){
int b,total=0;int a[5];
int *c;for(b=0;b
8/7/2019 fundamental of computing(FOC)
29/29
OUTPUT:
enter the number 15
enter the number 25
enter the number 36
enter the number 48
enter the number 54
5
5684
total =28