0% found this document useful (0 votes)
11 views13 pages

Kunj Bhatt

5 Documents

Uploaded by

kunjbhatt3011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Kunj Bhatt

5 Documents

Uploaded by

kunjbhatt3011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Kunj Bhatt

22BME075

Q8. Program to calculate string length, reverse the string, etc.


INPUT CODE :-
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void main()
{
int num,i,j;
int len=0;
printf("The size of the string is-");
scanf("%d",&num);
char str[num];
char x[num];
printf("Enter the string -");
scanf("%s",&str);
len = strlen(str);
printf("The length of the string is %d by using len function\n",len);
for(i=0,j=len-1;i<len,j>=0;i++,j--)
{
x[j]=str[i];
}
printf("The reverse string is - ");
for(int k=0;k<len;k++)
{
printf("%c",x[k]);
}
}

OUTPUT SCREEN :-
Kunj Bhatt
22BME075

Q9. Program to check the string and number is palindrome or


not.
INPUT CODE :-
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void main()
{
int num,i,j;
int x=0;
int len=0;
printf("The size of the string is-");
scanf("%d",&num);
char str[num];
char rev[num];
printf("Enter the string -");
scanf("%s",&str);
len = strlen(str);
printf("The length of the string is %d by using len function\n",len);
for(i=0,j=len-1;i<len,j>=0;i++,j--)
{
rev[j]=str[i];
}
printf("The reverse string is - ");
for(int k=0;k<len;k++)
{
printf("%c",rev[k]);
}
printf("\n");
for(int l=0;l<len;l++)
{
if(rev[l]==str[l])
{
x=x+1;
}
}
if(x==len)
{
printf("Palindrome String");
}
else
{
printf("Not a Palindrome");
}
Kunj Bhatt
22BME075
}

OUTPUT SCREEN :-

Q10. Program to generate sine, cosine, tan series.


Kunj Bhatt
22BME075

INPUT CODE :-
#include<stdio.h>
#include<math.h>
int factorial(int num)
{
int ctr;
int fact=1;//This function calculates and returns the factorial of number num
for(ctr=1;ctr<=num;ctr++)
fact=fact*ctr;
return fact;
}
int main()
{
int terms;
int ctr;
printf("Enter the number of terms till which you want to calculate the value
of expansion\n");
scanf("%d",&terms);//Enter the number of terms
double sum=0.0;
double x;
printf("Enter the value for x\n");
scanf("%lf",&x);
printf("1+x+x^2/2!+x^3/3!.......This series when expanded till infinity is
expansion for exp(x)\n");
for(ctr=0;ctr<terms;ctr++)
sum+=pow(x,ctr)/factorial(ctr);//finding the value for sum appropriately
printf("The value from the expansion is %.14lf\n",sum);
sum=0.0;
printf("1-x^2/2!+x^4/4!.......This series when expanded till infinity is
expansion for cosine(x)\n");
for(ctr=0;ctr/2<terms;ctr+=2)
sum=sum+pow(-1,ctr/2)*pow(x,ctr)/(factorial(ctr));
printf("The value from the expansion is %.14lf\n",sum);
sum=0.0;
printf("x-x^3/3!+x^5/5!.......This series when expanded till infinity is
expansion for sine(x)\n");
for(ctr=1;(ctr-1)/2<terms;ctr+=2)
sum=sum+pow(-1,(ctr-1)/2)*pow(x,ctr)/(factorial(ctr));
printf("The value from the expansion is %.14lf\n",sum);
printf("x+x^3/3+2x^5/15.......This series when expanded till infinity is
expansion for tan(x)\n");
sum=0.0;
double B,temp;
int Bn,k,r;
for(ctr=1;ctr<=terms;ctr+=1)
{//This loops here calculate Bernoulli number which is further used to get
the coefficient in expansion of tan x
Kunj Bhatt
22BME075
B=0;
Bn=2*ctr;
for(k=0;k<=Bn;k++)
{
temp=0;
for(r=0;r<=k;r++)
{
temp=temp+pow(
1,r)*factorial(k)*pow(r,Bn)/(factorial(r)*factorial(k-r));
}
B=B+temp/((double)(k+1));
}
sum=sum+pow(-4,ctr)*(1-pow(4,ctr))*B*pow(x,2*ctr-1)/factorial(2*ctr);
}
printf("The value from the expansion is %.14lf\n",sum);
}

OUTPUT SCREEN :-

Q11. Program to generate Fibonacci series.


INPUT CODE :-
#include<stdio.h>
#include<conio.h>
void main()
Kunj Bhatt
22BME075
{
int a,b,sum,n,i;
a=0;
b=1;
printf("Enter the number till you want - ");
scanf("%d",&n);
printf("The Fibonacci Series Is\n");
printf("0\n");
printf("1\n");
for(i=1;i<(n-1);i++)
{
sum=a+b;
printf("%d\n",sum);
a=b;
b=sum;
}
}

OUTPUT SCREEN :-

Q12. Program to calculate factorial using recursion.


INPUT CODE :-
#include<stdio.h>
#include<stdlib.h>
Kunj Bhatt
22BME075
int factorial(int n)
{
if(n==0)
{
return 1;
}
return n*factorial(n-1);
}
int main()
{
int x;
int f;
printf("Enter the value of n- ");
scanf("%d",&x);
f=factorial(x);
printf("The factorial is %d",f);
}

OUTPUT SCREEN :-

Q13. Program to create a database using array of structures.


INPUT CODE :-
#include <stdio.h>

// Define the RoomCondition structure


struct RoomCondition {
int humidity;
int temperature;
int AQI;
Kunj Bhatt
22BME075
};

int main(void) {
// Declare an array of RoomCondition structures to store the readings
struct RoomCondition readings[5];

// Get input for each reading


for (int i = 0; i < 5; i++) {
printf("Enter humidity, temperature, and AQI for reading %d: ", i+1);
scanf("%d %d %d", &readings[i].humidity, &readings[i].temperature,
&readings[i].AQI);
}

// Calculate the sums of the members for all readings


int humidity_sum = 0;
int temperature_sum = 0;
int AQI_sum = 0;
for (int i = 0; i < 5; i++) {
humidity_sum += readings[i].humidity;
temperature_sum += readings[i].temperature;
AQI_sum += readings[i].AQI;
}

// Calculate the average room condition


struct RoomCondition average;
average.humidity = humidity_sum / 5;
average.temperature = temperature_sum / 5;
average.AQI = AQI_sum / 5;

// Display the average room condition


printf("Average room condition: humidity = %d, temperature = %d, AQI = %d\n",
average.humidity, average.temperature, average.AQI);

return 0;
}

OUTPUT SCREEN :-
Kunj Bhatt
22BME075

Q14. Program to find intersection and union of two 1D array.


INPUT CODE :-
#include<stdio.h>
void main()
Kunj Bhatt
22BME075
{
int n,m,sum;
int o=0;
printf("Enter the size of the array A");
scanf("%d", &n);
printf("Enter the size of the array B");
scanf("%d",&m);
sum=n+m;
int A [n];
int B [m];
int U[sum];
int i,j,k,l,r,s,x,y,z,w;
printf("\nWrite the elements of array A:");
for(i =0; i<n; i++)
{
scanf("%d",&A[i]);//INPUT
printf("A(%d) = %d, ", i, A[i] );//OUTPUT
}
printf("\nWrite the elements of array B:");
for(j =0; j<m; j++)
{
scanf("%d",&B[j]);//INPUT
printf("B(%d) = %d, ", j, B[j] );//OUTPUT
}
printf("\nThe Commom point are -\n");
for(k=0;k<n;k++)
{
for(l=0;l<m;l++)
{
if(A[k]==B[l])
{
printf("%d\n",A[k]);
}
}
}
printf("\nThe union points are\n");
for(r=0;r<n;r++)
{
U[o]=A[r];
o = o+1;
}
for(s=0;s<m;s++)
{
U[o]=B[s];
o=o+1;
}
for(x=0;x<sum;x++)
{
Kunj Bhatt
22BME075
for(y=x+1;y<sum;)
{
if(U[x]==U[y])
{
for(z=y;z<sum;z++)
{
U[z]=U[z+1];
}
sum=sum-1;
}
else
{
y=y+1;
}
}
}
for(w=0;w<sum;w++)
{
printf("U(%d) = %d, ", w, U[w] );//OUTPUT
}
}

OUTPUT SCREEN :-
Kunj Bhatt
22BME075

Q15. Programs related to file.


INPUT CODE :-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*writing using fputs*/
void main()
{
FILE *fp;
char ch[100];
fp=fopen("sample.txt","w");
if(fp==NULL)
{
printf("Error");
exit(1);
}
fputs("Hello Hi",fp);
fclose(fp);
fp=fopen("sample.txt","r");
Kunj Bhatt
22BME075
if(fp==NULL)
{
printf("Error");
exit(1);
}
while(fgets(ch,50,fp)!=NULL)
{
printf("%s",ch);
}
fclose(fp);
}

OUTPUT SCREEN :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy