Saturday, December 20, 2008

basic C programs

The basic C programs are as follows:

1) program to perform addition of two numbers


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("the sum is=%d",c);
getch();
}

2) progam to display first n numbers

#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf("enter a no upto which u want to display the numbers\n");
scanf("%d",n);
for(i=0;i<=n;i++)
{
printf("%d\n",i);
}
getch();
}

3) program to find biggest of two numbers

#include<stdio.h>

#include<conio.h>
main()
{
int a,b;
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
if(a>b)
{
printf("a is bigger");
}
else
{
printf("b is bigger");
}
getch();
}

4) program to perform biggest of three numbers

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
printf("enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
printf("a is bigger");
}
else
{
if(b>c)
{
printf("b is bigger");
}
else
{
printf("c is bigger");
}
}
getch();
}

5) program to find factorial of a number


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,fact=1;
printf("enter a number\n");
scanf("%d",&n);
for(i=n;i>0;i--)
{
fact=fact*i;
}
printf("The factorial of given no is %d",fact);
getch();
}

6) program to find whether the given number is palindrome or not


#include<stdio.h>
#include<conio.h>
main()
{
int n,d,sum=0,m;
printf("enter a number\n");
scanf("%d",&n);
m=n;
while(n!=0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
if(sum==m)
{
printf("the given no is palindrome");
}
else
{
printf("the given no is not a palindrome");
}
getch();
}

7) program to check whether the given number is armstrong or not


#include<stdio.h>
#include<conio.h>
main()
{
int n,d,m,sum=0;
printf("enter a number\n");
scanf("%d",&n);
m=n;
while(n!=0)
{
d=n%10;
sum=sum+(d*d*d);
n=n/10;
}
if(sum=m)
{
printf("The given number is armstrong");
}
else
{
printf("The given number is not a armstrong");
}
getch();
}

8) program to print sum of individual digits in a given number


#include<stdio.h>
#include<conio.h>
main()
{
int n,d,sum=0;
clrscr();
printf("enter a number\n");
scanf("%d",&n);
while(n!=0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("The sum of individual digits of a given no is %d",sum);
getch();
}

9) program to print reverse of a given number

#include<stdio.h>
#include<conio.h>
main()
{
int n,d,rev=0;
clrscr();
printf("enter a number\n");
scanf("%d",&n);
while(n!=0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
printf("The reverse of a given number is %d",rev);
getch();
}

10) program to display equivalent ASCII value of a character

#include<stdio.h>
#include<conio.h>
main()
{
char c;
printf("enter a character\n");
scanf("%c",&c);
printf("The ASCII value of given character is %d",c);
getch();
}

11) program to print output
*
**
***
****
*****


#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

12) program to find sum of first n numbers

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,sum=0;
printf("enter a number\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
sum=sum+i;
}
printf("The sum of first %d numbers is %d",n,sum);
getch();
}

13) program to display multiplication table


#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
printf("enter a number\n");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("%d*%d=%d\n",n,i,n*i);
}
getch();
}

14) program to display the following output:
1
22
333
4444
55555


#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

15) program to print following output:
1
12
123
1234
12345


#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

16) program to display power of a given number

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,a,m;
clrscr();
printf("enter a number\n");
scanf("%d",&n);
printf("enter the power\n");
scanf("%d",&m);
a=pow(n,m);
printf("%d power %d is %d",n,m,a);
getch();
}

OUTPUT:

enter a number
2
enter a power
5
2 power 5 is 32

17) program to print sum and average of three numbers


#include<stdio.h>
#include<conio.h>
main()
{
float a,b,c,sum=0,avg=0;
printf("enter the numbers\n");
scanf("%f%f%f",&a,&b,&c);
sum=a+b+c;
avg=sum/3;
printf("Sum is %d\n",sum);
printf("Average is %d",avg);
getch();
}

18) program to find area and perimeter of a circle


#include<stdio.h>
#include<conio.h>
main()
{
float rad,area,peri;
clrscr();
printf("enter radius\n");
scanf("%f",&rad);
area=3.147*rad*rad;
peri=2*3.147*rad;
printf("Area is %f",area);
printf("perimeter is %f",peri);
getch();
}

19) program to calculate simple interest
:

#include<stdio.h>
#include<conio.h>
main()
{
float p,t,r,si;
clrscr();
printf("enter values for p,t,r\n");
scanf("%f,%f,%f",&p,&t,&r);
si=(p*t*r)/100;
printf("Simple interest is %f",si);
getch();
}

20) program to find square root of a given number:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,s;
clrscr();
printf("enter a number\n");
scanf("%d",&n);
s=sqrt(n);
printf("squre root of %d is %d",n,s);
getch();
}

22) program to read and print a string:

#include<stdio.h>
#include<conio.h>
main()
{
char name[30];
clrscr();
printf("enter a string\n");
scanf("%s",name);
printf("The entered string is %s",name);
getch();
}

OUTPUT:

enter a string
sanjeev
The entered string is sanjeev

USAGE OF STRING LIBRARY FUNCTIONS:

22) program to find length of a given string:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char c[30];
int l;
clrscr();
printf("enter a string\n");
gets(c);
l=strlen(c);
printf("length of a string is %d",l);
getch();
}

OUTPUT:
enter a string
sanjeev
length of a string is 7

23) program to print reverse of given string:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[30];
clrscr();
printf("enter a string\n");
gets(x);
strrev(x);
printf("reverse of given string is %s",x);
getch();
}

OUTPUT:
enter a string
sanjeev
reverse of given string is veejnas

24) program to demonstrate strcat() function(i.e program to concatenate two strings):


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[30],y[30];
clrscr();
printf("enter first string\n");
gets(x);
printf("enter second string\n");
gets(y);
strcat(x,y);
printf("concatenated string is %s",x);
getch();
}

OUTPUT:
enter first string
sanjeev
enter second string
ravadi
concatenated string is sanjeevravadi

25) program to convert lowercase string into uppercase string:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[30];
clrscr();
printf("enter a lowercase string\n");
gets(x);
strupr(x);
printf("uppercase string is %s",x);
getch();
}

OUTPUT:
enter a lowercase string
sanjeev
uppercase string is SANJEEV

26) program to convert uppercase string into lowercase string:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[30];
clrscr();
printf("enter an uppercase string\n");
gets(x); //gets() is a function used to read string
strlwr(x);
printf("lowercase string is %s",x);
getch();
}

OUTPUT:
enter an uppercase string
SANJEEV
lowercase string is sanjeev

27) program to compare two strings:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[30],y[30];
int a;
clrscr();
printf("enter first string\n");
gets(x);
printf("enter second string\n");
gets(y);
a=strcmp(x,y);
printf("the value is %d",a);
getch();
}

OUTPUT:
enter first string
sanjeev
enter second string
SANJEEV
The value is 32
/*here strcmp() function compares the given strings and returns three types of
values i.e. positive,negative and zero.*/

28)program to print odd number series:


#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
printf("enter a number\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
if(i%2==1)
{
printf("%d\n",i);
}
}
getch();
}

OUTPUT:
enter a number
5
1
3
5