Articles by "C Language"
Showing posts with label C Language. Show all posts
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program that displays the position or index in the string S where the string T begins, or 1 if S doesn’t contain T.

Program Code:
#include <stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
found = strstr(s, t);
if(found)
{
printf("Second String is found in the First String at %d position.\n", found - s);

}
else
{
printf("-1");
}
getch();
}



no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program that reads a line of text and counts all occurrence of a particular word.

Program Code:
#include <stdio.h>
int main()
{
char str1[50], str2[50], str3[50], temp[50];
int len1, len2, len3, i, j, p, match, k,count=0;
printf("\n\n\t ENTER A SENTENCE:");
gets(str1);
printf("\n\n\t ENTER A STRING WHICH YOU WANT TO COUNT THE TIMES REPEATED: ");

gets(str2);
i=0; //Finding the length of sentence
while(str1[i]!='\0')
{
i++;
}
len1 = i-1;
i=0; //Finding the length of string, to delete
while(str2[i]!='\0')
{
i++;
}
len2 = i-1;
for(i=0;i<=len1;i++)
{
match = 1;
for(j=0;j<=len2;j++)
{
if(str2[j]!=str1[i+j])
{
match = 0;
break;
}
}
if(match)
{
count++;
}
}
printf("\t\t%s occurred %d times",str2,count);
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program to find a string within a sentence and replace it with another string.

Program Code:
#include <stdio.h>
int main()
{
char str1[50], str2[50], str3[50], temp[50];
int len1, len2, len3, i, j, p, match, k;
printf("\n\n\t ENTER A SENTENCE:");
gets(str1);
printf("\n\n\t ENTER A STRING WHICH YOU WANT TO DELETE: ");
gets(str2);
printf("\n\n\t ENTER A NEW STRING WHICH YOU WANT TO INSERT: ");
gets(str3);
i=0; //Finding the length of sentence
while(str1[i]!='\0')
{

i++;
}
len1 = i-1;
i=0; //Finding the length of string, to delete
while(str2[i]!='\0')
{
i++;
}
len2 = i-1;
i=0; //Finding the length of new string, to replace
while(str3[i]!='\0')
{
i++;
}
len3 = i-1;
for(i=0;i<=len1;i++)
{
match = 1;
for(j=0;j<=len2;j++)
{
if(str2[j]!=str1[i+j])
{
match = 0;
break;
}
}

if(match){ //When a substring is found, copy remaining String to temporary String
for(k=0,j=i+len2+1;j<=len1;j++,k++){
temp[k] = str1[j];
}
temp[k] = '\0';
for(j=0;j<=len3;j++)
{
str1[i+j] = str3[j];
}
str1[i+j] = '\0';
for(p=0;temp[p]!='\0';p++)
{
str1[i+j+p]=temp[p];
}
str1[i+j+p]='\0';
len1 = len1-len2 +len3; //Update length value
i = i + j;
}
}
printf("\n\n\t OUTPUT IS : ");
puts(str1);
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program to determine if the given string is a palindrome or not.

Program Code:
#include <stdio.h>
int main() {
int i,j,s1=0,count=0;
charstr[100],temp[100];
printf("Enter a string : ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
s1++;
}
j=0;
for(i=s1-1;i>=0;i--)
{
temp[j] = str[i];
j++;
}
temp[s1] = '\0';
for(i=0;i<s1;i++)
{
if(str[i]==temp[i]&&str[i]!='\0'&&temp[i]!='\0') 

{
count++;
}
}
if(count==s1)
{
printf("PALINDROME");
}
else
{
printf("NOT PALINDROME");
}
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program that uses functions to perform the following operations:
i. To insert a substring into a given main string from a given position.
ii. To delete n characters from a given position in a given string.

Program Code i:
#include <stdio.h>
#include <string.h>
int main() {

int i,j,p,s1=0,s2=0,tem=0;
char str1[100],str2[100],temp[100];
printf("Enter the first string : ");
gets(str1);
printf("Enter the second string : ");
gets(str2);
printf("Enter the position to insert the second string : ");
scanf("%d",&p);
for(i=0;str1[i]!='\0';i++)
{
s1++;
}
for(i=0;str2[i]!='\0';i++)
{
s1++;
}
for(i=0;temp[i]!='\0';i++)
{
tem++;
}
j=0;
for(i=p;i<s1;i++)
{
temp[j] = str1[i];
j++;
}
str1[p] = '\0';
j=0;
for(i=p;i<(s1+s2);i++)
{
str1[i] = str2[j];
j++;
}
strcat(str1,temp);
puts(str1);
}

Program Code ii:
#include <stdio.h>
int main() {
int i,p,j,s1=0,n;
charstr[100],temp[100];
printf("Enter the string : ");
gets(str);
printf("Enter the position : ");
scanf("%d",&p);

printf("Enter the number of characters you want to delete : ");
scanf("%d",&n);
for(i=0;str[i]!='\0';i++) 
{
s1++;
}
j=0;
for(i=p+n;i<s1;i++) 
{
temp[j] = str[i];
j++;
}
str[p] = '\0';
j=0;
for(i=p;i<s1-p+n;i++) 
{
str[i] = temp[j];
j++;
}
puts(str);
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program to merge two sorted arrays into another array in a sorted order.

Program Code:
#include<stdio.h>
#include<conio.h>
void main( )
{

int n,m,i,j,k,c[40],a[20],b[20];
printf("Enter how many elements for array A?:\n");
scanf("%d",&n);
printf ("Enter how many elements for array B?:\n");
scanf("%d",&m);
printf("Enter elements for A:-\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter elements for B:-\n");
for(j=0;j<m;j++)
scanf("%d",&b[j]);
i=j=k=0;
while(i<n&&j<m)
{
if(a[i]<b[j])
c[k++]=a[i++];
else
if(a[i]>b[j])
c[k++]=b[j++];
else
{
c[k++]=b[j++];
i++;
j++;
}
}
if(i<n)
{
int t;
for(t=0;t<n;t++)
c[k++]=a[i++];
}
if(j<m)
{
int t;
for(t=0;t<m;t++)
{

c[k++]=b[j++];
}
}
printf("\n\n Merged Array C:\n\n")
for(k=0;k<(m+n);k++)
printf("\t \n %d ",c[k]);
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program to count and display positive, negative, odd and even numbers in an array.

Program Code:
#include<stdio.h>
void main()
{

int a[10],i,o=0,p=0,e=0,n=0;
printf(" enter 10 nos\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
e++;
}
else
{
o++;
}
if(a[i]>=0)
{
p++;
}
else
{
n++;
}
}

printf("\n even nos.=%d",e);
printf("\n odd nos.=%d",o);
printf("\n +ve nos.=%d",p);
printf("\n -ve nos.=%d",n);
}
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program to perform the following:
i. Addition of two matrices
ii. Multiplication of two matrices


Program Code:
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],i,j,m,n,p,q;
printf(" enter row & column of Mat1\n");
scanf("%d%d",&m,&n);
printf(" enter row & column of Mat2\n");
scanf("%d%d",&p,&q);
if(m==p&&n==q)
{
printf(" enter Mat1\n");
for(i=0;i<m;i++)
{ 
for(j=0;j<n;j++)
{ 
scanf("%d",&a[i][j]);
}
}
printf(" enter Mat2\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{ 
scanf("%d",&b[i][j]);
}
}
printf("\n sum of the matrices\n");
for(i=0;i<m;i++)
{ 
for(j=0;j<n;j++)
{ 
printf("%8d",a[i][j]+b[i][j]);
}
printf("\n");
}
printf("\n difference of the matrices\n");
for(i=0;i<m;i++)
{ 
for(j=0;j<n;j++)
{ 
printf("%8d",a[i][j]-b[i][j]);
}

printf("\n");
}
}
else
printf("\n operations not possible...");
}

no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program to find the second largest integer in a list of integers.

Program Code:

#include<stdio.h>
void main()
{
int a[10],i,j,t;
printf(" enter 10 nos\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[j]>a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\n second largest no.=%d",a[8]);
}

no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +, -, *, /, % and use switch statement).

Program Code:
#include<stdio.h>
void main()
{
int a,b;
char op;
printf("\n Enter two number a,b,an operator: ");
scanf("%d %d %c",&a,&b,&op);
switch(op)
{

case'+':printf("\n Sum=%d",a+b);break;
case'-':printf("\nDifference=",a-b);break;
case'*':printf("\nProduct=",a*b);break;
case'/':printf("\nQuotient=",a/b);break;
case'%':printf("\nRemainder=",a%b);break;
default:printf("\n Please enter a valid input");
break;
}
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program to calculate the following sum:
sum = 1 – x2 /2! + x4 /4! – x6 /6! +x8 /8! – x10/10!

Program Code:
#include<stdio.h>
#include<math.h>
void main()
{

int i = 2, n, s = 1, x, pwr = 1, dr;
float nr = 1, x1, sum;
printf("\n\n\t enter the angle...: ");
scanf("%d", &x);
x1 = 3.142* (x /180.0);
sum = x1;
printf("\n\t enter the number of terms...: ");
scanf("%d", &n);
While ( i <= n )
{
pwr = pwr + 2;
dr = dr * pwr * (pwr - 1);
sum = sum + (nr / dr) * s;
s = s * (-1);
nr = nr * x1 * x1; i+= 2;
}
printf("\n\t the sum of the sine series is..: %0.3f",sum);
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a C program to find the roots of a quadratic equation.

Program Code:
#include<stdio.h>
#include<math.h>

void main()
{
float a,b,c,r1,r2,d;
printf("enter the values of equation:");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
printf("\n enter non zero values");
else
{
d=b*b-4*a*c;
if(d>=0)
{
r1=((-b+sqrt(d))/(2*a));
r2=((-b-sqrt(d))/(2*a));
printf("\n the roots are=\t%f\t%f",r1,r2);
}
else
printf("\n roots are imaginary");
}
}
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made the profit or incurred loss. Write a C program to determine how much profit or loss incurred in percentage.

Program Code:
#include<stdio.h>
void main()
{
float cp,sp,p,l,pp,lp;
printf("Enter the costprice and selling prices of an article: ");
scanf("%d%d",&cp,&sp);
if(cp > sp)
{
l=cp-sp;

lp=(l/cp)*100;
printf("The seller had a loss and loss precentage=%f",lp);
}
else if(sp > cp)
{
p=sp-cp;
pp=(p/cp)*100;
printf("The seller had a profit and profit percentage=%f",pp);
}
else
printf("The seller had no profit no loss");
}