DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF ECE
TRAINER NAME -[Link] AP/ECE & [Link] AP/ECE
[Link] to identify of the a number is positive or negative in C.
EXPLANATION
Program, to find whether A is positive, negative or zero; first
the number is taken as input from the user using scanf in, and then A is
checked for positive using statement and , and operators. Below is the C
program to find whether a number is positive, negative or zero.
Input: A = 2
Output: 2 is positive
Input: A = -554
Output: -554 is negative
PROGRAM
#include <stdio.h>
int main()
{
int A;
printf("Enter the number A: ");
scanf("%d", &A);
if (A > 0)
printf("%d is positive.", A);
else if (A < 0)
printf("%d is negative.", A);
else if (A == 0)
printf("%d is zero.", A);
return 0;
}
[Link] to Reverse a given Number.
EXPLANATION
The above program takes an integer num as input. We use a while loop
to iterate until the value of num becomes 0. Inside the loop, the last digit of
num is extracted using the modulo operator (num % 10). This digit is then
added to rev_num after multiplying it by 10, which means the existing digits of
rev_num are shifted one place to the left.
PROGRAM
Input: num
(1) Initialize rev_num = 0
(2) Loop while num > 0
(a) Multiply rev_num by 10 and add remainder of num
divide by 10 to rev_num
rev_num = rev_num*10 + num%10;
(b) Divide num by 10
(3) Return rev_num
int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Driver code
int main()
{
int num = 4562;
printf("Reverse of is %d", reverseDigits(num));
getchar();
return 0;
}
OUTPUT:
Reverse of no. is 2654.
3. . Program to print prime numbers in a given range
EXPLANATION
The range of numbers is taken as input and stored in the variables ‘a’
and ‘b’. Then using for-loop, the numbers between the interval of a and b are
traversed. For each number in the for loop, it is checked if this number is prime
or not. If found prime, print the number. Then the next number in the loop is
checked, till all numbers are checked.
Input: a = 1, b = 10
Output: 2, 3, 5, 7
Input: a = 10, b = 20
Output: 11, 13, 17, 19
int main()
{
// Declare the variables
int a, b, i, j, flag;
// Ask user to enter lower value
// of interval
printf("Enter lower bound of the interval: ");
// Take input
scanf("%d", &a);
// Ask user to enter upper value
// of interval
printf("Enter upper bound of the interval: ");
// Take input
scanf("%d", &b);
// Print display message
printf("Prime numbers between %d and %d are: ",
a, b);
// Traverse each number in the interval
// with the help of for loop
for (i = a; i <= b; i++)
{
// Skip 0 and 1 as they are
// neither prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell
// if i is prime or not
flag = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is
// not prime
if (flag == 1)
printf("%d ", i);
}
return 0;
Output:
Enter lower bound of the interval: 1
Enter upper bound of the interval: 10
Prime numbers between 1 and 10 are: 2 3 5 7
4. Program for Octal to decimal conversion.
The idea is to extract the digits of a given octal number starting from the
rightmost digit and keep a variable dec_value. At the time of extracting digits
from the octal number, multiply the digit with the proper base (Power of 8) and
add it to the variable dec_value. In the end, the variable dec_value will store
the required decimal number.
Input : 67
Output: 55
Input : 512
Output: 330
Input : 123
Output: 83
int octalToDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 8^0
int base = 1;
int temp = num;
while (temp) {
// Extracting last digit
int last_digit = temp % 10;
temp = temp / 10;
// Multiplying last digit with appropriate
// base value and adding it to dec_value
dec_value += last_digit * base;
base = base * 8;
}
return dec_value;
}
// Driver program to test above function
int main()
{
int num = 67;
cout << octalToDecimal(num) << endl;
}
Output
55
5. Program to concatenate a string.
Input: str1 = "hello", str2 = "DGCT"
Output: helloDGCT
Input: str1 = "DGCT", str2 = "placement"
Output: DGCTplacement
Approach: Using ‘+’ operator
int main() {
string str1 = "hello";
string str2 = "DGCT";
string result = str1 + str2;
cout << result << endl;
return 0;
}
6. Program to perform Matrix multiplication.
A matrix is a collection of numbers organized in rows and columns, represented
by a two-dimensional array in C. Matrices can either be square or rectangular.
Multiplication of two matrices is done by multiplying corresponding elements
from the rows of the first matrix with the corresponding elements from the
columns of the second matrix and then adding these products.
Note: The number of columns in the first matrix must be equal to the number
of rows in the second matrix.
Input:
mat1[][] = {{1, 2},
{3, 4}}
mat2[][] = {{5, 6},
{7, 8}}
Multiplication of two matrices:
{{1*5 + 2*7 1*6 + 2*8},
{3*5 + 4*7 3*6 + 4*8}}
Output:
{{19, 22},
{43, 50}}
void multiplyMatrix(int m1[][C1], int m2[][C2])
{
int result[R1][C2];
printf("Resultant Matrix is:\n");
for (int i = 0; i < R1; i++) {
for (int j = 0; j < C2; j++) {
result[i][j] = 0;
for (int k = 0; k < R2; k++) {
result[i][j] += m1[i][k] * m2[k][j];
}
printf("%d\t", result[i][j]);
}
printf("\n");
}
}
// Driver code
int main()
{
// R1 = 4, C1 = 4 and R2 = 4, C2 = 4 (Update these
// values in MACROs)
int m1[R1][C1] = { { 1, 1 }, { 2, 2 } };
int m2[R2][C2] = { { 1, 1, 1 }, { 2, 2, 2 } };
// if coloumn of m1 not equal to rows of m2
if (C1 != R2) {
printf("The number of columns in Matrix-1 must be "
"equal to the number of rows in "
"Matrix-2\n");
printf("Please update MACROs value according to "
"your array dimension in "
"#define section\n");
exit(EXIT_FAILURE);
}
// Function call
multiplyMatrix(m1, m2);
return 0;
}
Output
Resultant Matrix is:
3 3 3
6 6 6