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

Computer Programming Lab Programs 1 To 38

Computers programming manual

Uploaded by

yharsha782
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)
37 views

Computer Programming Lab Programs 1 To 38

Computers programming manual

Uploaded by

yharsha782
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/ 22

1. Basic C program.

Program:
// Basic C program
#include<stdio.h> // Link section
#include<conio.h>
#define X 20 // Definition section
int sum(int y); // Global Declaration section
int main(void) // Main() Function section
{
int y = 55; // Declaration part
clrscr( );
printf("Sum: %d", sum(y)); // Execution part
getch( );
return 0;
}
int sum(int y) // Sub program section
{
return y + X;
}
Output:
Sum: 75
2. Program on Arithmetic Operators
Program:
// Arithmetic Operators (+,-,*,/,%)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,result; // variables
clrscr();
printf("Enter Two numbers:");
scanf("%d%d",&a,&b);
result=a+b;
printf("The addition of two numbers is:%d",result);
result=a-b;
printf("\nThe subtraction of two numbers is:%d",result);
result=a*b;
printf("\nThe multiplication of two numbers is:%d",result);
result=a/b;
printf("\nThe division of two numbers is :%d",result);
result=a%b;
printf("\nThe Modulus division of two numbers is :%d" ,result);
getch();
}
Output:
Enter Two numbers:10 5
The addition of two numbers is:15
The subtraction of two numbers is:5
The multiplication of two numbers is:50
The division of two numbers is :2
The Modulus division of two numbers is :0
3. Program on increment and decrement operators.
Program:
// increment and decrement operators
#include<stdio.h>
#include<conio.h>
void main()
{
int x=10;
clrscr();
//post increment
printf("After post increment ,the x value is:%d",x++);
printf("\nThe x value is:%d",x);
//pre increment
printf("\nAfter pre increment ,the x value is :%d ",++x);
//post decrement
printf("\nAfter post deecrement, the x value is:%d",x--);
// pre decrement
printf("\nThe x value is:%d",x);
printf("\nAfter pre decrement , the x value is:%d",--x);
getch();
}
Output:
After post increment ,the x value is:10
The x value is:11
After pre increment ,the x value is :12
After post decrement, the x value is:12
The x value is:11
After pre decrement , the x value is:10
4. Program on relational operators.
Program :
// Relational operators
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Relational Expression\tReturn value");
printf("\n\t\t5<2:\t\t\t%d",5<2);
printf("\n\t\t5>2:\t\t\t%d",5>2);
printf("\n\t\t5<=2:\t\t\t%d",5<=2);
printf("\n\t\t5>=2:\t\t\t%d",5>=2);
printf("\n\t\t5==2:\t\t\t%d",5==2);
printf("\n\t\t5!=2:\t\t\t%d",5!=2);
getch() ;
}
Output:
Relational Expression Return value
5<2: 0
5>2: 1
5<=2: 0
5>=2: 1
5==2: 0
5!=2: 1
5. Program on logical operators.
Program:
// logical operators
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("logical Expression\tReturn value");
// logical and operator
printf("\n(5<2)&&(5>3)\t\t\t%d",(5<2)&&(5>3));
//logical or operator
printf("\n(5<2)||<5>3)\t\t\t%d",(5<2)||(5>3));
//logical not operator
printf("\n!(5<2)\t\t\t\t\t%d",!(5<2));
getch()
}
Output:
logical Expression Return value
(5<2)&&(5>3) 0
(5<2)||<5>3) 1
!(5<2) 1
6. program on assignment operators.
Program:
// program on assignment operators
#include <stdio.h>
#include<conio.h>
void main()
{
int x=25,y=14;
x=y;
clrscr();
printf("Simple assignment:%d",x);
printf("\nAddition and assignment:%d",x+=y);
printf("\nSubtraction and assignment:%d",x-=y);
printf("\nMultiplication and assignment:%d",x*=y);
printf("\nDivision and assignment:%d",x/=y);
printf("\nModular division and assignment:%d",x%=y);
getch();
}
Output:
Simple assignment:14
Addition and assignment:28
Subtraction and assignment:14
Multiplication and assignment:196
Division and assignment:14
Modular division and assignment:0
7. Program on conditional operator.
Program:
//program on conditional operator
#include <stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Enter your age:");
scanf("%d",&age);
(age>=18)? (printf("eligible for voting")) :
(printf("not eligible for voting"));
getch();
}
Output:
Enter your age:15
not eligible for voting
8. Program on special operators.
Program:
// special operators(sizeof(),&)
#include<stdio.h>
#include<conio.h>
int main()
{
int a,a1;
char b;
float c;
double d;
clrscr();
printf("storage size of int Data type is: %d\n",sizeof(a));
printf("storage size of Char Data type is: %d\n",sizeof(b));
printf("storage size of Float Data type is: %d\n",sizeof(c));
printf("storage size of Double Data type is: %d\n",sizeof(d));
printf("The address of a is :%u",&a);
getch();
}
Output:
storage size of int Data type is: 2
storage size of Char Data type is: 1
storage size of Float Data type is: 4
storage size of Double Data type is: 8
The address of a is :165326780
9. Program on bitwise operators.
Program:
//program on bitwise operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a=12,b=25;
clrscr();
printf("a&b=%d",a&b);
printf("\na|b=%d",a|b);
printf("\na^b=%d",a^b);
printf("\na<<2=%d",a<<2);
printf("\na>>2=%d",a>>2);
printf("\n~a=%d",~a);
getch();
}
Output:
a&b=8
a|b=29
a^b=21
a<<2=48
a>>2=3
~a=-13
10. Program on sum and average of three numbers.
Algorithm:
Step 1: start
Step 2: read num1, num2, num3
Step 3: calculate sum.
Step 4: calculate average=sum/3
Step 5: print sum and average
Step 6: stop
Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3,sum;
float avg;
clrscr();
printf("Enter three numbers:");
scanf("%d%d%d",&num1,&num2,&num3);
sum=num1+num2+num3;
avg=(float)sum/3;
printf("The sum of three numbers %d,%d,%d is:%d",num1,num2,num3,sum);
printf("\nThe Average of three numbers %d,%d,%d id:%.2f",num1,num2,num3,avg);
getch();
}
Output:
Enter three numbers:10 10 9
The sum of three numbers 10,10,9 is:29
The Average of three numbers 10,10,9 id:9.67
11. Conversion of Fahrenheit to Celsius and vice versa.
Program:
#include<stdio.h>
#include<conio.h>
void main() {
float a,celsius,fahrenheit;
clrscr()
printf("Conversion of Fahrenheit to Celsius ");
printf("\n__________________________________");
printf("\nEnter The Temperature in Fahrenheit: ");
scanf("%f", & a);
celsius = 5 * (a - 32) / 9;
printf("\nCelsius Temperature is: %f ", celsius);
printf("\n\nConversion of Celsius to Fahrenheit ");
printf("\n__________________________________");
printf("\nEnter The Temperature in Celsius: ");
scanf("%f", & a);
fahrenheit = ((9 * a) / 5) + 32;
printf("\n\nFahrenheit Temperature is: %f ", fahrenheit);
getch();
}
Output:
Conversion of Fahrenheit to Celsius
__________________________________
Enter The Temperature in Fahrenheit: 45
Celsius Temperature is: 7.222222
Conversion of Celsius to Fahrenheit
__________________________________
Enter The Temperature in Celsius: 7.22
Fahrenheit Temperature is: 44.995998
12. Program that takes marks of 5 subjects in integers, and find the total, average in float.
Profram:
// program to find sum and average of five subjects
#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,tot;
float avg;
clrscr();
printf("Enter five subjects marks:");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
tot=s1+s2+s3+s4+s5;
avg=(float)tot/5;
printf("The total marks are:%d",tot);
printf("\nThe average marks are:%.2f",avg);
getch();
}
Output:
Enter five subjects marks:45 55 66 77 88
The total marks are:331
The average marks are:66.20
13.Program on Finding the maximum of three numbers using conditional operator
Program:
//Find the maximum of three numbers using conditional operator
# include <stdio.h>
#include<conio.h>
void main()
{
int a, b, c, max ;
clrscr();
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
max = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe maximum number is : %d", max) ;
getch();
}
Output:
Enter three numbers : 1 2 3
The maximum number is : 3
14. Program on simple interest calculation.
Program:
//Simple interest calculation
#include<stdio.h>
#include<conio.h>
void main()
{
long int p,r,t,int_amt;
clrscr();
printf("Input principle, Rate of interest & time to find simple interest: \n");
scanf("%ld%ld%ld",&p,&r,&t);
int_amt=(p*r*t)/100;
printf("Simple interest = %ld",int_amt);
getch();
}
Output:
Input principle, Rate of interest & time to find simple interest:
10000 10 12
Simple interest = 12000
15. Program for finding the square root of a given number.
Program:
//finding square root of a given number
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double num,root;
clrscr();
printf("Enter an Integer:");
scanf("%lf",&num);
root=sqrt(num);
printf("\nThe square root of %.2lf is %.2lf",num,root);
getch();
}
Output:
Enter an Integer:25
The square root of 25.00 is 5.00
16. Finding compound interest
Program.
// compound interest program in c
#include <stdio.h>
#include <math.h>
#include<conio.h>
void main() {
double principal_amount, rate, time_duration, amount, compound_interest;
clrscr();
printf("Enter the principal amount: ");
scanf("%lf",&principal_amount);
printf("Enter the rate of interest: ");
scanf("%lf",&rate);
printf("Enter the time duration: ");
scanf("%lf",&time_duration);
amount = principal_amount*((pow((1+rate/100),time_duration)));
compound_interest = amount - principal_amount;
printf(" The compound interest is: %.2lf",compound_interest);
getch();
}
Output:
Enter the principal amount: 1000
Enter the rate of interest: 5
Enter the time duration: 2
The compound interest is: 102.50
17. Write a program to find area of a triangle using heron’s formulae
Program:
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main(){
float s1, s2, s3, s, area;
clrscr();
printf("Enter the length of three sides of triangle\n");
scanf("%f%f%f", &s1, &s2, &s3);
s = (s1+s2+s3)/2;
area = sqrt(s*(s-s1)*(s-s2)*(s-s3));
printf("Area of triangle : %0.2f\n", area);
getch();
}
Output:
Enter the length of three sides of triangle
345
Area of triangle: 6.0000
18. Write aprogram to find Distance travelled by an object
Program:
#include <stdio.h>
#include <conio.h>
void main(){
long int d,s,t;
clrscr();
printf("Enter the speed and time:\n");
scanf("%ld%ld",&s,&t);
d=s*t;
printf("The distance Travelled by the object is : %ld\n", d);
getch();
}
Output:
Enter the speed and time:
40 60
The distance Travelled by the object is : 2400
19. Write a program to evaluate expressions.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
double A=1,B=2,C=3,D=4,E=5,F=6,G=7,i=8,J;
clrscr();
printf("A=1 B=2 C=3 D=4 E=5 F=6 G=7");
printf("\n\tExpression \t\t Results");
printf("\n\tA+B*C+(D*E)+F*G: %.2lf",A+B*C+(D*E)+F*G);
printf("\n\tA/B*C-B+A*D/3 : %.2lf",A/B*C-B+A*D/3);
printf("\n\tA+++B---A : %.2lf",A+++B---A);
printf("\n\tJ=(i++)+(++i) : %.2lf",(i++)+(++i));
getch();
}
Output:
A=1 B=2 C=3 D=4 E=5 F=6 G=7
Expression Results
A+B*C+(D*E)+F*G : 69.00
A/B*C-B+A*D/3 : 0.83
A+++B---A : 1.00
J=(i++)+(++i) : 18.00
20. Write a C program to find the max and min of four numbers using if-else.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,n3,n4;
clrscr();
printf("Enter four numbers:");
scanf("%d%d%d%d",&n1,&n2,&n3,&n4);
// finding maximum among four numbers
if(n1>=n2 && n2>=n3 && n1>=n4)
printf("%d is maximum number",n1);
else if(n2>=n3 && n2>=n4)
printf("%d is maximum number",n2);
else if(n3>=n4)
printf("%d is maximum number",n3);
else
printf("%d is maximum number",n4);
// finding minimum amoung four numbers
if(n1<=n2 && n2<=n3 && n1<=n4)
printf("\n%d is minimum number",n1);
else if(n2<=n3 && n2<=n4)
printf("\n%d is minimum number",n2);
else if(n3<=n4)
printf("\n%d is minimum number",n3);
else
printf("\n%d is minimum number",n4);
getch();
}
Output:
Enter four numbers:-23 23 4 -230
23 is maximum number
-230 is minimum number
21. Write a C program to simulate a calculator using switch case.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,result,choice;
float f;
clrscr();
printf("MENU");
printf("\n*****");
printf("\n1.Addition.");
printf("\n2.Subtraction.");
printf("\n3.Multiplication.");
printf("\n4.Division.");
printf("\n5.Modulo division.");
printf("\nEnter your choice:");
scanf("%d",&choice);
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
switch(choice)
{
case 1:result=a+b;
printf("\nAddition of two numbers is:%d",result);
break;
case 2:result=a-b;
printf("\nSubtraction of two number is:%d",result);
break;
case 3:result=a*b;
printf("\nMultiplication of two numbers is:%d",result);
break;
case 4:f=(float)a/b;
printf("\nDivision of two numbers is:%f",f);
break;
case 5:result=a%b;
printf("\nModulo division of two numbers is:%d",result);
break;
default:printf("Invalid choice.");
}
getch();
}
Output:
MENU
*****
1.Addition.
2.Subtraction.
3.Multiplication.
4.Division.
5.Modulo division.
Enter your choice:1
Enter two numbers:10 5
Addition of two numbers is:15
22. Write a C program to generate electricity bill.
S.NO UNITS UNIT SPECIAL CUSTOMER
TARIFF CHARGE CHARGE
1 0-30 1.90 10 25
2 31-75 3.00 10 30
3 76-125 4.50 10 45
4 126-225 6.00 10 50
5 226-400 8.75 10 55
6 Above 9.75 10 55
400
Program:
#include<stdio.h>
#include<conio.h>
void main()
{

int units,spl_charge=10,cust_charge;
float units_amt,total_amt;
clrscr();
printf("Enter no of units consumed:");
scanf("%d",&units);
if (units <= 30)
{
units_amt=units * 1.90;
cust_charge=25;
}
else if (units <= 75)
{
units_amt= (30 * 1.90) +
(units - 30) * 3.00;
cust_charge=30;
}
else if (units <= 125)
{
units_amt= (30 * 1.90) +
(45 * 3.00) +
(units - 75) * 4.50;
cust_charge=45;
}
else if (units <= 225)
{
units_amt= (30 * 1.90) +
(45 * 3.00) +
(50 * 4.50) +
(units - 125) * 6.0;
cust_charge=50;
}
else if(units <= 400)
{
units_amt= (30 * 1.90) +
(45 * 3.00) +
(50 * 4.50) +
(100* 6.00) +
(units - 225) * 8.75;
cust_charge=55;

}
else if(units > 400)
{
units_amt= (30 * 1.90) +
(45 * 3.00) +
(50 * 4.50) +
(100 * 6.00) +
(175 * 8.75) +
(units - 400) * 8.75;
cust_charge=55;
}
total_amt=units_amt+spl_charge+cust_charge;
printf("Electricity bill = %.2f",total_amt );
getch();
}
Output:
Enter no of units consumed:290
Electricity bill = 1650.75
23. Find the roots of the quadratic equation.
Program:
#include <math.h>
#include <stdio.h>
#include<conio.h>
void main() {
double a, b, c, dis, r1, r2, RP, IP;
clrscr();
printf("Enter a, b and c values: ");
scanf("%lf%lf%lf",&a,&b,&c);

dis = b * b - 4 * a * c;
// condition for real and different roots
if (dis > 0) {
r1 = (-b + sqrt(dis)) / (2 * a);
r2 = (-b - sqrt(dis)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", r1, r2);
}
// condition for real and equal roots
else if (dis == 0) {
r1 = r2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", r1);
}
// if roots are not real
else {
RP = -b / (2 * a);
IP = sqrt(-dis) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", RP, IP, RP, IP);
}
getch()
}
Output:
Enter coefficients a, b and c: 1 1 4
root1 = -0.50+1.94i and root2 = -0.50-1.94i
24. Write a C program to find the given year is a leap year or not.
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
getch();
}
Output:
Enter a year: 2000
2000 is a leap year.
25. Find the factorial of given number using any loop
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i;
unsigned long long fact = 1;
clrscr( );
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
getch();
}
26. Find the given number is a prime or not.
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int num, i, c = 0;
clrscr( );
printf("Enter an Number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++){
if (num % i == 0){
c++;
}
}
if (c == 2){
printf("%d is a Prime Number.", num);
}
else {
printf("%d is not a Prime Number.", num);
}
getch();
}
Output:
Enter an Number: 6
6 is not a Prime Number.
27. Checking a number palindrome
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int n, reversed = 0, remainder, original;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if original and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

getch();
}
Output:
Enter an integer: 1221
1221 is a palindrome.
28. Construct a pyramid of numbers.
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j,s,n,r;
printf("Enter number of rows:");
scanf("%d",&r);
s=r-1;
for(i=1; i<=r; i++){
for(j=1; j<=s; j++){
printf(" ");
}
for(n=1; n<=i; n++){
printf("%d ",i);
}
printf("\n");
s=s-1;
}
//getch();
return 0;
}
Output:
Enter number of rows:9

1
22
333
4444
55555
666666
7777777
88888888
999999999
29. Find the min and max of a 1-D integer array.
//Find the min and max of a 1-D integer array.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10];
int i,n,min,max;
clrscr();
printf("How many numbers you store in an array:");
scanf("%d",&n);
printf(“Enter %d elements:\n”,n);
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
min=a[0];
max=a[0];
for(i=0; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("Minimum value in array is:%d\n",min);
printf("Maximum value in array is:%d\n",max);
getch();
}
Output:
How many numbers you store in an array: 5
Enter 5 elements:
1 -23 4 5 99
Minimum value in array is:-23
Maximum value in array is:99
30. Perform linear search on1D array.
Program:
#include <stdio.h>
//#include <conio.h>
void main()
{
int a[10],i,item,n;
//clrscr();
printf("\nEnter number of elements of an array:\n");
scanf("%d",&n);
printf("\nEnter elements: \n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\n%d Item found at array element %d",item, i+1);
break;
}
if (i > 9)
printf("\n%d Item does not exist.",item);
//getch();
}
Output:
Enter number of elements of an array:
5
Enter elements:
11 22 33 44 5
Enter item to search: 22
22 Item found at array element 2
31. The reverse of a 1D integer array
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter the size of the array:");
scanf(“%d”,&n);
int arr[n];
int i;
printf("Enter the %d array elements:",n);
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
printf("Reversed array is:\n");
for(i = n-1; i >= 0; i--)
{
printf("%d\t",arr[i]);
}
//getch();
}
Output:
Enter the size of the array:10
Enter the 10 array elements:1 2 3 4 5 6 7 8 9 10
Reversed array is:
10 9 8 7 6 5 4 3 2 1
32. Eliminate duplicate elements in an array.
Program:
#include <stdio.h>
int main()
{
int n, count = 0;
printf("Enter number of elements in the array: ");
scanf("%d", &n);
int arr[n], temp[n];
if(n==0)
{
printf("No element inside the array.");
exit(0);
}
printf("Enter elements in the array: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("\nArray Before Removing Duplicates: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
// To store unique elements in temp after removing the duplicate elements
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < count; j++)
{
if (arr[i] == temp[j])
break;
}
if (j == count)
{
temp[count] = arr[i];
count++;
}
}
printf("\nArray After Removing Duplicates: ");
for (int i = 0; i < count; i++)
printf("%d ", temp[i]);
return 0;
}
Output:
Enter number of elements in the array: 3
Enter elements in the array: 1 2 1
Array Before Removing Duplicates: 1 2 1
Array After Removing Duplicates: 1 2
33. Find 2’s complement of the given binary number.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 8
int main(){
int i, carry = 1;
char num[SIZE + 1], one[SIZE + 1], two[SIZE + 1];
printf("Enter the binary number:");
gets(num);
for(i = 0; i < SIZE; i++){
if(num[i] == '0'){
one[i] = '1';
}
else if(num[i] == '1'){
one[i] = '0';
}
}
one[SIZE] = '\0';
printf("Ones' complement of binary number %s is %s",num, one);
for(i = SIZE - 1; i >= 0; i--){
if(one[i] == '1' && carry == 1){
two[i] = '0';
}
else if(one[i] == '0' && carry == 1){
two[i] = '1';
carry = 0;
}
else{
two[i] = one[i];
}
}
two[SIZE] = '\0';
printf("\nTwo's complement of binary number %s is %s",num, two);
return 0;
}
Output:
Enter the binary number:1000010
Ones' complement of binary number 1000010 is 0111101
Two's complement of binary number 1000010 is 0111110
34. Addition of two matrices
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n;
int a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter the row and column size of matrices:");
scanf("%d%d",&m,&n);
printf("enter %d elements for a matrix:",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter %d elements for b matrix:",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
/*displaying array elements*/
printf("After addition array elements are:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter the row and column size of matrices:
2 2
enter 4 elements for a matrix:
1 2
3 4
enter 4 elements for b matrix:
1 2
3 4
After addition array elements are:
2 4
6 8
35. Multiplication two matrices
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int m, n, p, q, c, d, k;
int first[10][10], second[10][10], multiply[10][10];
clrscr();
printf("\nEnter the number of rows and columns of first matrix:\n");
scanf("%d%d", &m, &n);
printf("\nEnter the number of rows and columns of second matrix:\n");
scanf("%d%d", &p, &q);
if ( n != p )
{
printf("\nMatrices with entered orders can't be multiplied with each other.\n");
printf("\nThe column of first matrix should be equal to row of second.\n");
}
else
{
printf("\nEnter %d the elements of first matrix:\n",m*n);
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

printf("\nEnter %d the elements of second matrix:\n",p*q);


for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);

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


{
for ( d = 0 ; d < q ; d++ )
{
multiply[c][d] = 0;
for ( k = 0 ; k < p ; k++ )
{
multiply[c][d] = multiply[c][d]+first[c][k]*second[k][d];
}
}
}
printf("\nThe product of entered matrices is:\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);

printf("\n");
}
}
getch()
}
Output:
Enter the number of rows and columns of first matrix:
2 2
Enter the number of rows and columns of second matrix:
2 2
Enter 4 the elements of first matrix:
2 2
2 2
Enter 4 the elements of second matrix:
2 2
2 2
The product of entered matrices is:
8 8
8 8
36. Concatenate two strings without built-in functions.
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
char str1[100], str2[100];
int i, j;
clrscr();
printf("\n Enter the First String : ");
gets(str1);
printf("\n Please Enter the Second : ");
gets(str2);
// To iterate First String from Start to end
for (i = 0; str1[i]!='\0'; i++);
// Concatenating Str2 into Str1
for (j = 0; str2[j]!='\0'; j++, i++)
{
str1[i] = str2[j];
}
str1[i] = '\0';
printf("\n After the Concatenate String is %s", str1);
getch();
return 0;
}
Output:
Enter the First String : abc
Please Enter the Second : def
After the Concatenate String is abcdef
37. Reverse a string using without built-in string function.
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int i,n;
char str[20];
clrscr();
printf("Enter the String:\n");
gets(str);
n=strlen(str);
printf("\nReversed string is \n");
for(i=n-1;i>=0;i--)
{
printf("%c",str[i]);
}
getch();
}
Output:
Enter the String:
ssits
Reversed string is
stiss
38. Reverse a string using with built-in string function.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
clrscr();
printf("Enter the string:");
gets(str);
printf("\nThe reverse string is:%s",strrev(str));
getch();
}
Output:
Enter the string:abcd
The reverse string is:dcba

You might also like