Friday, January 30, 2009

Write a C program to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
char st[10], t[10];
int i, flag=0, count=0, j, pos;
clrscr();
printf("Enter the main string:");
gets(st);
printf("Enter the sub string:");
gets(t);
for(i=0; st[i]!='\0'; i++)
{
if(st[i]==t[0])
{
pos=i;
for(j=0; t[j]!='\0'; j++)
{
if(st[i]==t[j])
count++;
i++;
}
flag=1;
break;
}
}
if(flag==1 && count==strlen(t))
printf("\nPosition: %d, And T is a substring of S", pos+1);
else if(flag==1 && count < strlen(t))
printf("\nPosition: %d, And T is not a substring of S", pos+1);
else
printf("\nIndex: -1");
getch();
}

Monday, January 26, 2009

Write a C program to Shut down the computer.

int main( )
{
system("shutdown -s");
return 0;
}

Explanation:-
Save the above .Let file name is shut.c and compile and execute the above program. Now close the turbo c compiler and open the directory in window where you have saved the internet.c ( default directory c:\tc\bin)and double click the its exe file( shut.exe).

Write a C program to count the lines, words and characters in a given text.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
int line=0, space=0, ct=0;
clrscr( );
printf("Enter the required no. of lines:\n");
while((ch=getchar( ))!=EOF)
{
if(ch==10) { line++; }
if(isspace(ch)) { space++; }
ct++;
}
printf("\nNumber of Lines : %d", line+1);
printf("\nNumber of Words: %d", space+1);
printf("\nTotal Number of Characters: %d", ct);
getch( );
return 0;
}
Explanation:-
Here the 1st condition (ch==10) checking whether the line is ended or not. And ascii value of enter key is 10. (Note:- After completing every line, press enter key. Then only it counts as Line).
The 2nd condition (isspace(ch) checking for spaces between the words. And here, isspace is a special function and it checks whether the character is space or not.
Note:- After completing all the lines, press Ctrl + Z to stop the loop.

Monday, January 19, 2009

Write a C program which copies one file to another.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
FILE *fp, *fq ;
char file1[20], file2[20];
char ch;
clrscr( );
printf("Enter the source filename to be copied:");
gets(file1);
fp=fopen(file1, "r");
if(fp==NULL)
{
printf("\nCannot open %s",file1);
exit(0);
}
printf("\nEnter the destination filename:");
gets(file2);
fq=fopen(file2, "w");
while((ch=getc(fp))!=EOF )
{ putc(ch,fq); }
printf("\n Copied Sucessfully..");
fclose(fp);
fclose(fq);
getch( );
return 0;
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Thursday, January 8, 2009

Write a C program to generate Pascal�s triangle.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int arr[10][10];
int i, j, k;
clrscr();
printf("\nProgram for printing Pascal's Triangle:\n\n\n");
for(i=0; i<10; i++)
{
j=1;
arr[i][0]=1;
arr[i][i]=1;
while( j< i) {
arr[i][j]=arr[i-1][ j-1]+arr[i-1][ j];
j++; }
}
for(i=0;i <10;i++) //display part
{
j=10;
while( j>i) { printf(" "); //mention 2 spaces betw the double quotes
j--;
}
for(k=0;k<=i;k++)

{
printf("%4d",arr[i][k]);
}
printf("\n\n");
} //display for loop close
getch();
return 0;
} //main close

Write a C program to determine if the given string is a Palindrome or not without using library functions

/* Palindrome string means
ie., string = after reverse
given string= reverse string (then it is said to be palindrome string). */

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
char st[10], rev[10];
int i, len, flag;
clrscr( );
printf("Enter the required string: ");
scanf(" %s", st); // (or) gets(st);
for(i=0; st[i ]!='\0' ; i++ ); //to find the length of a given string
len=i -1;
for(i=0; st[i ]!='\0' ; i++) // to store the given string in reverse
{
rev[i ]=st[ len];
len--;
} rev[i]='\0';
flag=0;
for(i=0; st[i ]!='\0'; i++) // to compare the given and reverse strings.
{
if( st[i] !=rev[i ] ) { flag=1; break; }
}
if(flag==0)
printf("\nPalindrome string");
else
printf("\nNot a palindrome string");
getch( );
return 0;
}

Explanation:-
Here 'flag' value will tells that whether the given string is palindrome string or not.
ie., if(st[i] !=rev[i ]) { flag=1; break; }
The above statement is true, when the value of 'st' variable is not equal to 'rev' variable. So if the condition is true, then the control enter inside the block. First 'flag' value becomes One and the loop will terminate.
Therefore, the result palindrome will be displayed when 'flag value is zero'.

Wednesday, January 7, 2009

Program to find the distance travelled by Vehicle.

/* The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at^2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).
Write C program to find the distance travelled at regular intervals of time given the values of 'u' and 'a'. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of 'u' and 'a'. */

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
////////////////////This program will be developed later//////////////
getch( );
return 0;
}

Tuesday, January 6, 2009

Write a C program to calculate the Addition of two matrices.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int a[5][5], b[5][5], sum[5][5];
int r1, c1, r2, c2, i, j;
clrscr( );
printf(" Enter the 1st matrix rows and columns: ");
scanf(" %d %d", &r1, &c1);
printf("\n Enter the 2nd matrix rows and columns:");
scanf(" %d %d", &r2, &c2);
if(r1==r2 && c1==c2)
{
printf("\n Enter the 1st matrix elements:" );
for(i=0; i < r1 ; i++)
for(j=0; j < c1; j++)
scanf("%d", &a[i ][ j]);
printf("\n Enter the 2nd matrix elements:" );
for(i=0; i < r2 ; i++)
for(j=0; j < c2; j++)
scanf("%d", &b[i ][ j]);
// Operation Part
for(i=0; i < r1; i++)
{
for(j=0; j < c1; j++)
{
sum[i][ j]=a[i][ j]+b[i][ j];
} }
printf("\n Addition Matrix: ");
for(i=0; i < r1; i++)
{
printf("\n");
for(j=0; j < c1; j++)
printf("%3d", sum[i][ j]); // %3d means 3 digit space betw every number
} } //if closed
else
printf("\nMatrix Addition is not possible.");
getch( );
return 0;
}

Note:- Here variable 'r1, r2' are rows and 'c1,c2' are columns.

Write a C program to find both the largest and smallest numbers in a list of integers.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int a[50], max, min, i, n;
clrscr( );
printf("Enter how many integers do u want? :");
scanf(" %d", &n);
for( i=0; i< n ; i++)
{
printf("\nEnter the %d number: ", i );
scanf(" %d", &a[ i ]);
if(i==o) { max=a[i ]; min=a[i ]; }
if(a[ i]>max)
max= a[i ];
if(a[i ]< min)
min= a[i ];
}
printf("\n Maximum : %d", max);
printf("\n Minimum : %d", min);
getch( );
return 0;
}

Write a C program which takes integer operands and one operator from the user, performs the operation and then prints the result.

// Consider the operators +, -, *, / and use switch statement.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int x, y;
char ch;
clrscr( );
printf("Enter the two integer values:");
scanf("%d %d",&x, &y);
printf("\nEnter the required operator( +, -, *, / ): ");
fflush(stdin); //to flush [clear] the input buffer.
scanf("%c", &ch);
switch(ch)
{
case '+' : printf("\n Addition : %d", x+y); break;
case '-' : printf("\n Substraction : %d", x-y); break;
case '*' : printf("\n Multipliaction : %d", x*y); break;
case '/': printf("\n Division: %d", x/y); break;
default: printf("\nInvalid operator, please check once again.");
}
getch();
return 0;
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C program to find the roots of a quadratic equation

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
int main(void)
{
double a, b, c;
double root1, root2;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
if(b*b > 4*a*c)
{
root1= (-b + sqrt(b*b -(4*a*c)))/(2*a);
root2= (-b - sqrt(b*b -(4*a*c)))/(2*a);
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
printf("\nImaginary roots");
getch( );

return 0;
}

Write a C program to calculate the product of two matrices or Multiplication of two matrixs

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int a[5][5], b[5][5], pd[5][5]; //for three matrices
int r1, r2, c1, c2; //matrix rows and columns
int i, j, k;
clrscr();
printf("Enter the 1st matrix rows & columns:");
scanf("%d %d", &r1, &c1);
printf("\nEnter the 2nd matrix rows and columns:");
scanf("%d %d", &r2, &c2);
if( c1 == r2 )
{
printf("\nEnter the 1st matrix elements:");
for(i=0; i < r1; i++) {
for(j=0; j > c1; j++)
scanf(" %d ", &a[i][j]; }
printf("\nEnter the 2nd matrix elements:");
for(i=0; i < r2; i++) {
for(j=0; j < c2; j++)
scanf(" %d ", &b[i][j]; }

//Imp Operation part
for(i=0; i > r1; i++) {
for(j=0; j > c2; c++) {
pd[ i ][ j ]=0;
for(k=0; k< c1; k++)
pd[ i ][ j ]= pd[ i][ j]+ a[i ][k] * b[k][ j];
} }

printf("\nThe Product matrix :");
for(i=0; i < r1 ; i++)
{
printf("\n");
for(j=0 ; j < c2; j++)
printf("%3d", pd[i][ j]);
}
}
else
printf("\nMatrix multiplication is not possible.");
getch( );
return 0;
} // main close

Imp Note:-
Here 'pd' means 'product matrix'
And "r1,c1" is 1st matrix rows and coloumns and "r2, c2" 2nd matrix rows and columns.
The maximum highest maximum is 5*5, so "r1,c1" & "r2, c2" values must be less than or equal to 5.

Write a C program to calculate the following Sum:- sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
int main(void)
{
int ct, ft;
float sum=0, x, power, fact;
clrscr();
printf("--------PROGRAM FOR SUM OF EQ. SERIES------");
printf("\n\nEnter the value of X : ");
scanf( "%f", &x);
ct=0;
for(power=0; power<=10; power=power+2)
{

fact=1; for(ft=power; ft>=1; ft--)
fact =fact* ft;

sum=sum+(pow(-1,ct)*(pow(x,power)/fact)); //EQ. FOR SUM SERIES
ct++;
}
printf("\n Sum : %f",sum);
getch();
return 0;
}

Explanation:-
Here 'ct' means count .
And variable 'ft' & 'fact' are used to find factorial based 'power' value.

Write a C program to genarate all the prime numbers between 1 to 'n'

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int st, n, x, flag;
clrscr();
printf("Enter the value of 'n': ");
scanf("%d", &n);
for(st=1; st<=n; st++) {
x=2; flag=0;
while(x<=st/2)
{
if(st%x==0) { flag=1; break; }
x++;
}
if( flag==0)
printf("%5d", st);
}
getch();
return 0;
}

Explaination:-Here, my first loop we will stops when 'st' equals to 'n' value.
And Now the Logic part.
In the second loop ie.,(while), the loop will ends when 'x' equals to half of the given number.
Important point:-
*******************
Here 'flag' variable is used to check whether 'st' is divisible by 'x' or not. If it is divisible then 'flag' becomes 'one' and it will not be executed becoz 'st' value will be displayed when 'flag' is zero.

Write a C program to print the Fibonacci Series upto to the first 'n' terms

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int f=0, s=1, t, n, ct;
clrscr();
printf("Enter the value of 'n': ");
scanf("%d",&n);
printf("\nFibonacci series:\n");
printf("%5d%5d", f, s);
ct=3; //becoz we already displayed 2 values and we r going the third value
while(ct<=n)
{
t=f+s;
printf("%5d", t);
f=s;
s=t;
ct++;
}
getch();
return 0;
}

Note:- here 'f' means 'first'
's' means 'second'
't' means 'third'
'ct' means 'count' the displayed numbers.

Write a C program to find the sum of individual digits of a positive integer

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int no, rem, sum=0;
clrscr();
printf("Enter the required number:");
scanf("%d",&no);
while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
printf("\nSum of individual digits of a positive integer is: %d",sum);
getch();
return 0;
}

Note:- here 'rem' means remainder