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

R23 C Lab Record

Uploaded by

rishicse547
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

R23 C Lab Record

Uploaded by

rishicse547
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Week 1: Familiarization with programming environment

i) Basic Linux environment and its editor like Vi.


ii) Exposure to Turbo C, gcc
iii) Writing simple programs using printf(), scanf()

i) Basic Linux environment and its editors like Vi, Vim & Emacs etc.
Vi Editor:
 The vi editor was developed by William Joy as a more visual version of his own
command line editor program called ex, which was becoming popular as a text editor.
 The vi editor uses the terminal window for editing a file. For an example, run the
following command to open a new file or an existing file of the same name:
vi filename
 There are two ways of working in vi:

 Command Mode
 Insert Mode

o Command Mode: In command mode, actions are taken on the file. The vi editor starts in
command mode. Here, the typed words will act as commands in vi editor. To pass a
command, you need to be in command mode.
o Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will
take you to the command mode from insert mode.

o By default, the vi editor starts in command mode. To enter text, you have to be in insert
mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will
appear on the screen but you'll be in insert mode. Now you can type anything.
o To exit from insert mode press Esc key, you'll be directed to command mode.

Starting with vi editor:


Syntax:
$vi filename
Editing the file:

- Open the file using $ vifilename


- To add text at the end of the file, position the cursor at the last character ofthe file.
- Switch from command mode to text input mode by pressing‘a’.
- Here ‘a’ stands forappend.
- Inserting text in the middle of the file is possible by pressing‘i’.
- The editor accepts and inserts the typed character until Esc key ispressed.
Saving text:
:w – save the file and remains in edit mode
:wq – save the file and quits from edit mode
:q – quit without changes from edit mode

Quitting vi:
Press zz or ‘:wq’ in command mode.
ii) Exposure to Turbo C, gcc

Turbo C:
Turbo C was an integrated development environment (IDE) for programming in the C language.
It was developed by Borland and first introduced in 1987. At the time, Turbo C was known for
its compact size, comprehensive manual, fast compile speed and low price.

How to install C

There are many compilers available for c and c++. You need to download any one. Here, we are
going to use Turbo C++. It will work for both C and C++. To install the Turbo C software, you
need to follow following steps.

1. Download Turbo C++


2. Create turboc directory inside c drive and extract the tc3.zip inside c:\turboc
3. Double click on install.exe file
4. Click on the tc application file located inside c:\TC\BIN to write the c program

1) Download Turbo C++ software

You can download turbo c++ from many sites. download Turbo c++

2) Create turboc directory in c drive and extract the tc3.zip

Now, you need to create a new directory turboc inside the c: drive. Now extract the tc3.zip file in
c:\truboc directory.

3) Double click on the install.exe file and follow steps

Now, click on the install icon located inside the c:\turboc

Using an IDE - Turbo C

We will recommend you to use Turbo C or Turbo C++ IDE, which is the oldest IDE for C
programming. It is freely available over the Internet and is good for a beginner.

Step 1: Open turbo C IDE(Integrated Development Environment), click on File, and then click
on New
Step 2: Write a Hello World program

Step 3: Click on Compile menu and then on Compile option, or press the keys and press Alt +
F9 to compile the code.

Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C programs are first compiled to
generate the object code and then that object code is Run.
Step 5: Output is here.

GCC:

The original GNU C Compiler (GCC) is developed by Richard Stallman, the founder of the GNU
Project. Richard Stallman founded the GNU project in 1984 to create a complete Unix-like
operating system as free software, to promote freedom and cooperation among computer users
and programmers.

Installing GCC on Windows


For Windows, you could either install Cygwin GCC, MinGW GCC or MinGW-W64 GCC.

 Cygwin GCC: Cygwin is a Unix-like environment and command-line interface for


Microsoft Windows. Cygwin is huge and includes most of the Unix tools and
utilities. It also included the commonly-used Bash shell.
 MinGW: MinGW (Minimalist GNU for Windows) is a port of the GNU Compiler
Collection (GCC) and GNU Binutils for use in Windows. It also included MSYS
(Minimal System), which is basically a Bourne shell (bash).
 MinGW-W64: a fork of MinGW that supports both 32-bit and 64-bit windows.

Compile/Link a Simple C Program - hello.c


Below is the Hello-world C program hello.c:

// hello.c
#include <stdio.h>

int main() {
printf("Hello, world!\n");
return 0;
}

To compile the hello.c:

>gcc hello.c
// Compile and link source file hello.c into executable a.exe (Windows) or a (Unixes)

The default output executable is called "a.exe" (Windows) or "a.out".


To run the program:

// (Windows) In CMD shell


>a

To specify the output filename, use -o option:

// (Windows) In CMD shell


>gcc -o hello.exe hello.c
// Compile and link source file hello.c into executable hello.exe
>hello
// Execute hello.exe under CMD shell

iii) Writing simple programs using printf(), scanf()

printf() and scanf() in C

The printf() and scanf() functions are used for input and output in C language. Both functions are
inbuilt library functions, defined in stdio.h (header file).

printf() function

The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

printf("format string",argument_list);

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function

The scanf() function is used for input. It reads the input data from the console.

scanf("format string",argument_list);
Program to print sum of 2 numbers
#include<stdio.h>
int main()
{
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}

Output

enter first number:9


enter second number:9
sum of 2 numbers:18

Week 2: Converting algorithms/flowcharts into C Source code

i) Sum and average of 3 numbers

ii) Conversion of Fahrenheit to Celsius and vice versa

iii) Simple interest calculation

i) Sum and average of 3 numbers

Algorithm:

Step 1: Start

Step 2: Declare a variable a,b,c and avg as int;

Step 3: Read two numbers a,b and c;

Step 4: avg=(a+b+c)/3;

Step 5: Print avg;

Step 6: Stop.

Flowchart
Program:

#include<stdio.h>

int main()

int a, b, c, avg; //Declaring the variables

clrscr();

printf("Enter three variables:\n");

printf("a:");

scanf("%d", & a); //Reading Variable a

printf("b:");

scanf("%d", & b); //Reading Variable b

printf("c:");
scanf("%d", & c); //Reading Variable c

avg = (a + b + c) / 3;

printf("\nAverage of %d,%d and %d is %d", a, b, c, avg);

getch();

return 0;

Output:

Enter three varaiables:

a: 25

b: 35

c: 30

Average of 25,35 and 30 is 30

ii) Conversion of Fahrenheit to Celsius and vice versa

a) Conversion of Fahrenheit to Celsius and vice versa.

Algorithm:

Step 1: Start

Step 2: Declare a variable F and C as float;

Step 2: Read the temperature in Fahrenheit F

Step 3: Convert the temperature to Celsius using the formula C = (F – 32)*5/9

Step 4: Print the temperature in Celsius C


Step 5: Stop

Flowchart:

Program:

#include<stdio.h>

int main()

float fahrenheit, celsius;

printf("\n Enter Fahrenheit: ");

scanf("%f",&fahrenheit);

celsius =(fahrenheit -32)*5/9;

printf("\n Celsius: %f ", celsius);

return0;

Output:
Enter Fahrenheit:

100

Celsius: 37.777779

b) Conversion of Celsius to Fahrenheit.

Algorithm:

Step 1: Start

Step 2: Declare a variable C and F as float;

Step 2: Read the temperature in Celsius C

Step 3: Convert the temperature Celsius to Fahrenheit using the formula F = (C * 9/5) + 32.

Step 4: Print the temperature in Fahrenheit F.

Step 5: Stop

Flowchart:
Program:

#include<stdio.h>

#include<conio.h>

voidmain()

float c, f;

printf("Enter the celsius:\n");

scanf("%f",&c);

f=(c*9/5)+32;

printf("\n Temperature in Fahrenheit is : %f",f);

getch();

Output
Enter the Celsius:

37

Temperature in Fahrenheit is :98.59999

iii) Simple interest calculation

Algorithm:

Step 1: Start

Step 2: Declare a variables P , N, R and SI as float;

Step 3: Read P, N and R Values

Step 4: SI = (P * N* R) /100.0

Step 5: Print SI

Step 6: Stop

Flowchart:
Program:

#include<stdio.h>

int main()

float principle, time, rate, SI;

/* Input principle, rate and time */

printf("Enter principle (amount): ");

scanf("%f", &principle);

printf("Enter time: ");

scanf("%f", &time);

printf("Enter rate: ");

scanf("%f", &rate);

/* Calculate simple interest */


SI = (principle * time * rate) / 100;

/* Print the resultant value of SI */

printf("Simple Interest = %f", SI);

return 0;

Output:

Enter principle (amount):1200

Enter time: 2

Enter rate: 5.4

Simple Interest = 129.600006

Week 3: Simple computational problems using arithmetic expressions

i) Find the square root of the given number.

Program:

#include <stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
double num;
printf("Enter a number : ");
scanf("%lf", &num);
printf("The square root is : %lf\n", sqrt(num));
getch();
return 0;
}

Output:
ii) Finding compound interest

Program:

#include <stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
double principle, rate, time, amount, CI;
printf("Enter the principle amount:\n ");
scanf("%lf",&principle);
printf("Enter the rate of interest:\n ");
scanf("%lf",&rate);
printf("Enter the time duration: \n");
scanf("%lf",&time);
amount = principle*(pow((1+rate/100),time));
CI = amount - principle;
printf(" The compound interest is: %lf",CI);
getch();
return 0;
}

Output:
iii) Area of a triangle using heron’s formulae

Program:

# include<stdio.h>

#include<conio.h>

# include <math.h>

int main()

float a,b,c,s,area;

printf("Enter the length of three sides of a triangle:\n");

scanf("%f%f%f", &a,&b,&c);

s = (a+b+c)/2;

printf("The value of S is %.2f\n",s);

area = sqrt(s*(s-a) * (s-b) * (s-c));

printf("The area of the triangle is:%f\n", area);

getch();
return 0;

Output:

iv) Distance travelled by an object

Program:

#include <stdio.h>

void main()

float u, a, t,dist;

printf("Enter the acceleration value : ");

scanf("%f",&a);

printf("Enter the initial velocity : ");

scanf("%f",&u);

printf("Enter the time taken : ");

scanf("%f",&t);

dist = (u * t) + (a * t * t) / 2;

printf("Distance travelled : %f\n", dist);

getch();

}
Output:

Week 4: Simple computational problems using the operator’ precedence and associativity

i) Evaluate the following expressions.


a. A+B*C+(D*E)+F*G
b. A/B*C-B+A*D/3
c. A+++B---A
d. J= (i++) +(++i)
ii) Find the maximum of three numbers using conditional operator
iii) Take marks of 5 subjects in integers, and find the total, average in float

i) Problem Statement Analysis


A=1, B=2, C=3, D=4, E=5, F=6, G=7, i = 3

a. A+B*C+(D*E)+F*G
= 1 + 2 * 3 + (4 * 5) + 6 * 7
= 1 + 2 * 3 + 20 + 6 * 7
= 1 + 6 + 20 + 6 * 7
= 1 + 6 + 20 + 42
= 7 + 20 + 42
= 27 + 42
= 69

b.A/B*C-B+A*D/3
= 1 / 2 * 3– 2 + 1 * 4 / 3
= 0 * 3 -2 + 1 * 4 / 3
=0-2+1*4/3
=0-2+4/3
= 0– 2 + 1
= -2 + 1
= -1

c. A+++B---A
= 1+++2---1
= 2+2---1
= 2+1-1
= 3-1
=2

d. J=(i++)+(++i)
J = (3++) + (++3)
J = (4) + (4)
J=8

Program:

#include<stdio.h>

#include<conio.h>

void main()

int A=1, B=2, C=3, D=4, E=5, F=6, G=7, i=3, J;

clrscr();

J = A+B*C+(D*E)+F*G;

printf("Result=%d\n",J);

J = A/B*C-B+A*D/3;

printf("Result=%d\n",J);

J = A+++B---A;

printf("Result=%d\n",J);

J = (i++)+(++i);

printf("Result = %d",J);

getch();

Output:
ii) Find the maximum of three numbers using conditional operator
Program:

# include <stdio.h>

#include<conio.h>

void main()

int a, b, c, big ;

printf("Enter three numbers : ") ;

scanf("%d %d %d", &a, &b, &c) ;

big = a >b ? (a >c ? a : c) : (b > c ? b : c) ;

printf("\nThe biggest number is : %d", big) ;

getch();

Output:

Case 1:

Case 2:

Case 3:
iii) Take marks of 5 subjects in integers, and find the total, average in float
Program:

#include <stdio.h>

#include<conio.h>

int main()

int English, Maths, Chemistry, Computer, Physics;

float total, average;

clrscr();

printf("Enter the marks of English, Maths, Chemistry, Computer and Physics: \n");

scanf("%d %d %d %d %d", &English, &Maths, &Chemistry, &Computer, &Physics);

total = English + Maths + Chemistry + Computer + Physics;

average = total / 5;

printf("Total Marks of the Student: %.2f\n", total);

printf("Average Marks of the Student: %.2f\n", average);

getch();

return 0;

Output:
Week 5: Problems using control statements

i) Write a C program to find the max and min of four numbers using if-else.

ii) Write a C program to generate electricity bill.

iii) Find the roots of the quadratic equation.

iv) Write a C program to simulate a calculator using switch case.

v) Write a C program to find the given year is a leap year or not

i) Write a C program to find the max and min of four numbers using if-else.

PROGRAM:

#include<stdio.h>

#include<conio.h>

int main()

int a, b, c, d,min,max;

clrscr();

printf("Enter 4 integers to find largest and smallest: ");

scanf("%d %d %d %d", &a, &b, &c, &d);

max = min = a;

if (b > max)

max = b;

else if (b < min)

min = b;

if (c > max)

max = c;

else if (c < min)


min = c;

if (d > max)

max = d;

else if (d < min)

min = d;

printf("max: %d min : %d\n", max, min);

getch();

return 0;

CASE 1:

OUTPUT:

CASE 2:

OUTPUT:

CASE 3:

OUTPUT:

CASE 4:

OUTPUT:
ii) Write a C program to generate electricity bill.

PROGRAM:

#include <stdio.h>

#include<conio.h>

int main()

int unit,serviceno;

char cname[20];

float amt, total_amt, sur_charge;

clrscr();

printf("\n Enter Service Number:");

scanf("%d",&serviceno);

printf("\n Enter Customer Name:");

scanf("%s",cname);

printf("Enter total units consumed: ");

scanf("%d", &unit);

if(unit <= 50)

amt = 20+unit * 2.50;

}
else if(unit <= 150)

amt = 40 + ((unit-50) * 4.00);

else if(unit <= 250)

amt = 100 + ((unit-150) * 5.20);

else

amt = 200 + ((unit-250) * 7.50);

sur_charge = amt * 0.20;

total_amt = amt + sur_charge;

printf("Electricity Bill = Rs. %.2f", total_amt);

getch();

return 0;

CASE 1:

OUTPUT:
CASE 2:

OUTPUT:

CASE 3:

OUTPUT:

CASE 4:

OUTPUT:
iii) Find the roots of the quadratic equation.

PROGRAM:

#include <math.h>

#include <stdio.h>

#include<conio.h>

int main() {

double a, b, c, discriminant, root1, root2, realPart, imagPart;

clrscr();

printf("Enter coefficients a, b and c: ");


scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("root1 = %.2lf and root2 = %.2lf", root1, root2);

else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("root1 = root2 = %.2lf;", root1);

else {

realPart = -b / (2 * a);

imagPart = sqrt(-discriminant) / (2 * a);

printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);

getch();

return 0;

CASE 1:

OUTPUT:

CASE 2:

OUTPUT:
iv) Write a C program to simulate a calculator using switch case

PROGRAM:

#include <stdio.h>

#include<conio.h>

int main()
{

int num1,num2;

float result;

char ch; //to store operator choice

clrscr();

printf("Enter first number: ");

scanf("%d",&num1);

printf("Enter second number: ");

scanf("%d",&num2);

printf("Choose operation to perform (+,-,*,/,%): ");

scanf(" %c",&ch);

result=0;

switch(ch)

case '+':

result=num1+num2;

break;

case '-':

result=num1-num2;

break;

case '*':

result=num1*num2;

break;

case '/':

result=(float)num1/(float)num2;

break;
case '%':

result=num1%num2;

break;

default:

printf("Invalid operation.\n");

printf("Result: %d %c %d = %f\n",num1,ch,num2,result);

getch();

return 0;

CASE 1:

OUTPUT:

CASE 2:

OUTPUT:

CASE 3:

OUTPUT:
CASE 4:

OUTPUT:

CASE 5:

OUTPUT:

CASE 6:

OUTPUT:

v) Write a C program to find the given year is a leap year or not.


PROGRAM:

#include <stdio.h>

#include<conio.h>

int main() {

int year;

clrscr();

printf("Enter a year: ");

scanf("%d", &year);

if (year % 400 == 0) {

printf("%d is a leap year.", year);

else if (year % 100 == 0)

printf("%d is not a leap year.", year);

else if (year % 4 == 0)

printf("%d is a leap year.", year);

else

printf("%d is not a leap year.", year);

getch();

return 0;

}
CASE 1:

OUTPUT:

CASE 2:

OUTPUT:

Week 6

i) Find the factorial of given number using any loop in C

#include<stdio.h>
int main(){
int x,fact=1,n;
printf("Enter a number to find factorial: ");
scanf("%d",&n);
for(x=1;x<=n;x++)
fact=fact*x;
printf("Factorial of %d is: %d",n,fact);
return 0;
}
Output: Enter a number to find factorial:6
factorial of 6 is:720

ii) Find the given number is a prime or not in C


#include <stdio.h>

int main() {

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

// 0 and 1 are not prime numbers


// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;

for (i = 2; i <= n / 2; ++i) {

// if n is divisible by i, then n is not prime


// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}

// flag is 0 for prime numbers


if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}
Output:

Enter a positive integer: 29


29 is a prime number.

iii) Construct a pyramid of numbers in C

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

printf("Enter number of rows: ");// enter a number for generating the pyramid
scanf("%d",&rows);

for(i=0; i<rows; i++) // outer loop for displaying rows


{
for(space=1; space <= rows-i; space++) // space for each and every element
printf(" ");

for(j=0-i; j <= i; j++) //inner loop for displaying the pyramid of numbers
{

printf("%2d",abs(j)); // prints the value

}
printf("\n"); // every line in different row
}

}
Output:
Enter number of rows: 5
0
101
21012
3210123
432101234

Week 7

i) Find the min and max of a 1-D integer array in C


#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;
}
Output:
Enter size of the array: 5
Enter elements in array: 1
2
3
4
5
minimum of an array is: 1
maximum of an array is: 5

ii) Find 2’s complement of the given binary number in C


#include<stdio.h>
#include<stdlib.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("Two'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

Week 8
i) Multiplication two matrices in C
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;

printf("Enter the value of N (N <= 10): ");


scanf("%d", & n);
printf("Enter the elements of Matrix-A: \n");

for (i = 0; i < n; i++) {


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

printf("Enter the elements of Matrix-B: \n");

for (i = 0; i < n; i++) {


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

for (i = 0; i < n; i++) {


for (j = 0; j < n; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}

printf("The product of the two matrices is: \n");


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}

Output:

Enter the value of N (N <= 10): 2


Enter the elements of Matrix-A:
22
22
Enter the elements of Matrix-B:
22
22
Product of the two matrices is :
8 8
8 8

Week 9

Write a C program to find the total, average of n students using structures

#include<stdio.h>int

main() {

int student = 0, sum = 0, marks = 0, sub;clrscr();

for (student = 0; student < 3; student++) {sum =

0;

printf("\n Enter Marks for Student - %d ", student + 1);for (sub

= 0; sub < 3; sub++) {

printf("\nEnter Marks for Subject - %d ", sub + 1);

scanf("%d", & marks);

sum = sum + marks;

printf("\n For Student - %d : ", student + 1);

printf("\n Sum = %d", sum);

printf("\n Average = %.2f", ((float) sum) / sub);

return 0;

}
Output:

Enter marks for student 1

Enter marks for subject 1 36

Enter marks for subject 2 37

Enter marks for subject 3 38

For student 1

sum=111

Average=37.00

Enter marks for student 2

Enter marks for subject 1 37

Enter marks for subject 2 37

Enter marks for subject 3 38

For student 2

sum=112

Average=37.33

Enter marks for student 3

Enter marks for subject 1 39

Enter marks for subject 2 38


Enter marks for subject 3 38

For student 3

sum=115

Average=38.33

Week 10

Write a C program to find transpose of a matrix using function.

#include <stdio.h>int

main() {

int a[10][10], transpose[10][10], r, c;

printf("Enter rows and columns: "); scanf("%d

%d", &r, &c);

// asssigning elements to the matrix

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

(int i = 0; i < r; ++i)

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

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

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

// printing the matrix a[][]

printf("\nEntered matrix: \n");for

(int i = 0; i < r; ++i)

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

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

if (j == c - 1)
printf("\n");

// computing the transposefor

(int i = 0; i < r; ++i) for (int j

= 0; j < c; ++j) {

transpose[j][i] = a[i][j];

// printing the transpose printf("\nTranspose

of the matrix:\n");for (int i = 0; i < c; ++i)

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

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

== r - 1)

printf("\n");

return 0;

}
Output:

Enter rows and columns: 23

Enter matrix elements:

Enter element a11: 1

Enter element a12: 4

Enter element a13: 0

Enter element a21: -5

Enter element a22: 2

Enter element a23: 7

Entered matrix:

14 0

-5 2 7

Transpose of the matrix:

1 -5

4 2

0 7

Week 11

i) Program to find fibonacci series using recursion in C

#include<stdio.h>

void printFibonacci(int n){

static int n1=0,n2=1,n3;


if(n>0){

n3 = n1 + n2;

n1 = n2;

n2 = n3;

printf("%d ",n3);

printFibonacci(n-1);

int main(){

int n;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("Fibonacci Series: ");

printf("%d %d ",0,1);

printFibonacci(n-2);//n-2 because 2 numbers are already printed

return 0;

}
Output:

Enter the number of elements:15

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

ii) Program to find factorial of a number using recursion in C

#include<stdio.h>

int fact(int);

int main()

int x,n;

printf(" Enter the Number to Find Factorial :"); scanf("%d",&n);

x=fact(n);

printf(" Factorial of %d is %d",n,x); return 0;

int fact(int n)

if(n==0) return(1);

return(n*fact(n-1));

}
Output:

Enter the Number to Find Factorial :5

Factorial of 5 is 120

Week 12

Write a C program to find no of lowercase, uppercase, digits and other characters using pointers.

#include <stdio.h>

#include <stdlib.h>

int main()

char str[100];

int i;

int upper=0,lower=0,num=0,special=0;;

printf("Please enter the string \n");

gets(str);

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

//check for uppercase

if(str[i]>='A' && str[i]<='Z') {

upper++;

}else if(str[i]>='a' && str[i]<='z') {//check lower case


lower++;

}else if(str[i]>='1' && str[i]<='9') { //check number

num++;

else{

special++;

printf("\nUpper case letters: %d",upper);

printf("\nLower case letters: %d",lower);

printf("\nNumbers: %d",num);

printf("\nSpecial characters: %d",special);

getch();

return 0;

Output:

Please enter the string

To_Learn_Code:code4coding.com
Upper case letters: 3

Lower case letters: 21

Numbers: 1

Special characters: 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy