Wednesday, October 6, 2010

C program to compute exponential series with nested for stattement

#include math.h
int main()
{
float x,e=0.0;
int i,j,f=1;
printf("Enter the value of x");
scanf("%f",&x);
for(i=0;i< 7;i++)
{
for(j=i;j>1;j--)
{
f=f*j;
}
e+=pow(x,i)/f;
f=1;
}
printf("%f",e);
getch();
}


C Program to generate Fibonacci sequence

//to generate fibonacci
int main()
{
int ft=-1,st=1,nt,n,c;
printf("enter any number \n");
scanf("%d",&n);
for(c=1;c<=n;c++)
{
nt=ft+st;
ft=st;
st=nt;
printf("%d\t",nt);
}
system("pause");
return 0;
}

C Program to find factorial of a number

int main()
{
int n,i,f=1;
printf("enter any num \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("factorial is %d ",f);
system("pause");
}

Friday, October 1, 2010

program to print Multiplication Table from 1 to 10

int main()
{
int x,y;
for(x=1;x<=10;x++)
{
for(y=1;y<=10;y++)
{
printf("%d*%d=%d\n",x,y,x*y);
}
printf("\n\n");
}
getch();
}

Armstrong Number program using C language source code

Check Armstrong Number program using C


int main()
{
int n,s=0,t;
printf("Enter number to be checked");
scanf("%d",&n);
t=n;
do
{
s=s+pow((n%10),3);
printf("%d",pow((n%10),3));
printf("%d\n",s);
n/=10;
}
while(n);
if(s==t)
printf("It is a Armstrong number");
else
printf("It is not an Armstrong number");
getch();
}

Checking for leap year using simple C program

int main()
{
int y;
printf("Enter a year between 1000 to 2000:");
scanf("%d",&y);
if(y%100==0)
{
if(y%400==0)
printf("It is a leap year");
else
printf("Not a leap year");
}
else
{
if(y%4==0)
printf("It is a leap year");
else
printf("Not a leap year");
}
getch();
}

C program to print the even and odd numbers from 1 to 1000.

int main()
{
int i;
printf("Odd numbers between 1 to 1000\n");
for(i=1;i<=1000;i=i+2)
{
printf("%5d",i);
if(i%30==0)
printf("%\n");
}
printf("\nEven numbers between 1 to 1000\n");
for(i=2;i<=1000;i=i+2)
{
printf("%5d",i);
if(i%30==0)
printf("%\n");
}
getch();
}

C program to print all prime numbers between 1 and 100.

int main()
{
int i,j,n=0;
for(i=2;i<=100;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
{
n++;
}
}
if(n==2)
printf("%d\n",i);
n=0;
}
getch();
}

C program to display all the printable ASCII characters in a tabular form, each row with 10 characters.

int main()
{
int a;
for(a=0;a<=255;a++)
{
printf("%4d%2c",a,a);
if(a%10==9)
{
printf("\n");
}
}
getch();
}

C program to convert given input character from Upper case to Lower case.

main()
{
char a;
printf("Enter an Upper case Character");
a=getchar();
a=a+32;
printf("%c",a);
getch();
}

C program to check the given input character is Upper case, Lower case and digit.

//to check if entered character is uppercase or lowercase
//alphabet or is a digit
main()
{
char a;
printf("Enter the character\n");
a=getchar();
if(isalpha(a))
{
printf("It is a Alphabet\n");
if(isupper(a))
{
printf("It is in upper case\n");
}
else
{
printf("It is in lower case\n");
}
}
else if(isdigit(a))
{
printf("It is a digit\n");
}
system("pause");
}

Simple nested if program in c language

Code a C program to classify the given inputted number is single, two, three and
four digit using nested if … else statements.

int main()
{
int n;
printf("Enter a number with less than or equal to 4 digits: ");
scanf("%d",&n);
int x=0;
if(n/10000==0)
{
x=4;
if(n/1000==0)
{
x=3;
if(n/100==0)
{
x=2;
if(n/10==0)
{
x=1;
}
}
}
}
printf("It is a %d digit number",x);
getch();
}

Simple if else ladder program in C language

Write a program to calculate gross salary for the conditions given below.
BASIC SALARY(Rs) DA(Rs) HRA(Rs) CONVEYANCE(Rs)
----------------------- ----------------- ---------------- ----------------------
BS>=5000 110% of Basic 20% of Basic 500
BS>3000 &&
BS<5000 100% of Basic 15% of Basic 400
BS<3000 90% of Basic 10% of Basic 300


int main()
{
float bs,gs;
printf("Enter basic salary");
scanf("%f",&bs);

if(bs>=5000.0)
{
gs=bs+500.0+(.110*bs)+(.20*bs);
}else if(bs>=3000)
{
gs=bs+400.0+(0.1*bs)+(0.15*bs);
}
else
{
gs=bs+300.0+(0.90*bs)+(0.10*bs);
}
printf("Gross Salary=Rs%.2f",gs);
getch();
}

C program to exchange the values in two variables (swap) with and without using temporary variables.

//to swap the values of variable without using third variable
main()
{
int a,b;
printf("Enter the value of variable a\n");
scanf("%d",&a);
printf("Enter the value of variable b\n");
scanf("%d",&b);
printf("a=%d\nb=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("Swaping done....\n");
printf("Now a=%d\nb=%d\n",a,b);
system("pause");
}

C code to find the greater of two integers using the relational operator and if statement

int main()
{
int a,b;
printf("enter two numbers\n");
scanf("%d %d",&a,&b);
if(a>b)
printf("a (%d) is greator",a);
else
printf("b (%d) is greator",b);
getch();
}

to compute maturity value on deposit of Rs 12,000 for 2 years with a simple interest rate of 7%. (Interest=PNR/100)

int main()
{
float p=12000,r=7,n=2,a;
a=p+(p*n*r)/100;
printf("maturity value =%f",a);
getch();
return 0;
}

Code a program to convert decimal input into octal and hexadecimal formats. (with proper prefix for octal (0) and hexadecimal (0x)

int main()
{
int a;
printf("Enter a number:");
scanf("%d",&a);
printf("Octal equivelent of %d is %o\n",a,a);
printf("Hexadecimal equivelent of %d is %x\n",a,a);
getch();
return 0;
}

conditional operators in c

int main()
{
int a,b,max;
printf("enter two numbers\n");
scanf("%d %d",&a,&b);
max=(a>b)? a : b;
printf("greator number is %d",max);
getch();
}

to subtract 45 from the decimal ASCII value of character ‘a’. Divide it by 3.5.

to subtract 45 from the decimal ASCII value of character ‘a’. Divide it by
3.5. Print the result up to two decimal places.

int main()
{
char ch='a';
float a=ch;
a-=45;
a/=3.5;
printf("%.2f",a);
getch();
}

C program to print the memory sizes of all data types (with qualifiers)

int main()
{
int a;
char b;
float c;
double d;
long double e;
unsigned int f;

printf("Size of int data type is %d\n",sizeof(a));
printf("Size of char data type is %d\n",sizeof(b));
printf("Size of float data type is %d\n",sizeof(c));
printf("Size of double double data type is %d\n",sizeof(d));
printf("Size of long double int data type is %d\n",sizeof(e));
printf("Size of unsigned int data type is %d\n",sizeof(e));
system("pause");
return 0;
}

Swich statement in c program

Code programs to calculate the following:
a) Area of a square
b) Area of a rectangle
c) Area of a triangle
d) Area of a circle

main()
{
printf("Please enter your choice\n");
printf("1)Area of a square\n2) Area of a rectangle\n");
printf("3) Area of a triangle\n4) Area of a circle\n");
int ch;
scanf("%d",&ch);
float a,s,l,b,B,h,r;
switch(ch)
{
case 1:
printf("enter length of side of square\n");
scanf("%f",&s);
a=s*s;
break;
case 2:
printf("enter Length and width of rectangle\n");
scanf("%f%f",&l,&b);
a=l*b;
break;
case 3:
printf("enter base and height of triangle\n");
scanf("%f%f",&B,&h);
a=0.5*B*h;
break;
case 4:
printf("enter radius of circle\n");
scanf("%f",&r);
a=3.1415*r*r;
break;
default:
printf("WRONG CHOICE !!!!!\n");
exit(0);
}
printf("Area=%f\n",a);
getch();
return 0;
}

C program to convert Fahrenheit to Centigrade temperature scale. (F=(C*9/5)+32)

int main()
{
float f,c;
printf("Enter Fahrenheit");
scanf("%f",&f);
c=((f-32.0)*5.0)/9.0;
printf("%f Fahrenheit=%f Centigrade",f,c);
getch();
return 0;
}

C source code to Convert miles to kilometers and print with proper sentence for the user. (1 mile = 1.609 km)

C source code to Convert miles to kilometers 

int main()
{
float k,m;
printf("Enter miles");
scanf("%f",&m);
k=m*1.602;
printf("%f miles=%f kilometers",m,k);
getch();
return 0;
}

A 'C' program to evaluate a simple arithmetic expression

int main()
{
float a=5,b=7,c;
c=a+(b*2)/3*a*5+3/2;
printf("%f",c);
getch();
return 0;
}

Program in c to display a statement using printf

Q)With one printf() statement, print the following pattern of output:
I am
learning C
at SITE Lab

int main()
{
printf("I am\n\tlearning C\n\t\tat SITE Lab");
getch();
return 0;
}