0% found this document useful (0 votes)
19 views33 pages

C Programs for Basic Operations

The document contains a series of C programming examples demonstrating various functionalities, including calculating the sum and average of numbers, checking for odd/even, finding the largest number, and performing arithmetic operations using switch statements. It also includes programs for generating Fibonacci series, finding factorials, checking for palindromes, and performing matrix operations. Each program is accompanied by its output, showcasing the expected results for given inputs.

Uploaded by

zenogc176
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)
19 views33 pages

C Programs for Basic Operations

The document contains a series of C programming examples demonstrating various functionalities, including calculating the sum and average of numbers, checking for odd/even, finding the largest number, and performing arithmetic operations using switch statements. It also includes programs for generating Fibonacci series, finding factorials, checking for palindromes, and performing matrix operations. Each program is accompanied by its output, showcasing the expected results for given inputs.

Uploaded by

zenogc176
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

1. Program to find sum and average of three numbers.

Program

#include<stdio.h>
int main()
{
int a,b,c,sum;
float avg;
printf("Enter 3 numbers \n");
scanf("%d%d%d",&a,&b,&c);
sum = a+b+c;
avg =sum/3;
printf("Sum = %d \n",sum);
printf("Average=%f",avg);
return 0;
}

Output

Enter 3 numbers
2
3
1
Sum=6
Average=2.000000

1
2. Program to print the size of all fundamental data types.

Program

#include<stdio.h>
int main()
{
int a=sizeof(a);
printf(“int = %d\n”,a);
char b=sizeof(b);
printf(“char = %d \n”,b);
float c=sizeof(c);
printf(“float = %f\n”,c);
double d=sizeof(d);
printf(“double = %lf\n”,d);
return 0;
}

Output
int = 4
char = 1
float = 4.000000
double = 8.000000

2
3. Program to find largest among three numbers using conditional –
operator.

Program

#include<stdio.h>
int main()
{
int a,b,c,max;
printf("Enter three numbers:\n");
scanf("%d%d%d", &a,&b,&c);
max=a>b?(a>c?a:c):(b>c?b:c);
printf("%d is the largest ", max);
}

Output

Enter three numbers:


21
11
56
56 is the largest

3
4. Program to check odd or even using if statement

Program

#include<stdio.h>
int main()
{
int a;
printf("Enter a number\n");
scanf("%d", &a);
if(a%2==0)
{
printf("Entered number is even");
}
else
{
printf("Entered number is odd");
}
return0;
}

Output

Enter a number
25
Entered number is odd

4
5. Program to print the grade of a student using if

Program

#include<stdio.h>
int main()
{
printf(“Enter your marks out of 100 \n”);
scanf(“%d”,&m);
if(m>80)
{
printf(“The grade is A”);
}
else if(60<=m && m<=80)
{
printf(“The grade is B”);
}
else if(40<=m && m<=59)
{
printf(“The grade is C”);
}
else if(20<=m && m<=39)
{
printf(“The grade is D”);
}
else if(m<20)
{
printf(“You are fail”);
}
else
{
printf(“Invalid mark”);
}
return 0;
}

5
Output

Enter your marks out of 100

80

The grade is B

6
6. Program to perform arithmetic operation using switch statement.

Program

#include<stdio.h>
int main()
{
int a,b,c;
printf("1)Addition\n 2)Subtraction\n 3)Multiplication\n 4) Division \n");
printf("Enter two numbers\n");
scanf("%d%d", &a, &b);
printf("Enter your choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("The sum of %d and %d is %d",a,b,a+b);
break;
case 2:
printf("The difference of %d and %d is %d",a,b,a-b);
break;
case 3:
printf("The product of %d and %d is %d",a,b,a*b);
break;
case 4:
printf("The division of %d and %d is %d",a,b,a/b);
break;
default:
printf("invalidoutput");
}
return0;
}

Output1

1) Addition
2) Subtraction
3) Multiplication
4) Division
7
Enter two numbers
2
7
Enter your choice
1
The sum of 2 and 7 is 9

Output2

1) Addition
2) Subtraction
3) Multiplication
4) Division
Enter two numbers
9
5
Enter your choice
2
The difference of 9 and 5 is 4

Output3

1) Addition
2) Subtraction
3) Multiplication
4) Division
Enter two numbers
2
7
Enter your choice
3
The sum of 2 and 7 is 14

Output4

1) Addition
2) Subtraction
3) Multiplication
4) Division
Enter two numbers
8
4
Enter your choice
4
The sum of 8 and 4 is 2

8
7. Program to find the roots of a quadratic equation.

Program

#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,d,r1,r2;
printf("Enter three coeffiecents\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("Not a quadratic equation");
}
else
{
d=b*b-4*a*c;
if(d==0)
{
r1=r2=-b/(2*a);
printf("The roots are equal %f",r1);
}
else if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Roots are equal and distinct \n r1=%f\n r2=%f",r1,r2);
}
else
{
printf("Roots are imaginary");
}
}
return0;
}

9
Output

Enter three coeffiecents


1
5
4
Roots are eqaual and distinct
r1 =-1.000000
r2 =-4.000000

10
8. Program to find factorial of given number.

Program

#include<stdio.h>
int main()
{
int i, fact = 1, number;
printf("Enter a number\n");
scanf("%d", &number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",number, fact);
return0;
}

Output

Enter a number
6
Factorial of 6 is: 720

11
9. Program to generate the Fibonacci series.

Program

#include<stdio.h>
int main()
{
int n,a=0,b=1, c=0;
printf(“Enter a number\n”);
scanf(“%d”,&n);
printf(“0”);
while(n>=c)
{
a=b;
b=c;
c=a+b;
printf(“Fibonacci series : %d”,c);
}
}

Output

Enter a number
4
Fibonacci series : 011235

12
10. Program to find sum of n numbers using array.

Program

#include<stdio.h>
int main()
{
int i,n,sum=0,array[100];
printf("Enter the number of integers you want to add:");
scanf("%d",&n);
printf("\n\nEnter %d integers\n\n",n);
for (i=0;i<n;i++)
{
scanf("%d",&array[i]);
sum +=array[i];
}
printf("\n\nSum=%d\n\n",sum);
return 0;
}

Output

Enter the number of integers you want to add : 3


Enter 3 integers
1
2
3
Sum=6

13
11. Program to sort n numbers using array.

Program

#include<stdio.h>
void main()
{
int i,j,a,n,number[30];
printf("Enter the value of N\n");
scanf("%d",&n);
printf("Enter the number\n");
for(i=0;i<n;++i)
scanf("%d",&number[i]);
for(i=0;i<n;++i)
{
for(j=i+1;j<n;++j)
{
if(number[i]>number[j])
{
a=number[i];
number[i]=number[j];
number[j]=a;
}
}
}
printf("The number arranged in ascending order are given below\n");
for(i=0;i<n;++i)
{
printf("%d\n",number[i]);
}
}

14
Output

Enter the value of N 5


Enter the number
2
45
23
7
3

The number arranged in ascending order are given below


2
3
7
23
45

15
12. program to check the given string is palindrome or not.

Program

#include<stdio.h>
#include<string.h>
void main()
{
char string[20];
int i,length;
int flag=0;
printf("Enter text: ");
scanf("%s",&string);
length=strlen(string);
for (i=0;i<length;i++)
{
if(string[i]!=string[length-i-1])
{

flag=1;

break;
}
}
if(flag)
{
printf("%s is not palidrome",string);
}
else
{
printf("%s is palidrome",string);
}
}
Output1

Enter text: malayalam


Malayalam is palidrome

16
Output2

Enter text: Computer


Computer is not palidrome

17
13. Program to generate prime numbers with in a range

Program

#include<stdio.h>
void main()
{
int n1,n2,flag,i,j;
printf("Enter the range: \n");
scanf("%d%d", &n1,&n2);
printf("Prime numbers are:\n");
for(i=n1+1;i<n2;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0) printf("%d ",i);
}
}

Output

Enter the range:


2
30
Prime numbers are:
3 5 7 11 13 17 19 23 29

18
14. Program to implement any five built-in string function.

Program

#include<stdio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int n,r,ch;
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("\nEnter a choice: \n");
scanf("%d",&ch);
if(ch==1)
{
printf("\nEnter a string:\n");
scanf("%s",&str1);
n =strlen(str1);
printf("\nLength of string is %d",n);
}
else if(ch==2)
{
printf("\nEnter a string:");
scanf("%s",&str1);
strcpy(str2,str1);
printf("\nCopied string is %s",str2);
}
else if(ch==3)
{
printf("\nEnter two strings:\n");
scanf("%s %s",&str1,&str2);
strcat(str1,str2);
printf("\nConcantented string is %s",str1);
}
else if(ch==4)
{
printf("\nEnter 2 strings: ");
19
scanf("%s%s",&str1,&str2);
r=strcmp(str1,str2);
if(r==0)
{
printf("\nString is equal");
}
else if(r>0)
{
printf("%s > %s",str1,str2);
}
else
{
printf("\n%s is less than %s",str1,str2);
}
}
}

Output1

[Link]
[Link]
[Link]
[Link]
Enter a choice:
1
Enter a string:
AASHIL
Length of string is 6

Output2

[Link]
[Link]
[Link]
[Link]
20
Enter a choice:
2
Enter a string:
Hello
Copied string is Hello

Output3

[Link]
[Link]
[Link]
[Link]
Enter a choice:
3
Enter two strings:
Hai
Goodmorning
Concatenated string is HaiGoodmorning

Output4

[Link]
[Link]
[Link]
[Link]
Enter a choice:
4
Enter 2 strings:
Computer
Computer
String is equal

21
15. Program to perform any matrix operation.

Program

#include<stdio.h>
int main()
{
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
printf("Enter the number of rows (between1to100):");
scanf("%d",&r);
printf("Enter the number of coloumns (betweeen1to 100): ");
scanf("%d",&c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d:",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of 2nd matrix:\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element b%d%d:",i+1,j+1);
scanf("%d",&b[i][j]);
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
printf("\nSum of two matrices\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);

22
if(j==c-1)
{
printf("\n\n");
}
}
return0;
}

Output

Enter the number of rows(between1to100): 2


Enter the number of coloumns(betweeen1to100): 3

Enter elements of 1stmatrix:

Enter element a11: 10


Enter element a12:11
Enter element a13:12
Enter element a21:13
Enter element a22:14
Enter element a23:15

Enter elements of 2nd matrix:

Enter element b11: 21


Enter element b12:22
Enter element b13:23
Enter element b21:24
Enter element b22:25
Enter element b23:26

Sum of two matrices


31 33 35
37 39 41

23
24
25
26
27
28
29
30
31
32
33

You might also like