0% found this document useful (0 votes)
37 views

Program

The document contains programs written in C language for various basic mathematical and logical problems like addition of two numbers, swapping of two numbers, temperature conversion, finding roots of quadratic equation, area and circumference of circle, checking leap year, odd-even, calculator using switch case, finding sum and factorial of digits, checking Armstrong, palindrome and prime numbers, Fibonacci series, sorting, searching, matrix addition.

Uploaded by

Buddz Wiser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Program

The document contains programs written in C language for various basic mathematical and logical problems like addition of two numbers, swapping of two numbers, temperature conversion, finding roots of quadratic equation, area and circumference of circle, checking leap year, odd-even, calculator using switch case, finding sum and factorial of digits, checking Armstrong, palindrome and prime numbers, Fibonacci series, sorting, searching, matrix addition.

Uploaded by

Buddz Wiser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

4(a) ADDITION OF TWO NUMBERS

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c:
printf(“ enter the elements a and b”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“sum of the elements is%d”,c);
getch();
}

Output:

Enter the elements a and b: 4 5


Sum of the elements is 9
4(b) SWAPING OF TWO NUMBERS BY USING THIRD VARIABLE
Program

#include< stdio.h>
#include< conio.h>
void main()
{
int i,j;
clrscr();
printf("Enter the i & j value=\n ");
scanf("%d%d",&i,&j);
printf(“before swap”);
Printf(“\n%d\n%d\n”,i,j);
printf(“after swap”);
i=i+j;
j=i-j;
i=i-j;
Printf(“\n%d\n%d\n”,i,j);
getch();
}

Output

Enter the i & j value=3 4


before swap
i=3
j=4
after swap
i=4
j=3
4(c) TEMPERATURE CONVERSION

Program
#include <stdio.h>
#include<conio.h>
void main()
{
float celc, faren;
clrscr();
printf("Enter temperature in celsius ");
scanf("%f",&celc);
faren= (1.8 * celc) +32;
printf("\n Temperature in Farenheit = %f", faren);
getch();
}

Output

Enter temperature in Celsius : 20


Temperature in Fahrenheit = 68.0
4(d) QUADRATIC FORM

Program:

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int a, b, c, d;
float r1, r2;
clrscr();
printf("Enter co-efficient for x^2 : ");
scanf("%d", &a);
printf("Enter co-efficient for x : ");
scanf("%d", &b);
printf("Enter const co-efficient : ");
scanf("%d", &c);
d = b*b - 4*a*c;
r1 = (-b + sqrt(d)) / (2.0 * a);
r2 = (-b - sqrt(d)) / (2.0 * a);
printf("Real roots are %.2f and %.2f", r1, r2);
getch();
}

Output:

Enter co-efficient for x^2 : 2


Enter co-efficient for x : 8
Enter const co-efficient : 6
Real roots are -1.00 and -3.00
4(e) FINDING THE AREA, CIRCUMFERENCE OF CIRCLE

Program:

#include <stdio.h>
#include <conio.h>
#define pi 3.14
main()
{
int radius;
float area, circum;
clrscr();
printf("Enter radius of the circle : ");
scanf("%d", &radius);
area = pi * radius * radius;
circum = 2 * pi * radius;
printf("Area is %.2f and circumference is %.2f",area,circum);
getch();
}

Output:

Enter radius of the circle : 8


Area is 200.96 and circumference is 50.24
4(f) LEAP YEAR OR NOT

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter the year\n");
scanf("%d",&year);
if(year%4==0&&year%100!=0||year%400==0)
{
printf("it is leap year");
}
else
printf("it is not a leap year");
getch();
}

OUTPUT:

Enter the year : 2013


It is not a leap year
4(g) ODD OR EVEN

Program

#include<stdio.h>
void main()
{
int num;
printf("Enter the number...");
scanf("%d",&num);
if(num%2==0)
printf("The given number is a Even number");
else
printf("The given number is a Odd number");
}

Output

Enter the number…5

The given number is a Odd number


4(h) GREATEST OF TWO NUMBERS BY USING TERNARY

Program:

#include <stdio.h>
#include <conio.h>
void main()
{
int x,y,big;
clrscr();
printf("Enter the value of x,y : ");
scanf("%d%d", &x &y);
big=(x>y)?x:y;
printf("big is ….%d”,big);
getch();
}

Output:

Enter the value of x,y: 5 7


Big is …..7
5(a) GREATEST OF TWO NUMBERS BY USING IF STATEMENT

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the value of a and b\n");
scanf("%d%d",&a,&b);
if(a>b)
printf("%d is big",a);
else
printf("%d is big",b);
getch();
}

Output:

enter the value of a and b


5
8
8 is big
5(b) GREATEST AMONG THREE NUMBERS USING NESTED IF
STATEMENT

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the value of a,b,c \n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("max=a");
}
else
{
printf("max=c");
}}
else if(b>c)
{
printf("max=b");
}
else
{
printf("max=c");
}
getch();
}

OUTPUT:

enter the value of a,b,c


5
3
4
max=a
5(C) CALCULATOR USING SWITCH CASE

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n;
clrscr();
printf("enter values for 'a'and 'b'\n");
scanf("%d%d",&a,&b);
printf("enter the choice \n");
scanf("%d",&n);
switch(n)
{
case 1:
c=a+b;
printf("the value of c is %d",c);
break;
case 2:
c=a-b;
printf("the value of c is %d",c);
break;
case 3:
c=a*b;
printf("the value of c is %d",c);
break;
case 4:
c=a/b;
printf("the value of c is %d",c);
break;
default:
printf("wrong choice !!");
}
getch();
}

OUTPUT:

enter values for 'a'and 'b'


5
9
enter the choice
3
the value of c is 45
5(d) FIND THE SUM OF DIGITS

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0;
clrscr();
printf("Enter no:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("\nSum of digits = %d",s);
getch();
}

OUTPUT:

Enter no: 3425


Sum of digits=14
5(e) FINDING WHETHER A GIVEN NUMBER IS ARMSTRONG OR NOT

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,n,sum=0,c;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
a=n;
while(n>0)
{
c=n%10;
c=c*c*c;
sum=sum+c;
n=n/10;
}
if(sum==a)
printf("%d is armstrong number",sum);
else
printf("%d is not an armstrong number",sum);
getch();
}

OUTPUT:

enter the number


153
153 is armstrong number
5(f) FINDING PALINDROME OR NOT

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,a,rev=0;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
temp=n;
while(n>0)
{
a=n%10;
rev=rev*10;
rev=rev+a;
n=n/10;
}
if (temp==rev)
{
printf("it is palindrome");
}
else
{
printf("it is not palindrome");
}
getch();
}

OUTPUT:

enter the number


121
it is palindrome
5(g) PRIME NUMBER

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,flg=0;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if (n%i==0)
{
flg=1;
break();
}
}
if (flg==0)
printf(“the given number is prime”);
else
printf(“the given number is not prime”);
getch();
}

Output:

Enter the number: 7


The given number is prime.
5(h) FIBONACCI SERIES

Program

#include<stdio.h>
void main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");


scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}

Output:

Enter the number of terms: 5


First 5 terms of Fibonacci series are 0 1 1 2 3
5(i) FACTORIAL

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, fact;
i=1;
fact=1;
scanf(“%d”, &n);
printf(“\n enter the number”);
while(i<=n)
{
fact=fact*i;
i=i+1;
}
printf(“Factorial of n is %d”, fact):
getch();
}

Output:

Enter the number: 3


Factorial of n is 6
5(j) THE NUMBER DIVISIBLE BY 2BUT NOT DIVISIBLE BY
3 AND 5 UPTO 100

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b=0
clrscr();
for(a=0;a<100;a++)
if(a%2==0&&a%3!=0&&a%5!=0)
{
printf(“%d”,a);
b++;
}
printf(“no of counts %d”,b);
getch();
}

Output:

2 4 8 14 16 22 26 28 32 34 38 44
46 52 56 58 62 64 68 74 76 82 86 88
92 94 98

No of counts: 27
6(a) SORTING

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,temp;
clrscr();
printf("enter the limit \n");
scanf("%d",&n);
printf("enter the array element \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("the sorted element n is ");
for (i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}
OUTPUT:

enter the limit


5
enter the array element
9
6
4
10
5
the sorted element n is
4
5
6
9
10
6(b) SEARCHING

Program:

#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], search, c, n;
clrscr();
printf("Enter the number of elements in array\n");
scanf("%d",&n);

printf("Enter %d integer(s)\n", n);


for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter the number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
getch();
}
OUTPUT:

enter no.of. elements


5
enter the elements
9
5
6
7
10
enter the element to be searched
5
the element 5 is in position 2
6(c) MATRIX ADDITION

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j;
clrscr();
printf("enter the row & column size of matrix a \n");
scanf("%d%d",&m,&n);
printf("enter the row & column size of matrix b\n");
scanf("%d%d",&p,&q);
if (m==p&&n==q)
{
printf("enter the elements of matrix a \n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}}
printf("enter the elements of matrix b \n");
for (i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf ("%d",&b[i][j]);
}}

for (i=0;i<m;i++)
{

for (j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}}
printf("the added matrix is");
for (i=0;i<m;i++)
{
printf("\n");
for (j=0;j<n;j++)
{
printf("%3d",c[i][j]);
}}}
else
printf("the row & column size is invalid");
getch();
}

OUTPUT:

enter the row & column size of matrix a


22
enter the row & column size of matrix b
22
enter the elements of matrix a
12
34
enter the elements of matrix b
23
45
the added matrix is
3 5
7 9
6(d) MATRIX MULTIPLICATION

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
clrscr();
printf("enter the row & column size of matrix a \n");
scanf("%d%d",&m,&n);
printf("enter the row & column size of matrix b \n");
scanf("%d%d",&p,&q);
if (n==p)
{
printf("enter the elements of matrix a \n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}}
printf("enter the elements of matrix b \n");
for (i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf ("%d",&b[i][j]);
}}

for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
c[i][j]=0;
for (k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}}}
printf("the multiplied matrix is");
for (i=0;i<m;i++)
{
printf("\n");
for (j=0;j<n;j++)
{
printf("%3d",c[i][j]);
}}}
else
{
printf("not possible to multiplication");
}
getch();
}

OUTPUT:

enter the row & column size of matrix a


22
enter the row & column size of matrix b
22
enter the elements of matrix a
12
23
enter the elements of matrix b
23
12
the multiplied matrix is
6 8
8 10
6(e) MATRIX TRANSPOSE

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],m,n,i,j;
clrscr();
printf("enter the row & column size of matrix a \n");
scanf("%d%d",&m,&n);
printf("enter the elements of matrix a \n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
b[i][j]=a[j][i];
}}
printf("the matrix b is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%3d",b[i][j]);
}

}
getch();
}
OUTPUT:

enter the row & column size of matrix a


22
enter the elements of matrix a
23
45
The matrix b is
24
35
7(a) STRING CONCATENATION

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[10],str2[10],str3[10];
int i,j;
clrscr();
printf("Enter the two string:");
scanf("%s%s",&str1,&str2);
for(i=0;str1[i]!='\0';i++)
{
str3[i]=str1[i];
}
for(j=0;str2[j]!='\0';j++)
{
str3[i]=str2[j];
i++;
}
str3[i]='\0';
printf("Concatenated Stringis.....%s\n",str3);
getch();
}

OUTPUT:

Enter thetwo string:


Anna
University

Concatenated stringis……. AnnaUniversity


7(b)STRING PALINDROME USING STRING FUNCTION

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[40],str2[40];
int n;
clrscr();
printf("enter the string 1 \n");
gets (str1);
strcpy(str2,str1);
strrev(str1);
n=strcmp(str1,str2);
if(n==0)
{
printf("the given string is palindrome");
}
else
printf("the given string is not palindrome");
getch();
}

OUTPUT:

enter the string 1


MALAYALAM
The given string is palindrome
8(a) SWAPING OF TWO NUMBERS USING CALL BY VALUE

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}

Output:

enter the value of a & b


5
9
After swapping the value for a & b is 9 5
8(b) SWAPING OF TWO NUMBERS USING CALL BY REFERENCE

Program:

#include<stdio.h>
#include<conio.h>
void swap(int *num1, int *num2);
void main()
{
int x, y;
printf("\nEnter First number : ");
scanf("%d", &x);
printf("\nEnter Second number : ");
scanf("%d", &y);
printf("\nBefore Swaping x = %d and y = %d", x, y);
swap(&x, &y);
printf("\nAfter Swaping x = %d and y = %d", x, y);
getch();
}

void swap(int *num1, int *num2)


{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}

Output:

Enter First number :20


Enter Second number:10
Before Swaping x = 20 and y = 10
After Swaping x = 10 and y = 20
9(a) FACTORIAL USING RECURSION

Program:

#include<stdio.h>
#include<conio.h>
int recursive(int);
void main ()
{
int a,fact;
clrscr();
printf("Enter the number:");
scanf("%u",&a);
fact=recursive(a);
printf("The factorial of%d is %d",a,fact);
getch();
}
int recursive(x)
int x;
{
int f;
if(x==0)
return(1);
else
f=x*recursive(x-1);
return(f);
}

OUTPUT:

Enter thenumber5
The factorial of 5 is 120
10(a) EMPLOYEMENT PAYROLL USING STRUCTURE

Program:
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[15];
int empid;
float bsal;
float net;
float gross;
};
void main()
{
struct employee emp;
float hra,da,tax;
clrscr();
printf("\nEmployee Details");
printf("\nEnter the employee name");
scanf("%s",&emp.name);
printf("\nEnter the employee id");
scanf("%d",&emp.empid);
printf("\nEnter the basic salary");
scanf("%f",&emp.bsal);
hra=((10*emp.bsal)/100);
da=((35*emp.bsal)/100);
tax=((15*emp.bsal)/100);
emp.gross=emp.bsal+hra+da;
emp.net=emp.gross-tax;
printf("\nEmployee name:%s",emp.name);
printf("\nEmployee no:%d",emp.empid);
printf("\nEmployee Basic salary:%f",emp.bsal);
printf("\nHRA:%f",hra);
printf("\nDA:%f",da);
printf("\nTax:%f",tax);
printf("\nNetSalary%f",emp.net);
printf("\nGross salary:%f",emp.gross);
getch();
}
OUTPUT:

Employee Details:
Enter the employee name : Robin
Enter the employee Id : 100
Enter the basic salary : 30000

Employee name : Robin


Employee Id : 100
Employee Basic salary : 30000.000000
HRA : 3000.000000
DA : 10,500.000000
Tax : 4500.000000
Gross salary : 39000.000000
10(b) STUDENT MARKLIST USING UNION

PROGRAM:
#include<stdio.h>
main()
{
union student
{
char name[20];
char regno[12];
int avg;
char grade;
} stud[25],*ptr;
int i,no;
printf(“Enter the number of the students...”);
scanf(“%d”,&no);
for(i=0;i<no;i++)
{
printf(“\n student[%d] information:\n”,i+1);
printf(“Enter the name”);
scanf(“%s”,stud[i].name);
printf(“\nEnter the roll no of the student”);
scanf(“%s”,stud[i].regno);
printf(“\nEnter the average value of the student”);
scanf(“%d”,&stud[i].avg);
}
pt=stud;
for(pt=stud;pt<stud+no;ptr++)
{
if(ptr->avg<30)
ptr->grade=’D’;
else if(ptr->avg<50)
ptr->grade=’C’;
else if(ptr->avg<70)
ptr->grade=’B’;
else
ptr->grade=’A’;
}
printf(“\n”);
printf(“NAME REGISTER-NO AVERAGE GRADE\n”);
for(ptr=stud;ptr<stud+no;pt++)
{
printf(“%-20s%-10s”,ptr->name,ptr->regno);
printf(“%10d \t %c\n”,ptr->avg,ptr->grade);
}
}
OUTPUT:

Enter the number of the students


3
student[1] information:
Enter the name Jack
Enter the roll no of the student 31705205001
Enter the average value of the student 90
student[2] information:
Enter the name Raj
Enter the roll no of the student 31705205002
Enter the average value of the student 88
student[3] information:
Enter the name Kiran
Enter the roll no of the student 31705205003
Enter the average value of the student 75

NAME REGISTER-NO AVERAGE GRADE


Jack 31705205001 90 S
Raj 31705205002 88 A
Kiran 31705205003 75 B

You might also like