Monday, 23 February 2015

Storage Classes in C


1.    Automatic
2.    Extern
3.    Static
4.    Register

Variables:
Automatic Variables:

#include<stdio.h>
#include<conio.h>
void main()
{
long int n;
long int factorial(n);
printf(“Enter the number:”);
scanf(“%d”,&n);
printf(“\n %ld”,factorial(n));
getch();
}
long int factorial(int n)
{
int i;
long int prod=1;
if(n>1)
for(i=2;i<=n;++i)
prod *= i;
return(prod);
}

Recursive function:

#include<stdio.h>
#include<conio.h>
void main()
{
long int factorial(int);
int n;
clrscr();
printf(“n=”);
scanf(“%d”,&n);
printf(“\n\n %d!=%ld\n”,n,factorial(n));
getch();
}
long int factorial(int number)
{
if(number>1)
return(number * factorial(number – 1));
else
return(1);
}

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter an integer:”);
scanf(“%d”,&n);
clrscr();
fact(n);
getch();
}
fact(x)
int x;
{
int i;
long int value=1;
for(i=1;i<=x;i++)
value *= i;
printf(“\t\t Value of %d and its factorial is %ld”,x,value);
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n;
float p,r;
clrscr();
printf(“Enter p,n,r values:”);
scanf(“%f%d%f”,&p,&n,&r);
display(p,n,r);
getch();
}
display(p1,n1,r1)
int n1;
float p1,r1;
{
float si,ci,d;
si=p1*n1*r1/100;
d=(1+r1/100);
ci=p1*pow(d,n1)-p1;
line();
printf(“SI=%f\n”,si);
line();
printf(“CI=%f\n”,ci);
line();
}
line()
{
int i;
for(i=0;i<15;i++)
printf(“*”);
printf(“\n”);
}

#include<stdio.h>
#include<conio.h>
int add(int,int);
int sub(int,int);
int mul(int,int);
int div(int,int);
void main()
{
int x,y,ch,z;
clrscr();
printf(“\n 1. Adition”);
printf(“\n 2. Subtraction”);
printf(“\n 3. Multiplication”);
printf(“\n 4. Division”);
printf(“Enter two numbers:”);
scanf(“%d%d”,&x,&y);
printf(“Enter your choice:”);
scanf(“%d”,&ch);
if(ch= =1)
{
z=add(x,y);
printf(“The addition value:%d\n”,z);
}
if(ch= =2)
{
z=sub(x,y);
printf(“The subtraction value:%d\n”,z);
}
if(ch= =3)
{
z=mul(x,y);
printf(“The multiplication value:%d\n”,z);
}
if(ch= =4)
{
z=div(x,y);
printf(“The division value:%d\n”,z);
}
getch();
}
add(int a,int b)
{
return a+b;
}
sub(int a,int b)
{
return a-b;
}
mul(int a,int b)
{
return a*b;
}
div(int a,int b)
{
return a/b;
}

No comments:

Post a Comment