You are on page 1of 40

Rohan kumar (IT 2022-2026)

PRACTICAL - 01

AIM : Introduction to C and installing VScode.

Introduction to C language
C is a procedural programming language initially developed by Dennis Ritchie in the year
1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming
language to write the UNIX operating system.

The main features of the C language include:


• General Purpose and Portable
• Low-level Memory Access
• Fast Speed
• Clean Syntax

These features make the C language suitable for system programmings like an operating
system or compiler development.

Many later languages have borrowed syntax/features directly or indirectly from the C
language. Like syntax of Java, PHP, JavaScript, and many other languages are mainly based
on the C language. C++ is nearly a superset of C language (Only a few programs may
compile in C, but not in C++).
So, if a person learns C programming first, it will help him to learn any modern
programming language as well. As learning C help to understand a lot of the underlying
architecture of the operating system. Like pointers, working with memory locations, etc.
Installing Visual Studio Code

Steps to install VS Code in Windows 10.

1. Download the executable file from the link below.

https://code.visualstudio.com/

2. Click the option download.

3. Double click the downloaded file.

4. Now a dialogue box appears.

5. Select I accept the agreement.

6. Then select Next.

Rohan kumar (IT 2022-2026)


7. Select a folder by clicking Browse or just follow the default path.

8. Then select Next.

Rohan kumar (IT 2022-2026)


7. Select a folder by clicking Browse or just follow the default path.

8. Then select Next.

Rohan kumar (IT 2022-2026)


9. Select the required options as per your need by clicking in the checkbox.

10. Then select Next.

11. Select :Install

Rohan kumar(IT 2022-2026)


12.Wait a bit while it gets installed (The green color fills the bar).

13.Click Finish to exit Setup. Check in the check box to launch VS Code right now.

Rohan kumar(IT 2022-2026)


14.Congratulation! VS Code got installed in your system successfully.

15.Now a new dialogue box appears. This is VS Code IDE.

16. Click New file to open a new file.

17.All done!

18. Happy Coding!

Rohan kumar (IT 2022-2026)


PRACTICAL – 02

AIM : Running First C program.

PROGRAM

#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL – 03

AIM : WAP to show the sum of three numbers.

PROGRAM

#include <stdio.h>
int main() {
float a,b,c;
printf("enter first number\n");
scanf("%f",&a);
printf("enter second number\n");
scanf("%f",&b);
printf("enter third number\n");
scanf("%f",&c);
printf("sum of three numbers = %f\n",a+b+c);
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL – 04

AIM : WAP to compute the Ascii value of a character.

PROGRAM

#include <stdio.h>
int main() {
char c;
printf("Enter a character :");
scanf("%c", &c);
printf("ASCII value of %c = %d \n", c, c);
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL – 05

AIM : WAP to take multiple inputs from the user.

PROGRAM
#include <stdio.h>
int main() {
int p,t;
float r;
printf("enter principle amount:");
scanf("%d",&p);
printf("enter rate of interest:");
scanf("%f",&r);
printf("enter time period:");
scanf("%d",&t);
printf("simple interest is: %f\n",(p*r*t)/100);
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL –06

AIM : WAP to use getchar() and putchar() functions.

PROGRAM

#include <stdio.h>
int main() {
char ch;
printf("enter a character:");
ch=getchar();
printf("The character entered is:");
putchar(ch);
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL – 07

AIM : WAP to use gets() and puts() functions.

PROGRAM
#include <stdio.h>
#include <string.h>
int main() {
char name[15];
printf("enter name:");
gets(name);
printf("The name entered is:");
puts(name);
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL – 08

AIM : Write an algorithm and draw a flowchart to determine a student’s


final grade and indicate whether it is passing or failing. The final
grade is calculated as the average of four marks.

PROGRAM

#include<stdio.h>
int main() {
float a,b,c,d,e;
printf("enter marks of science:");
scanf("%f",&a);
printf("enter marks of history:");
scanf("%f",&b);
printf("enter marks of computer:");
scanf("%f",&c);
printf("enter marks of maths:");
scanf("%f",&d);
e=(a+b+c+d)/4;
printf("student aggregate marks = %f\n",e);
if(e>=33)
{
printf("student passed\n");
}
else
{
printf("student failed\n");
}
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


FLOWCHART:
START

Input marks of four


subjects

Avg=(sub1+sub2+sub3+
sub4)/4

yes
Avg>=40

No

Display ‘’PASS’’
Display ‘’FAIL’’

END
PRACTICAL – 8(ii)

AIM : Write an algorithm, pseudocode, and draw a flowchart that will


read the two sides of a rectangle and calculate its area.

PROGRAM

#include <stdio.h>
int main() {
float l,b;
printf("Enter length of rectangle:");
scanf("%f",&l);
printf("Enter breadth of rectangle:");
scanf("%f",&b);
printf("area of rectangle is = %f ",l*b);
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


START

Input, Length,
Breadth

Area =Length * Breadth

Print Area

END
PRACTICAL –09

AIM : WAP to find the max from 3 numbers.

PROGRAM

#include <stdio.h>
int main() {
float a,b,c;
printf("Enter First number:");
scanf("%f",&a);
printf("Enter Second number:");
scanf("%f",&b);
printf("Enter Third number:");
scanf("%f",&c);
if(a>b && a>c)
{
printf("Maximun number is %f",a);
}
else if(b>a && b>c)
{
printf("Maximun number is %f",b);
}
else if(c>b && c>a)
{
printf("Maximun number is %f",c);
}
else
{
printf("input error");
}
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL –10

AIM : Write a program to take the marks of a student and display fail if
marks is less than 40% otherwise display The Student is PASS.

PROGRAM

#include<stdio.h>
int main() {
float marks;
printf("enter the marks(%) of a student:");
scanf("%f",&marks);
if(marks<40)
{
printf("The Student is FAIL");
}
else
{
printf("The Student is PASS");
}
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL –11

AIM : WAP to use switch statement to print Monday to Sunday.

PROGRAM

#include <stdio.h>
int main() {
int day;
printf("Enter Day Number (like monday =1 to sunday =7)\n");
scanf("%d", &day);

switch(day){
case 1 : printf("Monday\n");
break;
case 2 : printf("Tuesday\n");
break;
case 3 : printf("Wednesday\n");
break;
case 4 : printf("Thursday\n");
break;
case 5 : printf("Friday\n");
break;
case 6 : printf("Saturday\n");
break;
case 7 : printf("Sunday\n");
break;
default: printf("wrong input!\n");
}
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


PRACTICAL –12

AIM : WAP to print whether a given no is even or odd.

PROGRAM

#include<stdio.h>
int main() {
int a;
printf("enter any number:");
scanf("%d",&a);
if(a%2 == 0)
{
printf("The number is Even");
}
else
{
printf("The number is odd");
}
return 0;
}

OUTPUT

Rohan kumar (IT 2022-2026)


13
PRACTICAL –26
AIM :Write a C program to input electricity unit charge
and calculate the 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.

How to calculate electricity bill using if else in C


programming. Program to find electricity bill using
if else in C. Logic to find net electricity bill in C program.
PROGRAM

#include <stdio.h>

int main()
{
int unit;
float amt, total_amt, sur_charge;

/* Input unit consumed from user */


printf("Enter total units consumed: ");
scanf("%d", &unit);

/* Calculate electricity bill according to given conditions */


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);

return 0;
}
OUTPUT
PRACTICAL –14

AIM : Write a programe to check whether a number is


prime or not.

PROGRAM :
#include <stdio.h>
int main()
{
int num, c;
printf("enter the number:");
scanf("%d", &num);
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
c++;
}
}
if (c == 2)
{
printf("the enter number is a prime number\n");
}
else
{
printf(" enter number is not a prime number\n");
}

return 0;
}
OUTPUT :

Rohan kumar (IT 2022-2026)


PRACTICAL –15

AIM : Write a programe to print positive integers from 1 to


10.

PROGRAM :
#include <stdio.h>
int main(){
int a;
for(a=1;a<=10;a++){
printf("%d\n", a);
}
return 0;
}

OUTPUT :

Rohan kumar (IT 2022-2026)


PRACTICAL –16

AIM : Write a program to generate Fibonacci series .

PROGRAM
#include <stdio.h>
int main()
{

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

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


scanf("%d", &n);

printf("Fibonacci Series: %d, %d, ", t1, t2);

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


{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

OUTPUT
PRACTICAL –17

AIM :Write a program to reverse a given integer .

PROGRAM
#include <stdio.h>

int main() {

int n, reverse = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}

printf("Reversed number = %d", reverse);

return 0;
}

OUTPUT
PRACTICAL –18

AIM :Write a program to check whether a number is palindrome


or not.

PROGRAM
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}

OUTPUT
PRACTICAL –19

AIM :Write a program to calculate factorial of a number using


recursion and iteration method .

PROGRAM- by recursion
#include <stdio.h>
int factorial(int a);

int main()
{

int a;
printf("enter the value\n ");
scanf("%d", &a);
printf("the value of factorial %d is %d\n", a, factorial(a));
return 0;
}
int factorial(int a)
{
printf("the calling value is %d\n", a);
if (a == 1 || a == 0)
{
return 1;
}
else
{
return a * factorial(a - 1);
}
}

OUTPUT
PRACTICAL –19

AIM :Write a program to calculate factorial of a number using


recursion and iteration method .

PROGRAM- by iteration method


#include <stdio.h>

int main()
{

int num;
printf("enter the number\n");
scanf("%d", &num);
int res = 1, i;
for (i = 2; i <= num; i++)
{
res *= i;
}

printf("Factorial of %d is %d", num, res);


return 0;
}

OUTPUT
PRACTICAL –20

AIM :Write a program to print the triangle of stars as


follows(taken number of lines from user).

PROGRAM
#include <stdio.h>

int main()
{
int i, j, rows;
printf("Enter the number of rows\n");
scanf("%d", &rows);

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


{
/* Prints one row of triangle */
for (j = 1; j <= i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}

OUTPUT
PRACTICAL –21

AIM :Write a program to insert 5 elements into an array and


display the elements of the array.

PROGRAM
#include <stdio.h>
int main()
{
int a[4], i ;
printf("\n Enter the Array Element : \n") ;
for ( i=0 ; i<=4; i++)
{
scanf("%d", &a[i]);
}
printf("\n Array Elements are : \n ") ;
for ( i=0 ; i<=4 ; i++)
{
printf("\t %d ",a[i]) ;
}
return 0;
}

OUTPUT
PRACTICAL –22

AIM :Write a program to display the reverse of an array.

PROGRAM
#include <stdio.h>
int main()
{
int num, i, j, array1[50], array2[50];
printf("Enter no of elements in array\n");
scanf("%d", &num);

printf("Enter array elements\n");


for (i = 0; i < num; i++)
scanf("%d", &array1[i]);

for (i = num - 1, j = 0; i >= 0; i--, j++)


array2[j] = array1[i];

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


array1[i] = array2[i];
printf("The reversed array:\n");
for (i = 0; i < num; i++)
printf("%d\n", array1[i]);
return 0;
}

OUTPUT
PRACTICAL –23

AIM :Write a c program to display max elements of an array.

PROGRAM
#include <stdio.h>

int main()
{
int array[100], maximum, size, c, location = 1;

printf("Enter the number of elements in array\n");


scanf("%d", &size);

printf("Enter %d integers\n", size);

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


scanf("%d", &array[c]);

maximum = array[0];

for (c = 1; c < size; c++)


{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}

printf("Maximum element is present at location %d and it's value is


%d.\n", location, maximum);
return 0;
}

OUTPUT
PRACTICAL –24

AIM :Write a program to access an element in 2-D array.

PROGRAM
#include<stdio.h>

int main() {
int i, j, a[3][3];

// i : For Counting Rows


// j : For Counting Columns

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


for (j = 0; j < 3; j++) {
printf("\nEnter the a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}

//Print array elements


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

return 0;
}

OUTPUT
PRACTICAL –25

AIM :Write a program to find the sum of the element a matrix.

PROGRAM

#include<stdio.h>
int main()
{
int i,j,m,n;
float a[10][10], sum=0.0;

printf("Enter row and column size:\n");


scanf("%d%d", &m, &n);
printf("Enter matrix elements:\n");
for(i=0;i< m;i++){
for(j=0;j< n;j++){

printf("a[%d][%d]=",i,j);
scanf("%f", &a[i][j]);}
}
for(i=0;i< m;i++){
for(j=0;j< n;j++){
sum = sum + a[i][j];}
}
printf("Sum = %f\n", sum);

return 0;
}

OUTPUT
PRACTICAL –26
AIM :Write a program to find the transpose of the element a
matrix.
PROGRAM
#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 transpose


for (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
PRACTICAL –27
AIM :Write a program to multiply two 3 X 3 matrices.

PROGRAM
#include<stdio.h>

int main()
{
int i,j,k;
float a[3][3], b[3][3], mul[3][3];

printf("Enter elements of first matrix:\n");


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

printf("Enter elements of second matrix:\n");


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

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

printf("Multiplied matrix is:\n");


for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
printf("%f\t", mul[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT

You might also like