Thursday, 1 December 2011
Wednesday, 9 November 2011
Write a C program in C to carry out following operations on strings using library functions... 1) To concatenate a string s2 to string s1 2) To find the length of a given string 3) To compare two strings s1 nad s2 4) To copy a string s2 to another string s1
# include<stdio.h>
void main()
{
char a[20],b[20],c[20],d[20];
int z1=0,z2=0;
clrscr();
printf("\nEnter First String : ");
scanf("%s",&a);
printf("\nEnter Second String: ");
scanf("%s",&b);
printf("\n*** Length *** \n");
z1 = strlen(a);
printf("\nLength of String1 = %d\n",z1);
z2 = strlen(b);
printf("\nLength of String2 = %d\n",z2);
printf("\n*** Compare ***\n");
if (z1>z2)
{
printf("\nString1 is greater than String2\n");
}
else if(z1<z2)
{
printf("\nString2 is greater than String1\n");
}
else if (z1==z2)
{
printf("\nBoth strings are equal....\n");
}
printf("\n*** Copy ***\n");
printf("\nBefore Copy\n");
printf("\nString1 = %s\n",a);
printf("\nString2 = %s\n",b);
printf("\nAfter Copy\n");
z1 = strcpy(a,b);
printf("\nString1 = %s\n",z1);
printf("\nString2 = %s\n",b);
printf("\n*** Concatenate ***\n");
z1 = strcat(a,b);
printf("\nThe concatinated value is %s\n",z1);
getch();
}
Write a program in C to compute addition/subraction of two matrices.Use functions to read,display and add/subtract the matrices.
# include<stdio.h>
# include<conio.h>
void main()
{
void show(int c[2][2]);
void insert(int l[2][2]);
void add(int a[2][2],int b[2][2]);
void sub(int a[2][2],int b[2][2]);
int x[2][2],y[2][2],op;
clrscr();
printf("\n Enter First Matrix : \n");
insert(x);
printf("\n Eneter Second Matrix: \n");
insert(y);
printf("\n First Matrix is : \n");
show(x);
printf("\n");
printf("\n Second Matrix is : \n");
show(y);
printf(" \n\n");
printf("\n 1) Addition \n");
printf("\n 2) Subtraction \n");
printf("\nEnter your option: \n\n");
scanf("%d",&op);
switch(op)
{
case 1 : add(x,y);
break;
case 2 : sub(x,y);
break;
}
getch();
}
// insert function allow us to insert values of matrix
void insert(int c[2][2])
{
int i,j;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d",&c[i][j]);
} // end of inner for loop
} // end of outer for loop
} // end of insert function
// show function allow us to display the element of matrix
void show(int c[2][2])
{
int i,j;
printf(" \n ");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf(" %d ",c[i][j]);
} // end of inner for loop
} // end of outer for loop
} // end of show function
// add function allow us to add two matrix
void add(int a[2][2],int b[2][2])
{
int i,j,c[2][2];
printf("\n\nAddition is \n\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j] = a[i][j] + b[i][j]; // Addition of two matrix is perform here
printf("\t %d ",c[i][j]);
} // end of inner for loop
} // end of outer for loop
} // end of add function
// sub function allow us to Subtract two matrix
void sub(int a[2][2],int b[2][2])
{
int i,j,c[2][2];
printf("\n\n Sunstraction is \n\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j] = a[i][j] - b[i][j]; // Substraction of two matrix is perform here
printf("\t %d ",c[i][j]);
} // end of inner for loop
} // end of outer for loop
} // end of sub function
// Write a program in C to find out prime numbers between 1 to "N"
#include<stdio.h>
void main()
{
int i,n=1,num;
clrscr();
printf("\nEnter your range to find out prime numbers: ");
scanf("%d",&num);
printf("\nPrime numbers between %d are: \n\n",num);
while(n<num) // Outer while loop
{
i=2;
while(i<num) // Inner while loop
{
if(n%i==0)
{
break;
} // end of if statement
else
{
i++;
} // end of else statement
} // End of inner while loop
if(i==n)
{
printf(" %d ",i);
}
n++;
} // end of outer while loop
getch();
}
Output:-
Enter your range to find out prime numbers: 100
Prime numbers between 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
79 83 89 97
/* Write a program in C to read an integer and display each of digit of the integer in English.*/
void main()
{
long int num=0,n=0,n1=0,rev=0,rev1=0;
clrscr();
printf("\n\nEnter any number: ");
scanf("%ld",&num);
printf("\n\n");
while(num>0)
{
n = (num%10);
rev = (rev*10)+n;
num = (num/10);
}
rev1 = rev;
do
{
n1 = (rev1%10);
rev1 = (rev1/10);
switch(n1)
{
case 0 : printf(" ZERO ");
break;
case 1 : printf(" ONE ");
break;
case 2 : printf(" TWO ");
break;
case 3 : printf(" THREE ");
break;
case 4 : printf(" FOUR ");
break;
case 5 : printf(" FIVE ");
break;
case 6 : printf(" SIX ");
break;
case 7 : printf(" SEVEN ");
break;
case 8 : printf(" EIGHT ");
break;
case 9 : printf(" NINE ");
break;
} // end of switch statement
}while(rev1>0);
getch();
}
Output:-
Enter any number: 1502
ONE FIVE ZERO TWO
Wednesday, 2 November 2011
Print Factorial of given no. by using recursion......
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int num;
clrscr();
printf("\n enter a number : \n ");
scanf("%d",& num);
printf("\n Factorial = %d ", fact(num));
getch();
}
int fact (int num)
{
if (num ==1)
return 1;
else
return ( num * fact(num-1));
}
Tuesday, 4 October 2011
Program to find roots of quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,c,b;
float d,r1,r2;
clrscr();
printf("\n Enter values of coefficient of x^2, x , and constant : \n");
scanf ( "%d%d%d" ,&a,&b,&c);
d = sqrt((b*b) - (4*a*c));
r1= (-b+d)/(2*a);
r2= (-b-d)/(2*a);
printf("\n Roots are : %f and %f ", r1, r2);
getch();
}
Program to print fibonacci series of 1 to 20 nos.
#include<stdio.h>
#include<conio.h>
void main()
{
int fib=0, a=1,b=0, i ;
clrscr();
printf("\n Fibonacii sereis of 1st 20 nos. is :");
for( i =0 ; i <20; i++)
{
printf("\n%d . %5d",i+1,fib);
fib = a + b;
a= b;
b=fib;
}
getch();
}
Program to print factorial of a number by recursion
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&n);
fact = rec(n);
printf("\n Factorial is : %d ",fact);
getch();
}
rec(int n)
{
int f;
if( n==1)
return 1;
else
return f= n * rec(n-1);
}
Program to print GCD of two nos. by method 2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("\nEnter two nos. : \n ");
scanf("%d%d",&n1,&n2);
while(n1!=n2)
{
if(n1>=n2-1)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\n GDCD of is : %d ",n1);
getch();
}
Program to print GCD of two nos.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y, i,m ;
clrscr();
printf("enter two nos.: \n");
scanf("%d%d",&x,&y);
if ( x>y )
m= y ;
else
m=x;
for(i=m;i>=1;i--)
if(x%i==0 && y%i==0)
{
printf("\n GCD is :%d ",i);
break;
}
getch();
}
Program to print given number in words
#include<stdio.h>
#include<conio.h>
void main()
{
int number,reverse=0;
clrscr();
printf("\n Enter a number :");
scanf("%d",&number);
for(;number>0;number/=10)
{
reverse = reverse*10+ number%10;
}
for(;reverse>0;reverse/=10)
{
switch(reverse%10)
{
case 0:printf("Zero ");break;
case 1:printf("One ");break;
case 2:printf("Two ");break;
case 3:printf("Three ");break;
case 4:printf("Four ");break;
case 5:printf("Five ");break;
case 6:printf("Six ");break;
case 7:printf("Seven ");break;
case 8:printf("Eight ");break;
case 9:printf("Nine ");break;
}
}
getch();
}
Program to print reverse number of given number
#include<stdio.h>
#include<conio.h>
void main()
{
int num , rev = 0 , x;
clrscr();
printf( " \n Enter any number : " );
scanf("%d", &num);
while ( num > 0)
{
x= num %10;
rev = rev* 10 + x;
num= num /10;
}
printf("\n Reverse of Given no. is : %d ",rev);
getch();
}
Program 4 from term work FE
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char c[100];
int i,vowel=0, a=0;
clrscr();
printf("\n Ener a string : ");
gets(c);
for(i=0;c[i]!='\0';i++)
{
switch(tolower(c[i]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowel ++ ;
}
switch(tolower(c[i]))
{
case 'a' : a++;
}
}
printf("\n\n You have entered : %s", c);
printf("\n\n number of charcters in given string : %d",i);
printf("\n\n Number of vowels in given string is : %d",vowel);
printf("\n\n Number of accurance of \'a'\ is : %d",a);
getch();
}
Program to check type of triangle.......
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n Enter lengths of 3 sides of triangle : " );
scanf("%d%d%d",&a,&b,&c);
if( a==b && b==c)
{
printf("\n This is an equilateral triangle");
}
else if((a*a)== (b*b) + (c*c) ||(b*b) == (a*a) + (c*c) || (c*c)== (a*a) +(b*b))
{
printf("\n This is a right angled triangle");
}
else if(a==b || b==c || a==c)
{
printf("\n This triangle is isosceles triangle");
}
else
{
printf("\n This triangle is neither equilateral nor isosceles and not rigt angled triangle");
}
getch();
}
Program to find numbers divisible by 4 between 1 to 100 and their sum...
#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum = 0 ;
clrscr();
for ( i =0 ; i <= 100 ; i++)
{
if(i%4 == 0)
{
printf("\n %d",i);
sum+=i;
}
}
printf("\n Sum = %d",sum);
getch();
}
C program to find given year is leap year or not.......
#include<stdio.h>
#include<conio.h>
void main()
{
int yr;
clrscr();
printf("\n Enter a year: ");
scanf("%d",&yr);
if ( yr %100 == 0)
{
if( yr %400 == 0)
{
printf("\n Leap Year ");
}
else
{
printf("\n Not Leap Year ");
}
}
else
{
if (yr %4 ==0)
{
printf("\n Leap Year ");
}
else
{
printf("\n Not Leap Year ");
}
}
getch();
}
Subscribe to:
Posts (Atom)