0% found this document useful (0 votes)
59 views11 pages

C Programs for Basic Calculations and Operations

The document contains multiple C programs that perform various tasks such as calculating areas of geometric shapes, generating Fibonacci series, searching numbers in an array, manipulating strings, and performing mathematical operations. Each program is structured with user input and output, demonstrating fundamental programming concepts like loops, conditionals, and functions. Overall, it serves as a collection of simple algorithms and exercises for learning C programming.

Uploaded by

chaitanya.31411
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)
59 views11 pages

C Programs for Basic Calculations and Operations

The document contains multiple C programs that perform various tasks such as calculating areas of geometric shapes, generating Fibonacci series, searching numbers in an array, manipulating strings, and performing mathematical operations. Each program is structured with user input and output, demonstrating fundamental programming concepts like loops, conditionals, and functions. Overall, it serves as a collection of simple algorithms and exercises for learning C programming.

Uploaded by

chaitanya.31411
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

/* menu driven program to calculate area */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int ch;
float s,l,b,r,base,ht;
float acircle,arect,atriangle;
clrscr();
do
{
printf("\n Main Menu \n");
printf("1. Area of circle\n");
printf("2. Area of rectangle\n");
printf("3. Area of triangle\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter radius ");
scanf("%f",&r);
acircle=3.14*r*r;
printf("\n Area of circle = %f",acircle);
break;
case 2:printf("\n Enter length and breadth of rectangle ");
scanf("%f%f",&l,&b);
arect=l*b;
printf("\n Area of rectangle = %f",arect);
break;
case 3:printf("\n Enter base and height of triangle ");
scanf("%f%f",&base,&ht);
atriangle=0.5*base*ht;
printf("\n Area of triangle = %f",atriangle);
break;

case 4:exit(0);
}
}while(ch>=1 && ch<=4);
}
/* program to print Fibonacci series upto given terms */
#include<stdio.h>
#include<conio.h>
main()
{
int t1=1,t2=1,t3,i,n;
clrscr();

printf("enter the number of terms ");


scanf("%d",&n);
printf("%d\t%d\t",t1,t2);
for(i=1;i<=n-2;i++)
{

t3=t1+t2;
printf("%d\t",t3);
t1=t2;
t2=t3;
}

getch();
}

/* Write a C program to calculate x to the power y without using standard function */


#include<stdio.h>
void main()
{
int x,y;
int p=1,count=1;
printf("Enter the values of x and y ");
scanf("%d%d",&x,&y);
while(count<=y)
{
p*=x;
count++;
}
printf("%d to power %d = %d",x,y,p);
}

/*Program to search a given number in a list of numbers */


#include<stdio.h>
void main()
{
int a[5],i,sum=0,x,f=0;
printf("Enter the numbers ");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("Enter the number to be searched ");
scanf("%d",&x);

for(i=0;i<5;i++)
{
if(a[i]==x)
{
f=1;
break;
}
}
if(f==1)
printf("%d is found",x);
else
printf("%d is not found",x);

/* Program to convert a given string in uppercase and vice versa.*/


#include<stdio.h>
#include<conio.h>
main()
{
int i;
char s[20];
printf("\n enter a string");
gets(s);
for(i=0;s[i]!='\0';i++)

if(s[i]>=65&&s[i]<91)
s[i]=s[i]+32;
else
s[i]=s[i]-32;
// s[i]='\0';
puts(s);

getch();
}
/* Program to accept a string from user and display alternate character in upper case
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
main()
{
char str[15],str1[15];
int i,j,m;
clrscr();
printf("enter a string ");
gets(str);
m=strlen(str);
for(i=0;i<=m;i++)
{
if(i%2==0)

str[i]=toupper(str[i]);

}
puts(str);

getch();
}

/* Write a ‘C’ Program to accept ‘n’ numbers from user, store these numbers into an
array. Find out Maximum and Minimum number from an Array(using function).*/

#include<stdio.h>
#include<conio.h>
void maxmin(int [],int);
void main()
{
int a[5];
int n,i,j,temp;
clrscr();
printf("\n Enter size of array");
scanf("%d",&n);

printf("enter elements of array\n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);
maxmin(a,n);
}
void maxmin(int a[],int y)
{
int max,min,i;
max=a[0];
min=a[0];
for(i=0;i<y;i++)
{
if(max < a[i])
max =a[i];
}
for(i=0;i<y;i++)
{
if(min>a[i])
min=a[i];
}

printf("\n max = %d,min=%d",max,min);

getch();
}

/* Write a C program to delete all vowels from a string not more than 15 characters
*/
#include<stdio.h>
#include<conio.h>
main()
{
char s[15],str1[15];
int i=0,j=0;
clrscr();
printf("\n enter a string not more than 15 characters \n");
gets(s);
while(s[i]!='\0')
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
i++;
else
if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
i++;
else
{
str1[j]=s[i];
i++;
j++;
}
}
str1[j]='\0';
printf("\n string after removing all vowels \n");
puts(str1);
getch();
}

*Program to copy one string to another without


using standard library function*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char s[20],t[20];
clrscr();
printf("Enter the source string:\n");
gets(s);
for(i=0;s[i]!='\0';i++)
t[i]=s[i];
t[i]='\0';
printf("\nSource string s=%s",s);
printf("\nTarget string t=%s",t);
getch();
}

* program to calculate length of string without using library function */


#include<stdio.h>
#include<conio.h>
main()
{
char s[25];
int n=0;
clrscr();
printf("\n enter a string \n");
gets(s);
while(s[n]!='\0')
n++;
printf("given string is \n%s \n length = %d \n",s,n);
getch();
}

* Write a C program to print the sum of the digits of a given number */


#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0,rem;
clrscr();
printf("Enter a number");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("sum=%d",sum);

* Write a C program to print whether a given number is palindrome or not palindrome.


*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,n1,reverse=0,rem;
clrscr();

printf("Enter a number");
scanf("%d",&n);
n1=n;
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n=n/10;
}
if(reverse==n1)
printf("%d is Palindrome",n1);
else
printf("%d is not Palindrome",n1);

/* Selection sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int n,i,j,temp;
clrscr();
printf("\n Enter size of array");
scanf("%d",&n);

printf("enter elements of array\n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("elements of array in ascending order\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);

* program to arrange given list of numbers in ascending order


using bubble sort method e.g. 44,33,55,22,11*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,a[10],t,j;
clrscr();
printf("enter the number of elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
printf("\n elements of array in ascending order are \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);

/* Program to accept n different numbers and display sum of all +ve & -ve numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,x,s=0,s1=0;
printf("enter how many numbers\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the numbers\n");
scanf("%d",&x);
if(x>0)
s=s+x;
else
s1=s1+(x);
}
printf("sum of positive numbers=%d \t sum of negative numbers=%d",s,s1);
}
*Program to print sum of elements of all
elements of a mxn matrix*/

#include<stdio.h>
#include<conio.h>

main()
{
int n,m,i,j,a[10][10],sum=0;
clrscr();
printf("Enter the order of matrix:\n");
scanf("%d %d",&n,&m);

/*For inputting matrix*/


printf("Enter the matrix\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);

/*For printing the matrix*/


printf("The matrix you entered is:\n");
for(i=0;i<n;i++)
{
printf("\n\n");
for(j=0;j<m;j++)
printf("%4d",a[i][j]);
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
sum=sum+a[i][j];
printf("\nSum of elements=%d",sum);

/* Write a C program to calculate x to the power y without using standard function */


#include<stdio.h>
void main()
{
int x,y;
int p=1,count=1;
printf("Enter the values of x and y ");
scanf("%d%d",&x,&y);
while(count<=y)
{
p*=x;
count++;
}
printf("%d to power %d = %d",x,y,p);
}

You might also like