Saturday, October 11, 2008

Sample C prog For Demo Of Structure


struct student
{
char name[30];
int mark1,mark2,mark3,mark4,total;
}S[10];
void main()
{int i=0;
clrscr();
printf("Enter the name 7 marks of 4 sub of call student");
for(i=0;i<=10;i++)
{
scanf("%s%d%d%d%d",&S[i].name,&S[i].mark1,&S[i].mark2,&S[i].mark3,&S[i].mark4);
S[i].total=S[i].mark1 + S[i].mark2 + S[i].mark3 + S[i].mark4;

}
printf("the name of the students & total marks are \n");
for(i=0;i<=10;i++)
{

printf("%s \t%d\n",S[i].name,S[i].total);
}getch();
}

Sample C Prog For Recursive Function


#include
#include
void fibo(int, i nt);
void main()
{
int i, t, old=0, current=1,new;
clrscr();
printf("%d\t%d\t",old, current);
fibo(old, current);
printf("\n\n\n\n\npress any key to exit");
getch();
}
void fibo(int old, int current)
{static int terms=2;
int new;
if(terms<20)
{
new=current+old;
printf("%d\t",new);
fibo(current,new);
terms=terms+1;
}
else
return;
}

Sample C Prog For Prime Number


#include
#include
void main()
{
int n,a;
clrscr();
printf("the input value of n is");
scanf("%d",&n);
for(a=2;a{
if(n%a==0);
{
printf("%d is a not a prime number",n);
break;
}
}
if(a==n);
{
printf("%d is a prime number",n);
}
getch();
}

Sample C Prog For Swapping


#include
#include
void main()
{
int a,b,c;
clrscr();
printf("the value of a,b is");
scanf("%d%d",&a,&b);
c=a;
a=b;
b=c;
printf("the value of a is %d,the value of b is%d",a,b);
getch();
}

Sample C prog. For Concatination of Strings


#include
#include
#include
void main()
{
char a[20],b[10];
int i,j;
clrscr();
printf("\n\n\t string compare & concatenation");
printf("\n\nEnter the 1st string :");
gets(a);
printf("\nEnter the 2nd string :");
gets(b);
if(strcmp(a,b)<0)
{
strcat(a,b);
printf("\n\n Concatenated String :");
puts(a);
}
else
printf("\n\n 2nd String is smaller or equal to 1st");
getch();
}

Sample C prog For Matrix Multiplication


#include
#include
void main()
{
int no,m[20][20],r,c;
clrscr();
printf("No till where tables are to be printed");
scanf("%d",&no);
for(r=0;r<10;r++)
{
for(c=0;c<no;c++)
{
m[r][c]=(r+1)*(c+1);
printf("\t %d",m[r][c]);
}
printf("\n");
}
getch();
}

Sample C Prog. For Calculator operations


#include
#include
#include
void main()
{
int a,m;
clrscr();
do
{
printf("\n\nEnter the choice what u want to do?\n");
printf("1.Addition\n2.Substractions\n3.Multiplication\n4.Division\n5.Power\n6.Souare Root\n");
scanf("%d",&a);
switch(a)
{
case 1:
{
int num1,num2,sum;
printf("Enter the 2 nos.::\n");
scanf("%d%d",&num1,&num2);
sum=num1+num2;
printf("Sum=%d",sum);
}break;
case 2:
{
int num3,num4,diff;
printf("Enter the 2 nos.::\n");
scanf("%d%d",&num3,&num4);
diff=num3-num4;
printf("Difference = %d",diff);
}break;
case 3:
{
int num5,num6,prdct;
printf("Enter the 2 nos.::\n");
scanf("%d%d",&num5,&num6);
prdct=num5*num6;
printf("Product = %d",prdct);
}break;
case 4:
{
float num7,num8,div;
printf("Enter the 2 nos.::\n");
scanf("%f%f",&num7,&num8);
div=num7/num8;
printf("Division = %f",div);
}break;
case 5:
{
float num9,num10,power;
printf("Enter the 2 nos.::\n");
scanf("%f%f",&num9,&num10);
power=pow(num9,num10);
printf("value of power=%f",power);
}break;
case 6:
{
float num11,srt;
printf("Enter the no::");
scanf("%f",&num11);
srt=sqrt(num11);
printf("square root of no. = %f",srt);
}break;
}
printf("\n\nWanna do more Calculations...\n1.yes\n2.no\n::");
scanf("%d",&m);
}
while(m==1);

getch();
}

Sample prog. Of C for Checking A Palindrome


#include
#include

void main()
{
char a[50] , b [50];
int x;
clrscr();
printf("Enter string ");
gets(a);
strcpy(b,a);
strrev(b);
x = strcmp(a,b);
if ( x == 0)
{
printf("it is a palindrome");
}
else
{
printf(" it is not palindrome");
}
getch();
}

Sample Program For Adding Elements into the Array


#include
void main()
{
int i , marks [20] ;
float sum = 0 ,avg;
clrscr();
for(i = 0 ; i<20 ;i++)
{
printf("Enter marks :");
scanf("%d",&marks[i]);
}
for(i = 0; i <20 ; i++)
{
sum = sum + marks[i];
}
avg = sum / 20;
printf("\nThe sum is %f",sum);
printf("\nThe average is %f",avg);
getch();
}

Sample program for Concatnating String Using Pointers


#include
#include
#include
void main()
{
char *p[25];
char *q[25];
char a[25], b[25];
int c,d,x;
clrscr();
printf("MENU\n 1. To find string length \n 2. To copy string \n 3. To compare strings \n 4. To concatenate strings\n Enter Option:\n");
scanf("%d",&c);
printf("Enter a string :\n");
scanf("%s",&a);
printf("\nEnter a string:\n");
scanf("%s",& b);
strcpy(*p,a);
strcpy(*q,b);
switch (c)
{
case 1: {
d=strlen(*p);
printf("The string length is:%d",d);
break;
}
case 2:{
strcpy(*p,*q);
printf("copied string is :%s",*p);
break;
}
case 3 : {
x= strcmp(*p,*q);
if (x!=0)
printf("NOT SAME");
else
printf("SAME");
break;
}
case 4: {
printf("Concatenaed strings :%s", strcat(*p,*q));
break;
}
}
getch();
}