Programs For C Building Blocks:: 1. WAP in C To Print "HELLO WORLD"

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 86

PROGRAMS FOR C BUILDING BLOCKS:

1. WAP in C to print “HELLO WORLD”.


#include<stdio.h>
void main()
{
printf("HELLO WORLD");
}

2. WAP in C to calculate the area of circle.


#include<stdio.h>
void main()
{
float r=10, area;
area=3.14*r*r;
printf("Area of circle is %.2f",area);
}

3. WAP in C to divide two numbers.


#include<stdio.h>
void main()
{
float a=25, b=5, c;
c=a/b;
printf("%.2f / %.2f = %.2f",a,b,c); }

4. WAP in C to add two integers.


#include<stdio.h>
void main()
{
int a=4, b=5, c;
c=a+b;
printf("%d + %d = %d",a,b,c);
}

5. WAP in C to find the ASCII value of a character.


#include<stdio.h>
void main()
{
char c;
printf("Enter a character :");
scanf("%c",&c);
printf("ASCII value of %c is %d",c,c);
}

6. WAP in C to multiply two floating point number.


#include<stdio.h>
void main()
{
float a=3.14 , b=44.3, c;
c=a*b;
printf("%f * %f = %f",a,b,c);
}

PROGRAMS FOR DECISION MAKING:


· If…..Else Programs

1 1. WAP in C to check whether a year is leap year or not.


#include<stdio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if(year%4==0)
{
printf("Leap Year");
}
else
{
printf("Not a Leap Year");
}
}

2 2. Write a C program to check whether a character is uppercase or


lowercase alphabet.
#include<stdio.h>
void main()
{
char c;
printf("Enter an alphabat: ");
scanf("%c", &c);
if(c>='A' && c<='Z')
{
printf("Uppercase");
}
else(c>='a' && c<='z')
{
printf("Lowercase");
}
}

3 3. Write a C program to find all roots of a quadratic equation.


#include <stdio.h>
#include <math.h>
void main()
{
float a, b, c;
float x, y, i;
float d;

printf("Enter values in Quadratic equation ax^2 + bx +c = 0");


printf("\na: ");
scanf("%f", &a);
printf("b: ");
scanf("%f", &b);
printf("c: ");
scanf("%f", &c);
d=(b*b)-(4*a*c);
if(d > 0)
{
x = (-b + sqrt(d)) / (2*a);
y = (-b - sqrt(d)) / (2*a);
printf("Roots: %.2f, %.2f", x, y);
}
else if(d== 0)
{
x = y = -b / (2 * a);
printf("Roots: %.2f, %.2f", x, y);
}
else if(d< 0)
{
x = y = -b / (2 * a);
i = sqrt(-d) / (2 * a);
printf("Roots: %.2f+√%.2f, %.2f-√%.2f", x, i, y, i);
}
}
4 Write a C program to input electricity unit charges and calculate total
electricity bill according to the given condition: For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit For next 100 units Rs. 1.20/unit For unit
above 250 Rs. 1.50/unit An additional surcharge of 20% is added to the bill
#include <stdio.h>
void main()
{
int unit;
float amt, total_amt, sur_charge;
printf("Enter total units consumed: ");
scanf("%d", &unit);
if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
}
sur_charge = amt * 0.20;
total_amt = amt + sur_charge;
printf("Electricity Bill = Rs. %.2f", total_amt);
}

· Switch Case

1 4. Write a C program to input any alphabet and check whether it is vowel or


consonant.
#include <stdio.h>
void main()
{
char ch;

printf("Enter a character: ");


scanf("%c",&ch);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
}
2 5. Write a C program to input any number and check whether number is
odd or even
#include <stdio.h>
void main()
{
int a;
printf("Enter any number: ");
scanf("%d", &a);
switch(a%2)
{
case 0:
printf("%d is EVEN",a);
break;
case 1:
printf("%d is ODD",a);
break;
}
}
PROGRAMS FOR LOOPS:
· WHILE LOOP

1 Write a C program to print all natural numbers from 1 to n.


#include <stdio.h>
void main()
{
int a,i=1;
printf("Enter a number: ");
scanf("%d", &a);
printf("Natural Numbers till %d are: ",a);
while(i<=a)
{
printf("%d, ",i);
i++;
}
printf("\b\b.");
}

2 Write a C program to print all even numbers between 1 to 100.


#include <stdio.h>
void main()
{
int a=100,i=2;
printf("Even Numbers from 1 to 100 are: ");
if(i%2==0)
{
while(i<=a)
{
printf("%d, ",i);
i+=2;
}
printf("\b\b.");
}
}

3 Write a C program to print Fibonacci series up to n terms.


#include <stdio.h>
int main() {
int i=1, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
while(i<=n)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
i++;
}
printf("\b\b.");
}

4 Write a C program to check whether a number is palindrome or not.


#include <stdio.h>
void main()
{
int n, num, rev = 0;
printf("Enter any number: ");
scanf("%d", &n);
num = n;
while(n != 0)
{
rev = (rev * 10) + (n % 10);
n /= 10;
}
if(rev == num)
{
printf("%d is palindrome.", num);
}
else
{
printf("%d is not palindrome.", num);
}
}

· DO-WHILE LOOP

1 Write a C program to find sum of all even numbers between 1 to n.


#include<stdio.h>
int main()
{
int num, count = 1, sum = 0;
printf("Enter the limit: ");
scanf("%d", &num);

while(count<=num)
{
if(count%2 == 0)
{
sum = sum + count;
}
count++;
}
printf("sum of even numbers from 1 to %d is %d\n", num, sum);
return 0;
}

2 Write a C program to find HCF (GCD) and LCM of two numbers.


#include <stdio.h>
void main()
{
int n1, n2, max, x;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
max = (n1 > n2) ? n1 : n2;
do
{
if (max % n1 == 0 && max % n2 == 0)
{
printf("The LCM of %d & %d : %d.", n1, n2, max);
break;
}
max++;
}while (1);
int i=1;
do
{
if(n1%i==0 && n2%i==0)
x=i;
i++;
}while(i<=n1 || i<=n2);
printf("\nThe HCF of %d & %d : %d.",n1,n2,x);
}

3 Write a C program to find power of a number using for loop.


#include <stdio.h>
void main()
{
int base, exponent;
long long power = 1;
int i;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++)
{
power = power * base;
}
printf("%d ^ %d = %lld", base, exponent, power);
}

· NESTING LOOP (using for loop only)

1 Star pattern programs - Write a C program to print the given star patterns.
Pyramid Star Pattern
#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter number of rows : ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
for(j=i; j<rows; j++)
{
printf(" ");
}
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
}

Hollow Pyramid Star Pattern


#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter number of rows : ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
for(j=i; j<rows; j++)
{
printf(" ");
}
for(j=1; j<=(2*i-1); j++)
{
if(i==rows || j==1 || j==(2*i-1))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}}
2 Star pattern programs - Write a C program to print the given star patterns
Inverted Pyramid Star Pattern
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows : ");
scanf("%d", &rows);

for(i=1; i<=rows; i++)


{
/* Print leading spaces */
for(j=1; j<i; j++)
{
printf(" ");
}
for(j=1; j<=(rows*2 -(2*i-1)); j++)
{
printf("*");
}
printf("\n");
}

return 0;
}

Hollow Inverted Pyramid Star Pattern


#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
for(j=1; j<i; j++)
{
printf(" ");
}
for(j=1; j<=(rows*2 - (2*i-1)); j++)
{
if(i==1 || j==1 || j==(rows*2 - (2*i - 1)))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}

3 Star pattern programs - Write a C program to print the given star patterns.
Half Diamond Star Pattern
#include <stdio.h>

int main()
{
int i, j, rows;
int stars, spaces;

printf("Enter rows to print : ");


scanf("%d", &rows);

stars = 1;
spaces = rows - 1;

/* Iterate through rows */


for(i=1; i<rows*2; i++)
{
/* Print spaces */
for(j=1; j<=spaces; j++)
printf(" ");

/* Print stars */
for(j=1; j<stars*2; j++)
printf("*");

/* Move to next line */


printf("\n");

if(i<rows)
{
spaces--;
stars++;
}
else
{
spaces++;
stars--;
}
}

return 0;
}

4 Number pattern programs - Write a C program to print the given number


patterns
#include <stdio.h>
int main()
{
int rows, cols, i, j;

/* Input rows and columns from user */


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

/* Iterate through rows */


for(i=1; i<=rows; i++)
{
/* Iterate through columns */
for(j=1; j<=cols; j++)
{
printf("1");
}
printf("\n");
}
return 0; }
Number pattern 11111 00000 11111 00000 11111
#include <stdio.h>

int main()
{
int rows, cols, i, j;

/* Input rows and columns from user */


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

for(i=1; i<=rows; i++)


{
for(j=1; j<=cols; j++)
{
// Print 1 if current row is odd
if(i%2 == 1)
{
printf("1");
}
else
{
printf("0");
}
}

printf("\n");
}

return 0;
}

Number pattern 01010 01010 01010 01010 01010


#include <stdio.h>
int main()
{
int rows, cols, i, j;

/* Input rows and columns from user */


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

for(i=1; i<=rows; i++)


{
for(j=1; j<=cols; j++)
{
// Print 1 if current column is even
if(j%2 == 1)
{
printf("0");
}
else
{
printf("1");
}
}
printf("\n");
}

return 0;
}

Number pattern 10101 01010 10101 01010 10101


#include <stdio.h>

int main()
{
int rows, cols, i, j, k;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

k = 1;
for(i=1; i<=rows; i++)
{
for(j=1; j<=cols; j++)
{
if(k == 1)
{
printf("1");
}
else
{
printf("0");
}
k *= -1;
}

if(cols % 2 == 0)
{
k *= -1;
}

printf("\n");
}

return 0;
}
Number pattern 11111 10001 10001 10001 11111
#include <stdio.h>

int main()
{
int rows, cols, i, j;

/* Input rows and columns from user */


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

for(i=1; i<=rows; i++)


{
for(j=1; j<=cols; j++)
{
/*
* Print 1 if its first or last row
* Print 1 if its first or last column
*/
if(i==1 || i==rows || j==1 || j==cols)
{
printf("1");
}
else
{
printf("0");
}
}

printf("\n");
}

return 0;
}

Number pattern 1111 11111 11011 11111 11111


#include <stdio.h>
int main()
{
int rows, cols, i, j;
int centerRow, centerCol;

/* Input rows and columns from user */


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

/* Find center row and column */


centerRow = (rows + 1) / 2;
centerCol = (cols + 1) / 2;

for(i=1; i<=rows; i++)


{
for(j=1; j<=cols; j++)
{
if(centerCol == j && centerRow == i)
{
printf("0");
}
else if(cols%2 == 0 && centerCol+1 == j)
{
if(centerRow == i || (rows%2 == 0 && centerRow+1 == i))
printf("0");
else
printf("1");
}
else if(rows%2 == 0 && centerRow+1 == i)
{
if(centerCol == j || (cols%2 == 0 && centerCol+1 == j))
printf("0");
else
printf("1");
}
else
{
printf("1");
} }
printf("\n"); }
return 0;
}
PRO
GRAMS FOR CONVERSION

1. Write a C program to convert Binary number to Decimal, Octal and


Hexadecimal Number System.
BINARY TO DECIMAL:
#include <math.h>
#include <stdio.h>
int convert(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}

int convert(long long n) {


int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}

BINARY TO OCTAL
#include <math.h>
#include <stdio.h>
int convert(long long bin);
int main() {
long long bin;
printf("Enter a binary number: ");
scanf("%lld", &bin);
printf("%lld in binary = %d in octal", bin, convert(bin));
return 0;
}

int convert(long long bin) {


int oct = 0, dec = 0, i = 0;

// converting binary to decimal


while (bin != 0) {
dec += (bin % 10) * pow(2, i);
++i;
bin /= 10;
}
i = 1;

// converting to decimal to octal


while (dec != 0) {
oct += (dec % 8) * i;
dec /= 8;
i *= 10;
}
return oct;
}

BINARY TO HEXADECIMAL
#include<stdio.h>
#include<conio.h>

int main()
{
long int binary_number, hexadecimal_number = 0, i = 1, remainder;
printf("Please Enter any Binary Number: ");
scanf("%ld", &binary_number);
while (binary_number != 0)
{
remainder = binary_number % 10;
hexadecimal_number = hexadecimal_number + remainder * i;
i = i * 2;
binary_number = binary_number / 10;
}
printf("Equivalent Hexadecimal Number %lX", hexadecimal_number);

return 0;
}

2 Write a C program to convert Octal number to Binary, Decimal and


Hexadecimal number system.
OCTAL TO BINARY
#include <math.h>
#include <stdio.h>
long long convert(int oct);
int main() {
int oct;
printf("Enter an octal number: ");
scanf("%d", &oct);
printf("%d in octal = %lld in binary", oct, convert(oct));
return 0;
}
long long convert(int oct) {
int dec = 0, i = 0;
long long bin = 0;

// converting octal to decimal


while (oct != 0) {
dec += (oct % 10) * pow(8, i);
++i;
oct /= 10;
}
i = 1;

// converting decimal to binary


while (dec != 0) {
bin += (dec % 2) * i;
dec /= 2;
i *= 10;
}
return bin;
}

OCTAL TO DECIMAL
#include <stdio.h>
#include <math.h>

long long convertOctalToDecimal(int octalNumber);


int main()
{
int octalNumber;

printf("Enter an octal number: ");


scanf("%d", &octalNumber);

printf("%d in octal = %lld in decimal", octalNumber,


convertOctalToDecimal(octalNumber));

return 0;
}

long long convertOctalToDecimal(int octalNumber)


{
int decimalNumber = 0, i = 0;

while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}

i = 1;

return decimalNumber;
}

3 Write a C program to convert Decimal number to Binary, Octal and


Hexadecimal number system.
DECIMAL TO BINARY
#include <math.h>
#include <stdio.h>
long long convert(int n);
int main() {
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %lld in binary", n, convert(n));
return 0;
}

long long convert(int n) {


long long bin = 0;
int rem, i = 1, step = 1;
while (n != 0) {
rem = n % 2;
printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, rem,
n / 2);
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}

DECIMAL TO OCTAL
#include <stdio.h>
#include <math.h>

int convertDecimalToOctal(int decimalNumber);


int main()
{
int decimalNumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);

printf("%d in decimal = %d in octal", decimalNumber,


convertDecimalToOctal(decimalNumber));

return 0;
}

int convertDecimalToOctal(int decimalNumber)


{
int octalNumber = 0, i = 1;

while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}

return octalNumber;
}

DECIMAL TO HEXADECIMAL
#include <stdio.h>

int main()
{
long decimalnum, quotient, remainder;
int i, j = 0;
char hexadecimalnum[100];

printf("Enter decimal number: ");


scanf("%ld", &decimalnum);

quotient = decimalnum;

while (quotient != 0)
{
remainder = quotient % 16;
if (remainder < 10)
hexadecimalnum[j++] = 48 + remainder;
else
hexadecimalnum[j++] = 55 + remainder;
quotient = quotient / 16;
}

// display integer into character


for (i = j; i >= 0; i--)
printf("%c", hexadecimalnum[i]);
return 0;
}

4 Write a C program to convert Hexadecimal number to Binary, Decimal and


Octal number system.
HEXADECIMAL TO BINARY
#include <stdio.h>
#include <string.h>

int main()
{
char hex[17], bin[65] = "";
int i = 0;

/* Input hexadecimal number from user */


printf("Enter any hexadecimal number: ");
gets(hex);

/* Extract first digit and find binary of each hex digit */


for(i=0; hex[i]!='\0'; i++)
{
switch(hex[i])
{
case '0':
strcat(bin, "0000");
break;
case '1':
strcat(bin, "0001");
break;
case '2':
strcat(bin, "0010");
break;
case '3':
strcat(bin, "0011");
break;
case '4':
strcat(bin, "0100");
break;
case '5':
strcat(bin, "0101");
break;
case '6':
strcat(bin, "0110");
break;
case '7':
strcat(bin, "0111");
break;
case '8':
strcat(bin, "1000");
break;
case '9':
strcat(bin, "1001");
break;
case 'a':
case 'A':
strcat(bin, "1010");
break;
case 'b':
case 'B':
strcat(bin, "1011");
break;
case 'c':
case 'C':
strcat(bin, "1100");
break;
case 'd':
case 'D':
strcat(bin, "1101");
break;
case 'e':
case 'E':
strcat(bin, "1110");
break;
case 'f':
case 'F':
strcat(bin, "1111");
break;
default:
printf("Invalid hexadecimal input.");
}
}

printf("Hexademial number = %s\n", hex);


printf("Binary number = %s", bin);

return 0;
}

HEXADECIMAL TO DECIMAL
#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
char hex[17];
long long decimal, place;
int i = 0, val, len;

decimal = 0;
place = 1;

/* Input hexadecimal number from user */


printf("Enter any hexadecimal number: ");
gets(hex);

/* Find the length of total number of hex digit */


len = strlen(hex);
len--;

/*
* Iterate over each hex digit
*/
for(i=0; hex[i]!='\0'; i++)
{

/* Find the decimal representation of hex[i] */


if(hex[i]>='0' && hex[i]<='9')
{
val = hex[i] - 48;
}
else if(hex[i]>='a' && hex[i]<='f')
{
val = hex[i] - 97 + 10;
}
else if(hex[i]>='A' && hex[i]<='F')
{
val = hex[i] - 65 + 10;
}

decimal += val * pow(16, len);


len--;
}

printf("Hexadecimal number = %s\n", hex);


printf("Decimal number = %lld", decimal);

return 0;
}

HEXADECIMAL TO OCTAL
#include <stdio.h>

int main()
{
char hex[17];
long long octal, bin, place;
int i = 0, rem, val;

/* Input hexadecimal number from user */


printf("Enter any hexadecimal number: ");
gets(hex);

octal = 0ll;
bin = 0ll;
place = 0ll;

/* Hexadecimal to binary conversion */


for(i=0; hex[i]!='\0'; i++)
{
bin = bin * place;

switch(hex[i])
{
case '0':
bin += 0;
break;
case '1':
bin += 1;
break;
case '2':
bin += 10;
break;
case '3':
bin += 11;
break;
case '4':
bin += 100;
break;
case '5':
bin += 101;
break;
case '6':
bin += 110;
break;
case '7':
bin += 111;
break;
case '8':
bin += 1000;
break;
case '9':
bin += 1001;
break;
case 'a':
case 'A':
bin += 1010;
break;
case 'b':
case 'B':
bin += 1011;
break;
case 'c':
case 'C':
bin += 1100;
break;
case 'd':
case 'D':
bin += 1101;
break;
case 'e':
case 'E':
bin += 1110;
break;
case 'f':
case 'F':
bin += 1111;
break;
default:
printf("Invalid hexadecimal input.");
}

place = 10000;
}

place = 1;

/* Binary to octal conversion */


while(bin > 0)
{
rem = bin % 1000;

switch(rem)
{
case 0:
val = 0;
break;
case 1:
val = 1;
break;
case 10:
val = 2;
break;
case 11:
val = 3;
break;
case 100:
val = 4;
break;
case 101:
val = 5;
break;
case 110:
val = 6;
break;
case 111:
val = 7;
break;
}

octal = (val * place) + octal;


bin /= 1000;

place *= 10;
}

printf("Hexadecimal number = %s\n", hex);


printf("Octal number = %lld", octal);

return 0;
}

5 Write a C program to find one's complement and two's complement of a


binary number.
#include <stdio.h>

#define SIZE 8

int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
int i, carry=1;

printf("Enter %d bit binary value: ", SIZE);

/* Input 8-bit binary string */


gets(binary);

/* Find ones complement of the binary number */


for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';

/*
* Add 1 to the ones complement
*/
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';

printf("Original binary = %s\n", binary);


printf("Ones complement = %s\n", onesComp);
printf("Twos complement = %s\n", twosComp);

return 0;
}

PROGRAMS FOR ARRAY


1. Write a program in C to add two arrays and print them.
#include<stdio.h>

int main() {
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;

printf("\nEnter no of elements in 1st array :");


scanf("%d", &n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}

printf("\nEnter no of elements in 2nd array :");


scanf("%d", &n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}

i = 0;
j = 0;
k = 0;

// Merging starts
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
res[k] = arr1[i];
i++;
k++;
} else {
res[k] = arr2[j];
k++;
j++;
}
}

/* Some elements in array 'arr1' are still remaining


where as the array 'arr2' is exhausted */

while (i < n1) {


res[k] = arr1[i];
i++;
k++;
}

/* Some elements in array 'arr2' are still remaining


where as the array 'arr1' is exhausted */

while (j < n2) {


res[k] = arr2[j];
k++;
j++;
}

//Displaying elements of array 'res'


printf("\nMerged array is :");
for (i = 0; i < n1 + n2; i++)
printf("%d ", res[i]);

return (0);

2. Write a program in C to find minimum and maximum from the array.


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

int main()
{
int a[1000],i,n,min,max;

printf("Enter size of the array : ");


scanf("%d",&n);

printf("Enter elements in array : ");


for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);

return 0;
}

3. Write a program in C to find any element from the array


#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;

/* Input size of array */


printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements of array */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nEnter element to search: ");


scanf("%d", &toSearch);
found = 0;

for(i=0; i<size; i++)


{
if(arr[i] == toSearch)
{
found = 1;
break;
}
}
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}

return 0;
}

4. Write a Program to find the length of the integer array and character
array.
#include <stdio.h>
int main() {
char c[] = "hello";
int n[] = {-4, -3, -2, -1, 0, 1, 2, 3, 4};
float f[] = {3.14, 2.71};
double d[] = {3.14, 2.71, 1.61};

printf("Size of the character array is: %lu", sizeof(c)/sizeof(char));


printf("\n");
printf("Size of the integer array is: %lu", sizeof(n)/sizeof(int));
printf("\n");
printf("Size of the float array is: %lu", sizeof(f)/sizeof(float));
printf("\n");
printf("Size of the double array is: %lu", sizeof(d)/sizeof(double));
printf("\n");

return 0;
}

5. Write a program to find concatenate two arrays of integer as well as


characters 5.
#include <stdio.h>
int main() {
int array[10];
int even[5] = {0, 2, 4, 6, 8};
int odd[5] = {1, 3, 5, 7, 9};

int loop, index, e_len, o_len;

e_len = o_len = 5;

index = 0;

for(loop = 0; loop < e_len; loop++) {


array[index] = even[loop];
index++;
}

for(loop = 0; loop < o_len; loop++) {


array[index] = odd[loop];
index++;
}

printf("\nEven -> ");

for(loop = 0; loop < e_len; loop++)


printf(" %d", even[loop]);
printf("\nOdd -> ");

for(loop = 0; loop < o_len; loop++)


printf(" %d", odd[loop]);

printf("\nConcat -> ");

for(loop = 0; loop < 10; loop++)


printf(" %d", array[loop]);

return 0;
}

6 . Write a program in C for addition of 2 matrices.


#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 (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 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("Enter elements of 2nd 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", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

7 . Write a program to find the transpose of matrix.


#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// Assigning elements to the matrix


printf("\nEnter matrix elements:\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]);
}

// Displaying the matrix a[][]


printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// Finding the transpose of matrix a


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// Displaying the transpose of matrix a


printf("\nTranspose of the matrix:\n");
for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
8 Write a program to count the length of string.
#include <stdio.h>
int main() {
char s[] = "Programming is fun";
int i;

for (i = 0; s[i] != '\0'; ++i);

printf("Length of the string: %d", i);


return 0;
}

9 Write a program to count to concatenate the two strings.


#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
int i, j;
/* Input two strings from user */
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);

/* Move till the end of str1 */


i=0;
while(str1[i] != '\0')
{
i++;
}

/* Copy str2 to str1 */


j = 0;
while(str2[j] != '\0')
{
str1[i] = str2[j];
i++;
j++;
}

// Make sure that str1 is NULL terminated


str1[i] = '\0';

printf("Concatenated string = %s", str1);

return 0;
}

10 Write a program to find out the product/Multiplication of two matrices


and print the product matrix. (order of matrices must be given by user)
#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

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


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

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

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++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

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


{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}
11 Write a program in C to display the index of smallest and largest element
in 3 X 4 matrix of integers.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10];
int i, j, row, col, small, big;
printf("Enter the order of the matrix : ");
scanf("%d %d", &row, &col);
printf("\nEnter the elements of the matrix : \n\n");
for(i = 0; i < row; i++)
for(j = 0; j < col; j++)
scanf("%d", &mat[i][j]);
big = mat[0][0];
small = mat[0][0];
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
if(mat[i][j] < small)
small = mat[i][j];
if(mat[i][j] > big)
big = mat[i][j];
}
}
printf("\nThe smallest element in the matrix is : %d\n\n",small);
printf("The biggest element in the matrix is : %d", big);
}

PROGRMAS FOR FUNCTIONS AND


RECURSIONS:

1. Write a C program to find diameter, circumference and area of circle using


functions.
#include <stdio.h>
#include <math.h>
double getDiameter(double radius);
double getCircumference(double radius);
double getArea(double radius);
int main()
{
float radius, dia, circ, area;
printf("Enter radius of circle: ");
scanf("%f", &radius);
dia = getDiameter(radius);
circ = getCircumference(radius);
area = getArea(radius);

printf("Diameter of the circle = %.2f units\n", dia);


printf("Circumference of the circle = %.2f units\n", circ);
printf("Area of the circle = %.2f sq. units", area);
return 0;
}
double getDiameter(double radius)
{
return (2 * radius);
}
double getCircumference(double radius)
{
return (2 * M_PI * radius); // M_PI = PI = 3.14 ...
}
double getArea(double radius)
{
return (M_PI * radius * radius); // M_PI = PI = 3.14 ...
}
2. Write a C program to check whether a number is Prime, Armstrong or
perfect number using functions.
#include <stdio.h>
#include <math.h>
int isPrime(int num);
int isArmstrong(int num);
int isPerfect(int num);
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(isPrime(num))
{
printf("%d is Prime number.\n", num);
}
else
{
printf("%d is not Prime number.\n", num);
}
if(isArmstrong(num))
{
printf("%d is Armstrong number.\n", num);
}
else
{
printf("%d is not Armstrong number.\n", num);
}
if(isPerfect(num))
{
printf("%d is Perfect number.\n", num);
}
else
{
printf("%d is not Perfect number.\n", num);
}

return 0;
}
int isPrime(int num)
{
int i;

for(i=2; i<=num/2; i++)


{
if(num%i == 0)
{
return 0;
}
}

return 1;
}
int isArmstrong(int num)
{
int lastDigit, sum, originalNum, digits;
sum = 0;

originalNum = num;
digits = (int) log10(num) + 1;
while(num > 0)
{
lastDigit = num % 10;
sum = sum + round(pow(lastDigit, digits));
num = num / 10;
}

return (originalNum == sum);


}
int isPerfect(int num)
{
int i, sum, n;
sum = 0;
n = num;

for(i=1; i<n; i++)


{
if(n%i == 0)
{
sum += i;
}
}
return (num == sum);
}

3. Write a C program to generate nth Fibonacci term using recursion.


#include <stdio.h>
unsigned long long fibo(int num);
int main()
{
int num;
unsigned long long fibonacci;
printf("Enter any number to find nth fiboacci term: ");
scanf("%d", &num);
fibonacci = fibo(num);
printf("%d fibonacci term is %llu", num, fibonacci);
return 0;
}
unsigned long long fibo(int num)
{
if(num == 0) //Base condition
return 0;
else if(num == 1) //Base condition
return 1;
else
return fibo(num-1) + fibo(num-2);
}

4. Write a C program to print all Armstrong numbers between given interval


using functions.
#include <stdio.h>
int isArmstrong(int num);
void printArmstrong(int start, int end);
int main()
{
int start, end;
printf("Enter lower limit to print armstrong numbers: ");
scanf("%d", &start);
printf("Enter upper limit to print armstrong numbers: ");
scanf("%d", &end);
printf("All armstrong numbers between %d to %d are: \n", start, end);
printArmstrong(start, end);

return 0;
}
int isArmstrong(int num)
{
int temp, lastDigit, sum;

temp = num;
sum = 0;
while(temp != 0)
{
lastDigit = temp % 10;
sum += lastDigit * lastDigit * lastDigit;
temp /= 10;
}
if(num == sum)
return 1;
else
return 0;
}
void printArmstrong(int start, int end)
{
while(start <= end)
{
if(isArmstrong(start))
{
printf("%d, ", start);
}
start++;
}
}

You might also like