Thursday, December 6, 2007

List of C Programs for students

/* Ascending array */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,number[100],temp;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements of the array :n");
for(i=1;i<=n;i++)
{
scanf("%d",&number[i]);
}
printf("The array elements entered are: n ");
for(i=1;i<=n;i++)
{
printf("%d n",number[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(number[j]>number[j+1])
{
temp=number[j+1];
number[j+1]=number[j];
number[j]=temp;
}
}
}
printf("The ascending order of the array elements is: n ");
for(i=1;i<=n;i++)
{
printf("%d n",number[i]);
}
getch();
}




/* adding 2 numbers using functions */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int add(int,int);
int a,b,sum;
printf("Enter the two numbers a and b:");
scanf("%d %d", &a,&b);
sum=add(a,b);
printf("the sum of %d and %d is %dn",a,b,sum);
getch();
}

int add(int x,int y)
{
int result;
result=x+y;
return result;
}




/* finding armstrong number */
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
clrscr();
int i,j,a,n,s=0,count=1;
printf("Enter number");
scanf("%d",&n);
i=n;
j=n;
i = i/10;
while( i > 0)
{
count++;
i = i/10;
}
while(j>10)
{
a=j%10;
s=s+pow(a,count);
j=j/10;
}
s=s+pow(j,count);
if(s==n)
{
printf("%d is armstrong numbern",n);
}
else
{
printf("%d is not armstrong number n",n);
}
getch();
}




/* factorial of a given number */
#include <stdio.h>
#include <conio.h>
void main()
{
clrsr();
int fact=1, i, n;
printf("Enter the number for which to find the factorial : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial value of number %d is %dn", n,fact);
getch();
}




/* sum of the individual digit */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int number,sum=0;
printf("nEnter the number:");
scanf("%d",&number);
printf("nThe entered number is %d",number);
while(number>0)
{
sum = sum+(number%10);
number = number/10;
}
printf("nThe sum of individual digits of the given number is %d n",sum);
getch();
}




/* calculate NCR */
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int fact(int);
int n,r,ncr;
printf("Enter the n and r values: ");
scanf("%d%d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The combination %dC%d = %dn",n,r,ncr);
getch();
}

int fact(int x)
{
int i,f=1;
for(i=1;i<=x;i++)
{
f=f*i;
}
return f;
}




/* find number of vowels in a string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char string[50], *flag;
int i,n,count=0;
printf("Enter the string: ");
scanf("%s",string);
n=strlen(string);
for(i=0;i<n;i++)
{
flag=strchr("AEIOUaeiou", string[i]);
if(flag != NULL)
{
count++;
}
}
printf("The number of vowels in the string %s is %dn",string,count);
getch();
}





/* palindrome */
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char string[20], temp[20];
printf("Enter the string:");
scanf("%s",string);
strcpy(temp,string);
strrev(temp);
if(strcmp(string,temp)==0)
{
printf("The String %s is palindrome", string);
}
else
{
printf("The string %s is not palindrome", string);
}
getch();
}





/* find given number is positive or negative */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf("Enter a number: ");
scanf("%d", &n);
if(n<0)
{
printf("%d is negative numbern",n);
}
else
{
printf("%d is positive numbern",n);
}
getch();
}




/* finding prime number */
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int n,i,flag=0;
printf("Enter the number : ");
scanf("%d", &n);
if(n<2)
{
printf("Invalid number enteredn");
exit (0);
}
else
{
for (i = 2; i < n; i++)
{
if ((n % i) == 0)
{
flag=1;
break;
}
}
if(flag == 0)
{
printf("The number %d is prime numbern",n);
}
else
{
printf("The number %d is not prime numbern",n);
}
}
getch();
}




/* reversing the number */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int number,reverse=0;
printf("nEnter the number:");
scanf("%d",&number);
printf("nThe entered number is %d",number);
while(number>0)
{
reverse = reverse*10;
reverse = reverse+(number%10);
number = number/10;
}
printf("nThe reversal number is %d n",reverse);
getch();
}




/* find the length of the string using functions */
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()
{
clrscr();
int l;
char length(char *string[25]);
char *string[25];
printf("Enter the string:");
scanf("%s",string);
l=length(string);
printf("The length of string %s is %d n", string,l);
getch();
}

char length(char *string[25])
{
int i,x;
x=strlen(string);
return x;
}




/* finding string length without using string functions */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char string[10];
int i=0;
printf("Enter the string:");
scanf("%s",string);
printf("nThe string entered is %sn",string);
while(string[i] != '0')
{
i=i+1;
}
printf("nnthe length of string is %d n",i);
getch();
}




/* program to calculate sum of the array */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,number[100],sum=0;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements of the array :n");
for(i=1;i<=n;i++)
{
scanf("%d",&number[i]);
sum=sum+number[i];
}
printf("The array elements are: n ");
for(i=1;i<=n;i++)
{
printf("%d n",number[i]);
}
printf("The sum of the array elements is %d n",sum);
getch();
}




/* program to print a given text */
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
char text[100];
printf("nEnter the text: ");
scanf("%s",text);
printf("nThe text Entered is %s n", text);
getch();
}



/* to find leap year or not */
#include <stdio.h>
#include <conio.h>
void main()
{
int year,leap;
printf("Enter the year:");
scanf("%d",&year);
leap=year%4;
if(leap==0)
{
printf("The year %d is leap year n",year);
}
else
{
printf("The year %d is not leap year n",year);
}
getch();
}




/* program to find biggest among 4 numbers */
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,d;
printf("enter the 4 numbers:");
scanf("%d%d%d%d",&a,&b,&c,&d);
if((a>b) && (a>c) && (a>d))
{
printf("%d is bigger n",a);
}
else if((b>a) && (b>c) && (b>d))
{
printf("%d is bigger n",b);
}
else if((c>a) && (c>b) && (c>d))
{
printf("%d is bigger n",c);
}
else
{
printf("%d is bigger n",d);
}
getch();
}




/* program to find display matrix */
#include <stdio.h>
#include <conio.h>
void main()
{
int matrix[100][100],i,j,row,column;
printf("Enter the row and column of the matrix:");
scanf("%d %d",&row,&column);
printf("Enter the elements of matrix:");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
scanf("%d",&matrix[i][j]);
}
}
printf("The matrix you entered is :n");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
printf("%d t",matrix[i][j]);
}
printf("n");
}
getch();
}



/* program to find number of occurance of alphabet */
#include <stdio.h>
#include <conio.h>
void main()
{
char string[20];
int count,i,j;
printf("Enter the string:");
scanf("%s",string);
i=0;
while(string[i] != '0')
{
count=0;
j=0;
while(string[j] != '0')
{
if(string[i] == string[j])
{
count=count+1;
}
j=j+1;
}
printf("The number occurance of alphabet %c is %dn",string[i],count);
i=i+1;
}
getch();
}

No comments: