Programs For C Building Blocks:: 1. WAP in C To Print "HELLO WORLD"
Programs For C Building Blocks:: 1. WAP in C To Print "HELLO WORLD"
Programs For C Building Blocks:: 1. WAP in C To Print "HELLO WORLD"
· Switch Case
· DO-WHILE LOOP
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;
}
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");
}
}
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;
stars = 1;
spaces = rows - 1;
/* Print stars */
for(j=1; j<stars*2; j++)
printf("*");
if(i<rows)
{
spaces--;
stars++;
}
else
{
spaces++;
stars--;
}
}
return 0;
}
int main()
{
int rows, cols, i, j;
printf("\n");
}
return 0;
}
return 0;
}
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;
printf("\n");
}
return 0;
}
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;
}
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;
}
OCTAL TO DECIMAL
#include <stdio.h>
#include <math.h>
return 0;
}
while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}
i = 1;
return decimalNumber;
}
DECIMAL TO OCTAL
#include <stdio.h>
#include <math.h>
return 0;
}
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];
quotient = decimalnum;
while (quotient != 0)
{
remainder = quotient % 16;
if (remainder < 10)
hexadecimalnum[j++] = 48 + remainder;
else
hexadecimalnum[j++] = 55 + remainder;
quotient = quotient / 16;
}
int main()
{
char hex[17], bin[65] = "";
int i = 0;
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;
/*
* Iterate over each hex digit
*/
for(i=0; hex[i]!='\0'; i++)
{
return 0;
}
HEXADECIMAL TO OCTAL
#include <stdio.h>
int main()
{
char hex[17];
long long octal, bin, place;
int i = 0, rem, val;
octal = 0ll;
bin = 0ll;
place = 0ll;
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;
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;
}
place *= 10;
}
return 0;
}
#define SIZE 8
int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
int i, carry=1;
/*
* 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';
return 0;
}
int main() {
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;
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++;
}
}
return (0);
int main()
{
int a[1000],i,n,min,max;
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;
}
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
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};
return 0;
}
e_len = o_len = 5;
index = 0;
return 0;
}
return 0;
}
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);
return 0;
}
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
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");
multiply[c][d] = sum;
sum = 0;
}
}
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);
}
return 0;
}
int isPrime(int num)
{
int i;
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 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++;
}
}