Priyac 2

You might also like

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

ECCX36

PROGRAMMING IN
EMBEDDED SYSTEMS
ASSIGNMENT-1

~A PROJECT BY
S.PRIYANKA-200051601025
INDEX

SI.NO. CONTENT PG.NO.

1. MANIPULATION 3-6
OF BITS
2. SIZE OF DATA 7-8
TYPES
3. LOOPS 9-10

4. CALLING A 11-13
FUNCTION
5. CONDITIONAL 13
OPERATOR
6. SWITCH CASE 14-15

7. ARRAY 15-16

8. MATRIX 17-20
MULTIPLICATION

9. POINTERS 20-21

10. STRUCTURE 21-23

11. PROGRAM TO 23-24


FORM PYRAMID
OF STARS
1.MANIPULATION OF BITS

TESTING THE BITS:


PROGRAM:
// TESTING THE BITS

#include <stdio.h>

int main() {

int a,b,c;

printf("enter a number");

scanf("%d",&a);

printf("enter the position");

scanf("%d",&b);

c=1<<b;

a=a&c;

if(a==0)

printf("the %d poistion is %d",b,0);

else

printf("the %d poistion is %d",b,1);

return 0;

OTPUT:
INVERTING A BIT:
PROGRAM:
// TINVERTING THE BITS
#include <stdio.h>

int main() {

int a,b,c;

printf("enter a number");

scanf("%d",&a);

printf("enter the position");

scanf("%d",&b);

b=a^(1<<b);

printf("the number is %b",b);

return 0;

OUTPUT:
CLEARING A BIT:
PROGRAM:
#include <stdio.h>

int main() {

int a,b,c;

printf("enter a number");

scanf("%d",&a);

printf("enter the position");

scanf("%d",&b);

b=a&(~(1<<b));

printf("the number is %b",b);

return 0;

OUTPUT:
EXTRACTING A BIT:
PROGRAM:
/ EXTRACTING THE BITS
#include <stdio.h>

int main() {

int a,b,c;

printf("enter a number");

scanf("%d",&a);

printf("enter the position");

scanf("%d",&b);

b=(a&(1<<b))>>b;

printf("the number is %b",b);

return 0;

OUTPUT:
SETTING A BIT:
PROGRAM:
// setting THE BITS
#include <stdio.h>

int main() {

int a,b,c;

printf("enter a number");

scanf("%d",&a);

printf("enter the position");

scanf("%d",&b);

b=a|(1<<b);

printf("the number is %b",b);

return 0;

OUTPUT:
2.Size of data types:
program:
// size of data type

#include <stdio.h>

int main() {

int i=5;

float j=7;

char k=1;

//to find size of datatype

printf("the size of integer is %u",sizeof(int));

printf("\nthe size of float is %u",sizeof(float));

printf("\nthe size of character is %u",sizeof(char));

//adress

printf("\nthe address of i %u",&i);

printf("\nthe address of j %u",&j);

printf("\nthe address of k %u",&k);

return 0;

OTPUT:
3.LOOPS

FOR LOOP:
Program:
// for looop

#include <stdio.h>

int main() {

int i=5;

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

printf("%d",i);

return 0;

OUTPUT:
WHILE LOOP:
Program:
// Online C compiler to run C program online

#include <stdio.h>

int main() {

int var=5;

while(var<=10)

printf("%d",var);

var++;

return 0;

}
Output:

DO WHILE:
Program:
/ DO while

#include <stdio.h>

int main() {

int i=5;

do{

printf("%d",i);

i++;

while(i<10);

return 0;

OUTPUT:
4.CALLING A FUNCTION:
Swapping of two numbers : call by value
Program:
/swapping two numbers by call by value

#include <stdio.h>

void swap(int , int); //prototype of the function

int main()

int a = 10;

int b = 20;

printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b
in main

swap(a,b);

printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do
not change by changing the formal parameters in call by value, a = 10, b = 20

void swap (int a, int b)

int temp;

temp = a;
a=b;

b=temp;

printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b =


10

Output:

SWAPPING OF NUMBERS-call by reference


Program:
//swapping two numbers by call by reference

#include<stdio.h>

void change(int *num) {

printf("Before adding value inside function num=%d \n",*num);

(*num) += 100;

printf("After adding value inside function num=%d \n", *num);

int main() {

int x=100;

printf("Before function call x=%d \n", x);


change(&x);//passing reference in function

printf("After function call x=%d \n", x);

return 0;

OUTPUT:

5.CONDITIONAL OPERATOR:
PROGRAM:
#include <stdio.h>

int main() {

int num;

scanf("%d", &num);

(num % 2 == 0)? printf("The given number is even") : printf("The given number is odd");

return 0;

OUTPUT:
6.SWITCH CASE:
PROGRAM:
// Program to create a simple calculator

#include <stdio.h>

int main() {

char operation;

double n1, n2;

printf("Enter an operator (+, -, *, /): ");

scanf("%c", &operation);

printf("Enter two operands: ");

scanf("%lf %lf",&n1, &n2);

switch(operation)

case '+':

printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);

break;

case '-':

printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);

break;

case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);

break;

case '/':

printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);

break;

// operator doesn't match any case constant +, -, *, /

default:

printf("Error! operator is not correct");

return 0;

OUTPUT:

7. ARRAY:
PROGRAM:
#include <stdio.h>

int main() {

//declaring and initializing one-dimensional array in C

int arr[3] = {10, 20, 30};


// After declaration, we can also initialize the array as:

// arr[0] = 10; arr[1] = 20; arr[2] = 30;

for (int i = 0; i < 3; i++) {

// accessing elements of array

printf(" Value of arr[%d]: %d\n", i, arr[i]);

OUTPUT:

8.MATRIX MULTIPLICATION USING MULTIDIMENSIONAL


ARRAY:
PROGRAM:
#include <stdio.h>

// function to get matrix elements entered by the user

void getMatrixElements(int matrix[][10], int row, int column) {

printf("\nEnter elements: \n");

for (int i = 0; i < row; ++i) {

for (int j = 0; j < column; ++j) {


printf("Enter a%d%d: ", i + 1, j + 1);

scanf("%d", &matrix[i][j]);

// function to multiply two matrices

void multiplyMatrices(int first[][10],

int second[][10],

int result[][10],

int r1, int c1, int r2, int c2) {

// Initializing elements of matrix mult to 0.

for (int i = 0; i < r1; ++i) {

for (int j = 0; j < c2; ++j) {

result[i][j] = 0;

// Multiplying first and second matrices and storing it in result

for (int i = 0; i < r1; ++i) {

for (int j = 0; j < c2; ++j) {

for (int k = 0; k < c1; ++k) {

result[i][j] += first[i][k] * second[k][j];

// function to display the matrix

void display(int result[][10], int row, int column) {


printf("\nOutput Matrix:\n");

for (int i = 0; i < row; ++i) {

for (int j = 0; j < column; ++j) {

printf("%d ", result[i][j]);

if (j == column - 1)

printf("\n");

int main() {

int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;

printf("Enter rows and column for the first matrix: ");

scanf("%d %d", &r1, &c1);

printf("Enter rows and column for the second matrix: ");

scanf("%d %d", &r2, &c2);

// Taking input until

// 1st matrix columns is not equal to 2nd matrix row

while (c1 != r2) {

printf("Error! Enter rows and columns again.\n");

printf("Enter rows and columns for the first matrix: ");

scanf("%d%d", &r1, &c1);

printf("Enter rows and columns for the second matrix: ");

scanf("%d%d", &r2, &c2);

// get elements of the first matrix

getMatrixElements(first, r1, c1);


// get elements of the second matrix

getMatrixElements(second, r2, c2);

// multiply two matrices.

multiplyMatrices(first, second, result, r1, c1, r2, c2);

// display the result

display(result, r1, c2);

return 0;

OUTPUT:

9.POINTERS:
PROGRAM:
#include <stdio.h>

int main()

int* pc, c;

c = 22;

printf("Address of c: %p\n", &c);


printf("Value of c: %d\n\n", c); // 22

pc = &c;

printf("Address of pointer pc: %p\n", pc);

printf("Content of pointer pc: %d\n\n", *pc); // 22

c = 11;

printf("Address of pointer pc: %p\n", pc);

printf("Content of pointer pc: %d\n\n", *pc); // 11

*pc = 2;

printf("Address of c: %p\n", &c);

printf("Value of c: %d\n\n", c); // 2

return 0;

OUTPUT:

10.STRUCTURE:
PROGRAM:
#include <string.h>

// create struct with person1 variable

struct Person {
char name[50];

int citNo;

float salary;

} person1;

int main() {

// assign value to name of person1

strcpy(person1.name, "George Orwell");

// assign values to other person1 variables

person1.citNo = 1984;

person1. salary = 2500;

// print struct variables

printf("Name: %s\n", person1.name);

printf("Citizenship No.: %d\n", person1.citNo);

printf("Salary: %.2f", person1.salary);

return 0;

OUTPUT:
11.PROGRAM TO FPRM PYRAMID OF STARS:
PROGRAM:
#include <stdio.h>

void main() {
int i,j,rows,k;
printf("enter a number");
scanf("%d",&rows);
printf("\n");
for(i=1;i<=rows;i++)
{
printf("");
for(j=1;j<=rows;j++){
}
for(k=i;k<=rows;k++)
{
printf("*");//print the start
}
printf("\n");
}
}
OUTPUT:

You might also like