You are on page 1of 54

E-Journal Lab

Rollno : B21616                                                                                    Page no: 01

BASIC PROGRAMMING CONSTRUCTS


Expt No. 02     Date: 28/10/2021

Aim: Programs using the concepts of variables, operators and datatypes.

 2.1          To
calculate the area and perimeter of rectangle and area and        
circumference of a circle. The length and breadth of the rectangle and radius of
the circle are input through the keyboard.

2.2          Temperature of a city degree Fahrenheit is entered through the keyboard. Write
a C program to convert this temperature to degree Celsius.

2.3          Calculation of simple interest

2.4          Calculation of volume of sphere.

Flowchart/Algorithm:
                                   
        
C Program
 Source Code:

         Exp:2.1
#include<stdio.h>
int main()
{
    //declare variable
    int l, b, AeraR=0, Peri=0;
    float r, AreaC=0.0, circm=0.0;

    //input
    printf("\n Enter the value of length, breadth of the rectangle \n");
    scanf("%d %d",&l, &b);
    printf("\n Enter the value of radius of the circle \n");
    scanf("%f",&r);

    //process
    AeraR=l*b;
    Peri=2*(l+b);
    AreaC=3.14*r*r;
    circm=3.14*r;

    //output
    printf("\n\n Area of rectangle with length =%d breadth =%d is %d",l,b,AeraR);
    printf("\t and perimeter of rectangle is %d \n\n",Peri);
    printf("\n\n Area of circle with radius=%f is %f",r,AreaC);
    printf("\t and circumference of circle is %f",circm);
    return 0;
}

Exp:2.2

/* Fahrenheit to Celsius
*/
#include<stdio.h>
int main()
{
    float c, f;
     printf("enter temperature in Fahrenheit \n");
    scanf("%f",&f);
    c=(f-32)*5/9;

    printf("\n %f Fahrenheit = %f Celsius", f,c);


    return 0;
}

Exp:2.3

/* Simple interest
*/
#include<stdio.h>
int main()
{
    int n;
    float p, r, si;
     printf("enter numbers of years \n\n");
    scanf("%d",&n);
    printf("enter Principal Amount \n\n");
    scanf("%f",&p);
    printf("enter Rate of Interest \n\n");
    scanf("%f",&r);
    si= (n* p* r)/100;
    printf("Simple Interest = %f",si);
    return 0;
}

Exp:2.4
/* To volume of sphere
*/
#include<stdio.h>
#define PI 3.14
int main()
{
    float r,v;
     printf("enter radius of sphere \n\n");
    scanf("%f",&r);
    v=(4 * PI * r * r * r)/3;
    printf("Volume of sphere= %f",v);
    return 0;
}

Output:

Exp:2.1
O/P:
Enter the value of length, breadth of the rectangle
12  4

 Enter the value of radius of the circle


2

 Area of rectangle with length =12 breadth =4 is 48      and perimeter of rectangle is 32

Exp:2.2
O/P:
enter temperature in Fahrenheit
77

 77.000000 Fahrenheit = 25.000000 Celsius

Exp:2.3
O/P:
enter numbers of years

5
enter Principal Amount

100000
enter Rate of Interest

3.25
Simple Interest = 16250.000000

Exp:2.4
O/P:
enter radius of sphere

5
Volume of sphere= 523.333313

BASIC PROGRAMMING CONSTRUCTS


Expt No.3                                                                            Date:
7/10/2021

Aim: Programs using the concept of variables and operators.

3.1        To interchange the values of any two variables.

3.2        To print the square root of any given variable.


3.3         Given a number X. The task is to find the value of the below expression for the
given value of X.

   (X +1) +(X +1)


6 6

3.4      Print the maximum number given any two numbers.

Flowchart/Algorithm:

C Program
 Source Code:
3.1

/* To interchange
*/
#include<stdio.h>
int main()
{
    int a,b,t=0;
    printf("to interchange the value of any to numbers \n");
    printf("Enter the value of a \n");
    scanf("%d",&a);
    printf("Enter the value of b \n");
    scanf("%d",&b);
    printf("\n before interchange the value a= %d and the value of b=%d ",a,b);
    t=a;
    a=b;
    b=t;
    printf("\n\n after interchange the value of a =%d and the value of b =%d",a,b);
    return 0;
}

3.2
#include<stdio.h>
#include<math.h>
int main()
{
    float a,sr;
     printf("to find the square root of any number\n");
    printf("Enter the value of a\n");
    scanf("%f",&a);
    sr=sqrt(a);
     printf("\nthe square root of %f is = %f",a,sr);
    return 0;
}

3.3

#include<stdio.h>
#include<math.h>
int main()
{
    float a,x,r1,r2,v;
     printf("find the value of the below expression for the given value of X\n");
    printf("Enter the value of X\n");
    scanf("%f",&a);
    x=sqrt(a);
    r1=pow(x+1,6);
    r2=pow(x+1,6);
    v=r1+r2;
     printf("\nthe value of x is %f and the answers is = %f",a,v);
    return 0;
}

3.4
#include<stdio.h>
int main()
{
    float a,b;
     printf("Print the maximum number given any two numbers\n");
    printf("Enter the first number\n");
    scanf("%f",&a);
    printf("Enter the second number\n");
    scanf("%f",&b);
    (a>b)?printf ("%f is a maximum number\n",a):printf ("%f is a maximum number\n",b);
    return 0;
}

Output:
3.1
O/P:

to interchange the value of any to numbers


Enter the value of a
2
Enter the value of b
4

 before interchange the value a= 2 and the value of b=4

 after interchange the value of a =4 and the value of b =2

3.2
O/P:
to find the square root of any number
Enter the value of a
4

the square root of 4.000000 is = 2.000000


3.3
O/P:

find the value of the below expression for the given value of X

Enter the value of X

the value of x is 9.000000 and the answers is = 8192.000000

3.4
O/P:
Print the maximum number given any two numbers
Enter the first number
25
Enter the second number
40
40.000000 is a maximum number

 DECISION MAKING STATEMENTS


Expt No.4                                                                           
Date:14/10/2021

Aim: C Programs using if-else statement and conditional operator.

4.1        Write a C program that will print whether a given number is positive, negative or
zero using a conditional operator.

4.2 Write a C program that prints appropriate messages on the screen based
on user input character.

If Message
pressed...

A Excellent

B Good
C Fair

D Poor

4.3 A company insures its drivers in the following cases:

·      If the driver is married   m  


·      If the driver is unmarried u , male M and above 30yrs of age. 
·      If the driver is unmarried, female and above 25yrs of age.
In all other cases, the driver is not insured. If the marital_status, gender and
age of the driver are inputs. 

Write a C program to determine whether the driver is insured or not.

Flowchart/Algorithm:

C Program
 Source Code:
4.1
#include<stdio.h>
int main()
{
    float a,b;
     printf("print whether a given number is positive, negative or zero\n");
    printf("Enter the first number\n");
    scanf("%f",&a);
    (a>0)?printf ("%f is a positive number\n",a):(a<0)?printf ("%f is a negative number\n",a):printf ("%f
is a zero\n",a);
    return 0;
}

4.2
#include<stdio.h>
int main()
{
    char a;
     printf("Print the appropriate messages on the screen based on user input character\n");
    printf("Enter the grade between \'A\' to \'D\'\n");
    scanf("%c",&a);

    if(a=='A')
    {
       printf("Excellent");
    }
    else if(a=='B')
    {
       printf("Good");
    }
     else if(a=='C')
    {
       printf("Fair");
    }
    else if(a=='D')
    {
       printf("Poor");
    }
    else
    {
       printf("Enter the appropriate grade");
    }
    return 0;
}

4.3
#include<stdio.h>
int main()
{
    char marital_status,gender;
    int age;
     printf("To check if driver is married or unmarried \n");
    printf("Enter the marital status \'m\'-> Married and \'u\'-> Unmarried\n");
    scanf("%c",&marital_status);
    printf("\nEnter the gender \'M\'-> Male and \'F\'-> Female\n");
    scanf(" %c",&gender);
    printf("\nEnter the age\n");
    scanf("%d",&age);
    if(marital_status=='m')
    {
        printf("\nDriver is Married");
        printf("\nthe driver is insured");
    }
    else if(marital_status=='u' && gender =='M' && age >=30)
    {
       printf("\ndriver is Unmarried , Male and above 30yrs of age");
       printf("\nthe driver is insured");
    }
    else if(marital_status=='u' && gender =='F' && age >=25)
    {
       printf("\n driver is Unmarried , Female and above 25yrs of age");
       printf("\nthe driver is insured");
    }
    else
    {
      printf("\nthe driver is not insured");
    }
     return 0;
}
Output:

4.1
O/P:
print whether a given number is positive, negative or zero
Enter the first number
4
4.000000 is a positive number

4.2
O/P:

Print the appropriate messages on the screen based on user input character

Enter the grade between 'A' to 'D'

Excellent

4.3
O/P:

To check if driver is married or unmarried


Enter the marital status 'm'-> Married and 'u'-> Unmarried
u

Enter the gender '0'-> Male and '1'-> Female


M

Enter the age


34

driver is Unmarried , Male and above 30yrs of age


the driver is insured
         DECISION MAKING STATEMENTS
Expt No. 05   Date: 11/11/2021

Aim: C Programs using  if-else statements.

5.1        Write a C program that will print whether a given number is even or odd.
5.2 Write a C program to check divisibility of a number entered by the user.

5.3 Write a C program to determine whether the entered character is a vowel or a


consonant.

5.4 Write a C program that prints the smallest one given any 3 different numbers.

5.5 To print grade by user input marks:

Marks Message

Less than 40 Failed

40-60 Pass

60-80 First Class

80-100 Distinction
Any Other Invalid Marks Input

5.6 Write a C program to calculate the root of a Quadratic Equation.

5.7 Write a C program to accept a coordinate point in a XY coordinate


system and determine in which quadrant the coordinate point lies.

Flowchart/Algorithm:

C Program
 Source Code: 
5.1
#include<stdio.h>
int main()
{
    int a;
     printf("to check if a given number is even or odd\n");
    printf("Enter a number\n");
    scanf("%d",&a);

    if(a%2==0)
    {
       printf("%d is a even number",a);
    }
    else
    {
       printf("%d is a odd number",a);
    }

    return 0;
}

_________________________________________________________________________
___

5.2
#include<stdio.h>
int main()
{
    int a,b;
     printf("to check divisibility of a number entered by the user\n");
    printf("Enter a Dividend \n");
    scanf("%d",&a);
     printf("Enter a Divisor number\n");
    scanf("%d",&b);

    if(a%b==0)
    {
       printf("%d is divisible by %d",a,b);
    }
    else
    {
       printf("%d is not divisibility by %d",a,b);
    }

    return 0;
}

_________________________________________________________________________
___

5.3
#include<stdio.h>
int main()
{
    char a;
     printf("to check if a character is vowel or a consonant\n");
    printf("Enter a character \n");
    scanf("%c",&a);

    if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u'||a=='A'||a=='E'||a=='I'||a=='O'||a=='U')
    {
       printf("vowel");
    }
    else
    {
       printf("consonant");
    }
    return 0;
}

_________________________________________________________________________
_

5.4

 #include<stdio.h>
int main()
{
    int a,b,c;
     printf("to check the number is smallest one given any 3 different number\n");
    printf("Enter a first number \n");
    scanf("%d",&a);
    printf("Enter a second number \n");
    scanf("%d",&b);
    printf("Enter a third number \n");
    scanf("%d",&c);
    if(a<b)
    {
      if(a<c)
      {
          printf("%d is smallest",a);
      }
    }
    else
    {
        if(b<c)
        {
            printf("%d is smallest",b);
        }
        else
        {
            printf("%d is smallest",c);
        }
    }
    return 0;
}

______________________________________________________________________
5.5
#include<stdio.h>
int main()
{
    int a;
     printf("To check the grade\n");
    printf("Enter marks out of 100\n");
    scanf("%d",&a);

    if(a<=40)
    {
       printf("Fail");
    }
    else if(a<=60&&a>40)
    {
       printf("Pass");
    }
     else if(a<=80&&a>60)
    {
       printf("First Class");
    }
    else if(a<=100&&a>80)
    {
       printf("Distinction");
    }
    else
    {
       printf("Enter the appropriate marks");
    }
    return 0;
}

_________________________________________________________________________
5.6
#include <math.h>
#include <stdio.h>
int main() {
    float a, b, c, d, r1, r2;
    printf(" to calculate the root of a Quadratic Equation\n");
    printf("Enter coefficients a: ");
    scanf("%f",&a);
     printf("Enter coefficients b:");
    scanf("%f",&b);
     printf("Enter coefficients c: ");
    scanf("%f",&c);

    d =(b * b) - (4 * a * c);

    if (d > 0) {
        r1 = (-b - sqrt(d)) / (2 * a);
        r2 = (-b +  sqrt(d)) / (2 * a);
        printf("root1 = %.2f and root2 = %.2f", r1, r2);
    }

    else if (d == 0) {
        r1 = -b / (2 * a);
        printf("root1 = root2 = %.2f;", r1);
    }

    else {
        printf("then no real roots exist");
    }

    return 0;
}

_________________________________________________________________________
__

5.7
#include<stdio.h>
int main()
{
    int x,y;
     printf("in which quadrant the coordinate point lies\n");
    printf("Enter the x quadrant:");
    scanf("%d",&x);
    printf("Enter the y quadrant:");
    scanf("%d",&y);

    if(x>0 && y>0)


    {
       printf("quadrant first");
    }
    else if(x>0 && y<0)
    {
       printf("quadrant second");
    }
     else if(x<0 && y<0)
    {
       printf("quadrant third");
    }
    else
    {
       printf("quadrant forth");
    }

    return 0;
}

Output:

Exp:5.1
O/P:
to check if a given number is even or odd
Enter a number
4
4 is a even number

Exp:5.2
O/P:
to check divisibility of a number entered by the user
Enter a Dividend
10
Enter a Divisor number
5
10 is divisible by 5

    Exp:5.3
O/P:
to check if a character is vowel or a consonant
Enter a character
a
vowel

Exp:5.4 
O/P:
to check the number is smallest one given any 3 diffiernt number
Enter a first number
4
Enter a second number
2
Enter a third number
3
2 is smallest

Exp:5.5
O/P:
To check the grade
Enter marks out of 100
99
Distinction

Exp:5.6:
O/P:
 to calculate the root of a Quadratic Equation
Enter coefficients a: 1
Enter coefficients b:-7
Enter coefficients c: -60
root1 = -5.00 and root2 = 12.00

Exp:5.7

O/P:
in which quadrant the coordinate point lies
Enter the x quadrant:2
Enter the y quadrant:-4
quadrant second
DECISION MAKING AND LOOPING
Expt No.6                                                                              Date: 15/11/2021

ROLLNO:B21616

Aim: Programs using loops.

6.1    Write a C program to print the sum and reverse of any given 4-digit number
using a while loop.

6.2    Write a C program to generate n numbers starting from 0 to n using a do-


while loop.

6.3     Write a C program to print the following series for N>1 using a for loop:

6.4    Using nested loops print the following patterns:

a.    1                    b.  5                    c. $$$$$


12           44               $$$$
1 2 3                333                $$$
1234        2222                $$
1 2 3 4 5            1 1 1 1 1                        $
Flowchart/Algorithm:

C Program
 Source Code: 

6.1
#include<stdio.h>

int main()
{
    int R,rev=0,n,sum=0;
    printf("enter the number to print sum\n");
    scanf("%d",&n);
     while(n!=0)
    {
        int R=n%10;
        sum=sum+R;
        rev=rev*10+R;
        n=n/10;
}
printf("\n The sum of digits is %d",sum);
printf("\nNumber in reverse is %d",rev);

return 0;
}

6.2
#include<stdio.h>

int main()
{
    int i=1,n;
    printf("enter the n number\n");
    scanf("%d",&n);
    do
    {
        printf("%d ",i);
     i++;
    }
     while(i<=n);

6.3

#include<stdio.h>

int main()
{
    int N;
   printf("Enter the N number\n ");
   scanf("%d",&N);
    for ( int i =1; i<=N  ;i++ )
{
    if(i==N)
    {
     printf("%d/%d! ",i,i);
    }
    else
    {
printf("%d/%d! + ",i,i);
    }
}
return 0;
}

6.4
A)
#include<stdio.h>

int main()
{

   printf("nesting for loop\n");

    for ( int i =1; i<=5  ;i++ )


{
     for ( int j =1; j<=i ;j++ )
        {
          printf("%d",j);

        }
    printf("\n");
}
return 0;
}

B)
#include<stdio.h>

int main()
{
 int k=5;
   printf("nesting for loop\n");

    for ( int i =1; i<=5  ;i++,k-- )


{
     for ( int j =1; j<=i ;j++ )
        {
          printf("%d",k);

        }
    printf("\n");
}
return 0;
}

C)
#include<stdio.h>

int main()
{
   printf("nesting for loop\n");
    for ( int i =1; i<=5  ;i++)
    {
        for ( int j =1; j<=i ;j++ )
        {
          printf(" ");

        }
        for ( int j =5; j>=i ;j-- )
        {
          printf("$");
        }

        printf("\n");
   }

return 0;
}

d)Example: to make the triangle 


#include<stdio.h>

int main()
{
   printf("nesting for loop\n");

    for ( int i =1; i<=5  ;i++)


    {
       for ( int j =5; j>=i ;j-- )
        {
          printf(" ");
        }
        for ( int j =1; j<=i ;j++ )
        {
          printf("* ");
        }

        printf("\n");
    }
return 0;
}

Output:
Exp6.1:
o/p:
enter the number to print sum
1234

 The sum of digits is 10


Number in reverse is 4321
Exp6.2:
O/P:
enter the n number
9
123456789

Exp6.3:
O/P:
Enter the N number
 5
1/1! + 2/2! + 3/3! + 4/4! + 5/5!

Exp:6.4:
a)
nesting for loop
1
12
123
1234
12345

b)O/P:
nesting for loop
5
44
333
2222
11111

c)
O/P:
nesting for loop
 $$$$$
   $$$$
     $$$
       $$
         $

d)Example: to make the triangle


O/P:
nesting for loop
     *
    * *
   * * *
  * * * *
 * * * * *
DECISION MAKING 
USING SWITCH STATEMENTS
Expt No.7                                                                            Date:
25/11/2021

ROLLNO:B21616

Aim: Programs with menu selection.

7.1    Write a menu-driven program which has the following options:

1. Factorial of a number.

2. Prime or not.

3. Odd or even.
4. Exit.

  7.2   Write a C program to do the following using switch statement:

1. Quotient and Remainder of division


  
2. Reverse of a given number

3. Finding greatest given 3 nos using ternary/conditional operator

4. Illustration of increment and decrement operators

7.3 Write a menu-driven program using Switch case to calculate the


following:

1. Area of circle (A = pi *r*r)


2. Area of square (A = side * side)
3. Area of sphere (A = 4 * pi * r * r)
4. Exit

Flowchart/Algorithm:

C Program
 Source Code: 

7.1
#include<stdio.h>
int main(){
int ch,n,i,m=0,flag=0;
char c;
do
{
printf("press 1 to Factorial of a number \n");
printf("press 2 to Prime or not\n");
printf("press 3 to Odd or even \n");
printf("Enter the operation\n");
scanf("%d",&ch);

switch(ch)
{
case 1:printf("\n Enter a number\n");
    scanf("%d",&n);
    int fact=1,i=1;
for ( ;i<=n;i++)
{
fact= fact*i;
}
printf("%d! = %d",n,fact);

    break;
case 2:printf("Enter the number to check prime:");
      scanf("%d",&n);
      m=n/2;
      for(i=2;i<=m;i++)
     {
      if(n%i==0)
       {
        printf("%d is not prime",n);
       flag=1;
       break;
        }
         }
       if(flag==0)
       {
     printf("Number is prime");
       }
    break;
case 3:printf("\n Enter a number\n");
    scanf("%d",&n);
    if(n%2==0)
    {
     printf("%d is a even number",n);
    }
    else
    {
      printf("%d is a odd number",n);
    }

            break;

default:printf("Wrong choice!");
}
printf("\n Do you want to continue? (Press y/n)");
scanf(" %c",&c);
}while(c=='y');
return 0;
 }

7.2
#include<stdio.h>
int main(){
int ch,d,a,b,q=0,g=0,r=0;
char c;
do
{
printf("press 1 to Quotient and Remainder of division \n");
printf("press 2 to Reverse of a given number\n");
printf("press 3 to Find the greatest of 3 \n");
printf("press 4 to Illustration of increment and decrement operators \n\n");
printf("Enter the operation\n");
scanf("%d",&ch);

switch(ch)
{
case 1:printf("\n Enter two number\n");
    scanf("%d %d",&a,&b);
    q=a/b;
    r=a%b;
    printf("\nquotient of %d and %d is %d",a,b,q);
    printf("\nRemainder of %d and %d is %d",a,b,r);
    break;
case 2:printf("\n Enter a number\n");
    scanf("%d",&a);
    while(a>0)
    {
    int s=a;
    r=a%10;
    g=g*10+r;
    a=a/10;
    }
    printf("Reverse is %d\n",a,g);
    break;
case 3:printf("\n Enter 3 number\n");
    scanf("%d %d %d",&a,&b,&d);
    (a>b && a>d)?printf("%d is greater than %d and %d ",a,b,d):
        (b>d && b>a)?printf("%d is greater than %d and %d ",b,a,d):
            printf("%d is greater than %d and %d ",d,a,b);
            break;
case 4:printf("\n Enter a number\n");
    scanf("%d",&a);
    r=a;
    q=a;
    printf("\nincrement of %d is %d",a,++r);
    printf("\ndecrement of %d is %d",a,--q);
    break;
default:printf("Wrong choice!");
}
printf("\n Do you want to continue? (Press y/n)\n");
scanf(" %c",&c);
}while(c=='y');
return 0;
 }

7.3
#include<stdio.h>
int main(){
int ch,r,s;
char c;
do
{
printf("press 1 to Area of circle \n");
printf("press 2 to Area of square\n");
printf("press 3 to Area of sphere \n\n");
printf("Enter the operation\n");
scanf("%d",&ch);

switch(ch)
{
case 1:printf("\n Enter a radius\n");
    scanf("%d",&r);
    float c= 3.14*r*r;
    printf("Area of circle is %.2f",c);

    break;
case 2:printf("Enter side:");
      scanf("%d",&s);
      int sq = s*s;
        printf("Area of square is %d",sq);

    break;
case 3:printf("\n Enter a radius\n");
    scanf("%d",&r);
    float sp= 4*3.14*r*r;
    printf("Area of sphere is %.2f",sp);
            break;
default:printf("Wrong choice!");
}
printf("\n Do you want to continue? (Press y/n)\n");
scanf(" %c",&c);
}while(c=='y');

Output:
Exp.7.1:
O/P:
press 1 to Factorial of a number
press 2 to Prime or not
press 3 to Odd or even
Enter the operation
1

 Enter a number
4
4! = 24
 Do you want to continue? (Press y/n)y
press 1 to Factorial of a number
press 2 to Prime or not
press 3 to Odd or even
Enter the operation
2
Enter the number to check prime:5
Number is prime
 Do you want to continue? (Press y/n)y
press 1 to Factorial of a number
press 2 to Prime or not
press 3 to Odd or even
Enter the operation
3

 Enter a number
4
4 is a even number
 Do you want to continue? (Press y/n)n

Exp7.2:
O/P:
press 1 to Quotient and Remainder of division
press 2 to Reverse of a given number
press 3 to Find the greatest of 3
press 4 to Illustration of increment and decrement operators

Enter the operation


1

 Enter two number


62

quotient of 6 and 2 is 3
Remainder of 6 and 2 is 0
 Do you want to continue? (Press y/n)
y
press 1 to Quotient and Remainder of division
press 2 to Reverse of a given number
press 3 to Find the greatest of 3
press 4 to Illustration of increment and decrement operators

Enter the operation


2

 Enter a number
1234
Reverse  is 4321

 Do you want to continue? (Press y/n)


y
press 1 to Quotient and Remainder of division
press 2 to Reverse of a given number
press 3 to Find the greatest of 3
press 4 to Illustration of increment and decrement operators

Enter the operation


3

 Enter 3 number
237
7 is greater than 2 and 3
 Do you want to continue? (Press y/n)
y
press 1 to Quotient and Remainder of division
press 2 to Reverse of a given number
press 3 to Find the greatest of 3
press 4 to Illustration of increment and decrement operators

Enter the operation


4
 Enter a number
7

increment of 7 is 8
decrement of 7 is 6
 Do you want to continue? (Press y/n)
n

Exp7.3
O/P:
press 1 to Area of circle
press 2 to Area of square
press 3 to Area of sphere

Enter the operation


1

 Enter a radius
4
Area of circle is 50.24
 Do you want to continue? (Press y/n)
y
press 1 to Area of circle
press 2 to Area of square
press 3 to Area of sphere

Enter the operation


2
Enter side:5
Area of square is 25
 Do you want to continue? (Press y/n)
y
press 1 to Area of circle
press 2 to Area of square
press 3 to Area of sphere

Enter the operation


3

 Enter a radius
4
Area of sphere is 200.96
 Do you want to continue? (Press y/n)
n
Functions in C
Expt No.8                                                                            Date:
7/12/2021

ROLLNO:B21616

Aim: C Programs using Functions

8.1      Write a function to calculate the square of a given number.


8.2 Write a function to tell the user if he/she is able to vote or not.

( Consider minimum age of voting to be 18. )


8.3     Write a C program to calculate area and perimeter of a rectangle using
pointers.

8.4 Write a function to compute the greatest common divisor given


by Euclid’s Algorithm, exemplified for j=1980 k=1617 as follows:

       1980/1617 = 1            1980-1*1617 = 363


       1617/363  = 4               1617 – 4 *363 = 165

       363/165 = 2       363 – 2 * 165 = 33

       165/33 = 5         165 – 5 * 33 = 0

       So, GCD is 33.

8.5 Write a C program to print Fibonacci series using recursive


functions.
Flowchart/Algorithm:

C Program
 Source Code:

8.1
 #include<stdio.h>
void squareOfNumber();
void main()
{
    int number;
    printf("Please Enter any integer Value : ");
    scanf("%d", &number);
    squareOfNumber(number);
    return 0; 
}
void squareOfNumber(int num)
{
    int sque;
    sque = num*num;
    printf("square of a given number %d is  =  %d", num, sque);
   
}

8.2
#include<stdio.h>

void vote();
int main()
{
    int age;
    printf(" enter age\n");
    scanf("%d",&age);
    vote(age);
    return 0;
 }

void vote(int a)
{
    if(a>=18)
    {
        printf("\nEligible to vote");
    }
    else
    {
        printf("not eligible to vote");
    }
}

8.3
#include<stdio.h>

void rect();
int main()
{
    int a,b;

    printf("Enter the length of the rectangle\n");


    scanf("%d",&a);
     printf("Enter the breadth of the rectangle\n");
     scanf("%d",&b);
    rect(&a,&b);
}
void rect(int *len,int *bre)
{
    int area=*len * *bre;
    int perimeter=2*(*len+*bre);
    printf("\n the area of rectangle is : %d",area);
    printf("\n the perimeter of rectangle is : %d",perimeter);
}

8.4
#include<stdio.h>

void gcd();
int main()
{
    int a,b;

    printf("Enter the two number\n");


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

    gcd(    a,b);
    return 0;
}
void gcd(int j,int k)
{
int Q, R=1;
while(R!=0)
{
    Q=j/k;
R=j-Q*k;
j=k;
k=R;

}
printf("GCD is %d",j);

8.5
#include<stdio.h>
void Fido();
int main()
{
    int n1=0,n2=1,n3;

    printf("Enter the number\n");


    scanf("%d",&n3);

    Fibo(n1,n2,n3);
    return 0;
}
void Fibo(int a,int b,int c)
{
int i=1;
printf("%d,%d,",a,b);
while(i<c){
 int n4=a +b;
   a=b;
  b=n4;
  printf("%d,",n4);
  i++;}

Output:

Exp8.1:

Please Enter any integer Value : 21


square of a given number 21 is  =  441

Exp8.2:
enter age
19
Eligible to vote

Exp8.3
Enter the length of the rectangle
23
Enter the breadth of the rectangle

 the area of rectangle is : 6


 the perimeter of rectangle is : 10

Exp8.4:
Enter the two number
1980 1617
GCD is 33

Exp8.5:
Enter the number
12
0,1,1,2,3,5,8,13,21,34,55,89,144,

Advanced Programming Constructs:


1D-Arrays
Expt No.9                                                                            Date:
14/12/2021

Aim: C Programs using One-dimensional arrays

9.1 Program to access and print an array of 5 elements.

9.2 10 numbers are entered from a keyboard into an array. The


number to be searched is entered through the keyboard by the user.
Write a C program to find if the number to be searched is present in
the array and if it is present, display the number of times(frequency)
it appears in the array.

9.3 Write a C program to count the positive and negative integers in a


given array.

9.4 Write a C program to input an array A of 6 elements and compute the


square of these entered 6 elements and store in another array B. Also
store the product of elements of these two arrays in a third array C. 

Index 0 1 2  3 4 5

A[6] 7 9 12   6  2  1

B[6] 7*7 9*9 12*12     6*6   2*2  1

C[6] 49*7 81*9 144*12     36*6   4*2  1

Flowchart/Algorithm:
\

C Program
 Source Code:

9.1
 #include<stdio.h>
 int main()
 {
     printf(" Program to access and print an array of 5 elements.\n");
    int arr[5];
    printf("Enter the 5 elements of the array\n");
    for(int i=0;i<5;i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("entered elements are\n");
    for(int i=0;i<5;i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
 }

9.2
#include<stdio.h>
 int main()
 {
     printf(" 10 numbers are entered from a keyboard into an array. \nThe number to be
searched is entered through the keyboard by the user. \nWrite a C program to find if the
number to be searched is present in the array and if it is present, \ndisplay the number of
times(frequency) it appears in the array.\n");
    int arr[10];
    int count=0,searchs;
    printf("\n\nEnter the 10 elements of the array\n");
    for(int i=0;i<10;i++)
    {
        scanf("%d",&arr[i]);
    }
     printf("enter the number to be search : ");
     scanf("%d",&searchs);
    for(int i=0;i<10;i++)
     {
  if(searchs == arr[i])
 {
count++;
      }
      }
      if(count==0)
printf("Not present= %d ",searchs);
     else
printf(" %d present in the array %d times",searchs,count);
 }

9.3
#include<stdio.h>
 int main()
 {
     printf("Write a C program to count the positive and negative integers in a given array.\n\
n");
    int n;
    int countpos=0,countneg=0;
    printf("enter the size if the elements :");
    scanf("%d",&n);
    int arr[n];
    printf("\n\nEnter the elements of the array\n");
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(int i=0;i<n;i++)
     {if(arr[i]>0)
       {
        countpos++;
      }
      else if(arr[i]<0)
              {
                  countneg++;
              }

    }
    printf("the total positive are is %d and negative is%d",countpos,countneg);
 }

Exp9.4:
// Online C compiler to run C program online
#include <stdio.h>

int main() {
    // Write C code here
    //printf("Hello world");
    int a[6];
    int b[6],c[6];
    printf("enter the 6 elements=\n ");
    for(int i=0;i<6;i++)
    {
        scanf("%d",&a[i]);
    }
     printf("\n\n");
    printf("a[6]= ");
    for(int i=0;i<6;i++)
    {
        printf(",%d  ",a[i]);
    }
    printf("\n\n");
    printf("b[6]= ");
    for(int i=0;i<6;i++)
    {
        b[i]=a[i]*a[i];
        printf(", %d ",b[i]);
    }
    printf("\n\n");
    printf("b[6]= ");
    for(int i=0;i<6;i++)
    {
        c[i]=b[i]*a[i];
        printf(", %d",c[i]);
    }
    return 0;
}

Output:

Exp9.1: 
Program to access and print an array of 5 elements.
Enter the 5 elements of the array
12345
entered elements are
1 2 3 4 5 

Exp9.2:

10 numbers are entered from a keyboard into an array. 


The number to be searched is entered through the keyboard by the user. 
Write a C program to find if the number to be searched is present in the array and if it is
present, 
display the number of times(frequency) it appears in the array.

Enter the 10 elements of the array


1 2 3 4 5 5 6 7 88 8
enter the number to be search : 5
5 present in the array 2 times

Exp9.3:

Write a C program to count the positive and negative integers in a given array.

enter the size if the elements :5


Enter the elements of the array
1 -1 2 -2 3
the total positive are is 3 and negative is2

Exp9.4:
enter the 6 elements=
 7 9 12 6 2 1
 a[6]= ,7  ,9  ,12  ,6  ,2  ,1  

b[6]= , 49 , 81 , 144 , 36 , 4 , 1 

b[6]= , 343, 729, 1728, 216, 8, 1


2D-Arrays
Expt No.10                                                                              Date:
21/12/2021

Aim: C Programs using Two-dimensional arrays

10.1 Write a C program to access and print a user-defined array of 3X3


size and print the sum of odd and even numbers in that array. 

10.2 Write a C program to print the largest number from any 5 row by 3
column matrix. Also print its position.

10.3 Write a menu-driven C program to perform the following


operations on a 2D matrix:

A. Addition And subtraction of matrices


B. To compute the sum of diagonals of an array.
C. To find the transpose of a given matrix.
D. To find the scalar multiplication.
Flowchart/Algorithm:
C Program
 Source Code:

10.1
#include<stdio.h>
int main()
    {
    int SumEven=0,SumOdd=0;
    int arr[3][3];
    printf("enter the elements of the array\n");
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
     scanf("%d",&arr[i][j]);
   }
   }
   printf(" the elements are\n");
   for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
     printf("%d ",arr[i][j]);
   }
   printf("\n");
   }
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
     if(arr[i][j]%2==0)
     {
       SumEven+=arr[i][j];
    }
    else
    {
        SumOdd+=arr[i][j];
    }
   }
   }
   printf("The Sum of Odd Elements = %d and sum of even elements=%d
",SumOdd,SumEven);

   return 0;
    }

10.2
#include<stdio.h>
int main()
    {
    int largest=0,row,col;
    int arr[5][3];
    printf("enter the elements of the array\n");
    for(int i=0;i<5;i++)
    {
    for(int j=0;j<3;j++)
    {
     scanf("%d",&arr[i][j]);
   }
   }
   printf(" the elements are\n");
   for(int i=0;i<5;i++)
    {
    for(int j=0;j<3;j++)
    {
     printf("%d ",arr[i][j]);
   }
   printf("\n");
   }
    for(int i=0;i<5;i++)
    {
    for(int j=0;j<3;j++)
    {
     if(arr[i][j]>largest)
     {
      largest=arr[i][j];

    }
   }
   }
   printf("largest element is %d ",largest);

   return 0;
    }

10.3

#include<stdio.h>
void main()
{
char ch;
char choice;
int  c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
int i, j, a[10][10], Sum = 0;
int m, n, matrix[10][10], transpose[10][10];
int rows, columns, Multiplication[10][10], Number;
do{
printf("\n A. Addition And subtraction of matrices");
printf("\n B. To compute the sum of diagonals of an array.");
printf("\n C.To find the transpose of a given matrix. ");
printf("\n D. To find the scalar multiplication. \n");
printf("Select your choice\n");
scanf("%s",&ch);
switch(ch)
{
case 'A':
    printf("\nEnter the number of rows and columns of the first matrix \n\n");
    scanf("%d%d", &m, &n);

    printf("\nEnter the %d elements of the first matrix \n\n", m*n);


    for(c = 0; c < m; c++)   // to iterate the rows
        for(d = 0; d < n; d++)   // to iterate the columns
            scanf("%d", &first[c][d]);

    printf("\nEnter the %d elements of the second matrix \n\n", m*n);


    for(c = 0; c < m; c++)   // to iterate the rows
        for(d = 0; d < n; d++)   // to iterate the columns
            scanf("%d", &second[c][d]);

    /*
        printing the first matrix
    */
    printf("\n\nThe first matrix is: \n\n");
    for(c = 0; c < m; c++)   // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            printf("%d\t", first[c][d]);
        }
    printf("\n");
    }

    /* 
        printing the second matrix
    */
    printf("\n\nThe second matrix is: \n\n");
    for(c = 0; c < m; c++)   // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            printf("%d\t", second[c][d]);
        }
    printf("\n");
    }

    /* 
        finding the SUM of the two matrices 
        and storing in another matrix sum of the same size
    */
    for(c = 0; c < m; c++)
        for(d = 0; d < n; d++)
            sum[c][d] = first[c][d] + second[c][d];

    // printing the elements of the sum matrix


    printf("\n\nThe sum of the two entered matrices is: \n\n");
    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            printf("%d\t", sum[c][d]);
        }
        printf("\n");
    }

    /*
        finding the DIFFERENCE of the two matrices 
        and storing in another matrix difference of the same size
    */
    for(c = 0; c < m; c++)
        for(d = 0; d < n; d++)
            diff[c][d] = first[c][d] - second[c][d];

    // printing the elements of the diff matrix


    printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            printf("%d\t", diff[c][d]);
        }
        printf("\n");
    }

  break;
case 'B': 
  
  printf("\n Please Enter Number of rows and columns  :  ");
  scanf("%d %d", &i, &j);
 
  printf("\n Please Enter the Matrix Elements \n");
  for(rows = 0; rows < i; rows++)
   {
    for(columns = 0;columns < j;columns++)
     {
       scanf("%d", &a[rows][columns]);
     }
   }
      
  for(rows = 0; rows < i; rows++)
   {
    Sum = Sum + a[rows][rows];
   }
 
  printf("\n The Sum of Diagonal Elements of a Matrix =  %d", Sum );

        break;
case 'C':
   printf("Enter rows and columns :\n");
   scanf("%d%d", &m, &n);
   printf("Enter elements of the matrix\n");
   for (i= 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &matrix[i][j]);
   for (i = 0;i < m;i++)
      for (j = 0; j < n; j++)
         transpose[j][i] = matrix[i][j];
   printf("Transpose of the matrix:\n");
   for (i = 0; i< n; i++) {
      for (j = 0; j < m; j++)
         printf("%d\t", transpose[i][j]);
      printf("\n");
   }
  break;
case 'D':
  
  printf("\n Please Enter Number of rows and columns\n");
  scanf("%d %d", &i, &j);
 
  printf("\n Please Enter the Matrix Elements \n");
  for(rows = 0; rows < i; rows++)
   {
    for(columns = 0;columns < j;columns++)
     {
       scanf("%d", &Multiplication[rows][columns]);
     }
   }
   
  printf("\n Please Enter the Multiplication Value  :  ");
  scanf("%d", &Number);
    
  for(rows = 0; rows < i; rows++)
   {
    for(columns = 0; columns < j; columns++)
     {
       Multiplication[rows][columns] = Number * Multiplication[rows][columns];    
    }
   }
 
  printf("\n The Result of a Scalar Matrix Multiplication is : \n");
  for(rows = 0; rows < i; rows++)
   {
    for(columns = 0; columns < j; columns++)
     {
       printf("%d \t ", Multiplication[rows][columns]);
     }
     printf("\n");
   }
break;
default:printf("Wrong choice!");
}

printf("\n Do you want to continue? (Press y/n)");


scanf(" %c",&choice);
}while(choice=='y');
return 0;
}

Output:
10.1:
enter the elements of the array
243
654
953
 the elements are
2 4 3 
6 5 4 
9 5 3 
The Sum of Odd Elements = 25 and sum of even elements=16 

10.2:
enter the elements of the array
123
6 12 3
4 8 16
10 5 9
11 13 0
 the elements are
1 2 3 
6 12 3 
4 8 16 
10 5 9 
11 13 0 
largest element is 16 

10.3:
O/P:

A. Addition And subtraction of matrices


 B. To compute the sum of diagonals of an array.
 C.To find the transpose of a given matrix. 
 D. To find the scalar multiplication. 
Select your choice
A

Enter the number of rows and columns of the first matrix 

22

Enter the 4 elements of the first matrix 

67
80

Enter the 4 elements of the second matrix 

84
21
The first matrix is: 

6   7
8   0

The second matrix is: 

8   4
2   1

The sum of the two entered matrices is: 

14      11
10      1

The difference(subtraction) of the two entered matrices is: 

-2      3
6       -1

 Do you want to continue? (Press y/n)y

 A. Addition And subtraction of matrices


 B. To compute the sum of diagonals of an array.
 C.To find the transpose of a given matrix. 
 D. To find the scalar multiplication. 
Select your choice
B

 Please Enter Number of rows and columns  :  3 3

 Please Enter the Matrix Elements 


345
671
132

 The Sum of Diagonal Elements of a Matrix =  12


 Do you want to continue? (Press y/n)y

 A. Addition And subtraction of matrices


 B. To compute the sum of diagonals of an array.
 C.To find the transpose of a given matrix. 
 D. To find the scalar multiplication. 
Select your choice
C
Enter rows and columns :
33
Enter elements of the matrix
345
671
132
Transpose of the matrix:
3   6   1
4   7   3
5   1   2

 Do you want to continue? (Press y/n)y

 A. Addition And subtraction of matrices


 B. To compute the sum of diagonals of an array.
 C.To find the transpose of a given matrix. 
 D. To find the scalar multiplication. 
Select your choice
D

 Please Enter Number of rows and columns


33

 Please Enter the Matrix Elements 


345
671
132

 Please Enter the Multiplication Value  :  4

 The Result of a Scalar Matrix Multiplication is : 


12       16      20      
24       28      4       
4        12      8       

 Do you want to continue? (Press y/n)n

Strings in C
Expt No. 11                                                                              Date:
23/12/2021

Aim: C Programs to illustrate the use of  String methods

11.1 To print the count of characters in a given string.

11.2  To count the occurrences of a character 

11.3  To concatenate any two strings without the use of predefined function

11.4 To copy string without the use of predefined library function

11.5 To check whether a given string is palindrome or not.

Flowchart/Algorithm:
C Program
 Source Code:

Exp11.1:
#include <stdio.h>
#include <string.h>  
   
int main()  
{  
    char string[20];  
    int count = 0; 
    printf("enter you name \n");
    
    gets(string);
 
    
    for(int i = 0; i < strlen(string); i++) {  
        if(string[i] != ' ')  
            count++;  
    }  
      
      printf("Total number of characters in a string: %d", count);  
   
    return 0;  

Exp11.2:
 #include <stdio.h>
#include <string.h>  
   
int main()  
{  

char s[20]=“I love to code.”;


 printf("enter you name \n");
    
    gets(s);
printf("Enter character to be searched: ");
char c=getchar();
int charOccurence=0;
for(int i=0;i<strlen(s);i++)  
{
     if(s[i]==c)
     {
           charOccurence++;
}
}
     
printf("character '%c' occurs %d times \n ",c,charOccurence);
return 0;
}

Exp11.3:
#include <stdio.h>
#include <string.h>  
   
int main()  
{  
    char str1[20];
    char str2[20];
    char str3[40];
   printf("enter your first name \n");
    
    gets(str1);
     printf("enter your sceond name \n");
    
    gets(str2);
    int i = 0, j = 0;
  
    printf("\nFirst string: %s", str1);
    printf("\nSecond string: %s", str2);
  
    // Insert the first string in the new string
    while (str1[i] != '\0') {
        str3[j] = str1[i];
        i++;
        j++;
    }
  
    // Insert the second string in the new string
    i = 0;
    while (str2[i] != '\0') {
        str3[j] = str2[i];
        i++;
        j++;
    }
    str3[j] = '\0'; //Terminate the string str3
  
    // Print the concatenated string
    printf("\nConcatenated string: %s", str3);
    
    return 0;
}

Exp11.4:
#include <stdio.h>
int main() {
    char s1[100], s2[100], i;
    printf("Enter string s1: ");
    fgets(s1, sizeof(s1), stdin);

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


        s2[i] = s1[i];
    }

    s2[i] = '\0';
    printf("String s2: %s", s2);
    return 0;
}

Exp11.5
#include <stdio.h>
#include <string.h>
 
int main()
{
    char s[1000];  
    int i,n,c=0;
 
    printf("Enter  the string : ");
    gets(s);
    n=strlen(s);
 
    for(i=0;i<n/2;i++)  
    {
     if(s[i]==s[n-i-1])
     c++;
 
  }
  if(c==i)
      printf("string is palindrome");
    else
        printf("string is not palindrome");
 
   
     
    return 0;
}

Output:

Exp11.1:
o/p:
enter you name 
gildon ferreira
Total number of characters in a string: 14

Exp11.2:
o/p:
enter you name 
gildon ferreira
Enter character to be searched: r
character 'r' occurs 3 times 

Exp11.3:
o/p:
enter your first name 
gildon 
enter your sceond name 
ferreira
First string: gildon 
Second string: ferreira
Concatenated string: gildon ferreira

Exp11.4:
o/p:
Enter string s1: gildon ferreira
String s2: gildon ferreira

exp:11.5:
o/p:
Enter  the string : wow
string is palindrome
Arrays in Functions 
Expt No.12                                                                              Date:
04/01/2022

Aim: Arrays in functions

12.1 Write a user-defined function that searches an element in a given array.


Consider the array and the element to be searched is passed as arguments.

12.2 Write two functions that will reverse and sort the elements respectively of
any array. 

Flowchart/Algorithm:

C Program
 Source Code:
Exp12.1:
#include<stdio.h>
double searchElement();
void main()
{
    int num[5];
    int search;
    printf("Please Enter any 5 integer Value : ");
    for(int i=0;i<5;i++)
    {
    scanf("%d",&num[i]);
    }
    printf("Please Enter the search element : ");
    scanf("%d", &search);
    searchElement(num,search);
}
double searchElement(int arr[],int s) {

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


if(arr[i]==s){
printf(“%d is present in the array”,s);
break;
}
else
{
printf("%d is NOT present in the array",s);
break;
}
  }
}

Exp12.2:
#include<stdio.h>
void rev();
void sortasc();
void main()
{
    int A[6];
     printf("Please Enter any 6 integer Value : ");
    for(int i=0;i<=5;i++)
    {
    scanf("%d",&A[i]);
    }
   
    rev(A);
    sortasc(A);
}
void rev(int arr[]) {
printf("The elements in reverse order are\n");
  for (int i = 5; i >=0; i--) {
printf("%d ",arr[i]);
  }
}

void sortasc(int arr[])


{
//SORTING
for(int i=0;i<=5;i++)
{
for(int j=i+1;j<=5;j++)
{
if(arr[i]>arr[j])
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nsorted in ascending order \n");
for (int i = 0; i < 6; ++i) {
printf("%d ",arr[i]);
  }

Output:
12.1:
o/p:
Please Enter any 5 integer Value : 10 9 4 2 10
Please Enter the search element : 10
10 is present in the array

12.2:
o/p:
Please Enter any 6 integer Value : 5 8 45 6 -10 99
The elements in reverse order are
99 -10 6 45 8 5 
sorted in ascending order 
-10 5 6 8 45 99 

You might also like