OBJECTIVE

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

OBJECTIVE: To use switch…case statement to print the numbers entered by the user (1-10) in

words.

PROGRAM:
#include<stdio.h>
void main()
{
int no;

printf("Enter the number between 1 to 10: ");


scanf("%d",&no);

switch(no)
{
case 1:
printf("You entered ONE");
break;

case 2:
printf("You entered TWO");
break;

case 3:
printf("You entered THREE");
break;

case 4:
printf("You entered FOUR");
break;

case 5:
printf("You entered FIVE");
break;

case 6:
printf("You entered SIX");
break;

case 7:
printf("You entered SEVEN");
break;

case 8:
printf("You entered EIGHT");
break;

case 9:
printf("You entered NINE");
break;

case 10:
printf("You entered TEN");
break;

default:
printf("Invalid number");

}
}

Output :
PRACTICAL -9
OBJECTIVE: To use while statement to reverse the digits of a given number.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
getch();
return 0;
}

Output :
Enter a number : 123
Reversed number : 321
PRACTICAL -10

OBJECTIVE: To use for statement to print the multiplication table of a given number.

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

for (int i = 1; i <= 10; ++i) {


printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}

Output :

Enter an integer:

2*1= 2
2*2= 4
2*3= 6
2*4= 8
2*5= 10
2*6= 12
2*7= 14
2*8= 16
2*9= 18
2*10= 20
PRACTICAL -11

OBJECTIVE: To implement a menu driven arithmetic calculator using do while loop.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, i;
printf("Enter Number 1 :--> ");
scanf("%d", &a);
printf("\nEnter Number 2 :--> ");
scanf("%d", &b);
do
{
printf("\nEnter 1 for '+' ");
printf("\nEnter 2 for '-' ");
printf("\nEnter 3 for '*' ");
printf("\nEnter 4 for '/' \n");
printf("Enter 0 for Exit ");
printf("\nEnter your choice :--> ");
scanf("%d", &i);
switch(i)
{
case 0: break;
case 1:
printf("\na + b = %d\n", a + b);
break;
case 2:
printf("\na - b = %d\n", a - b);
break;
case 3:
printf("\na * b = %d\n", a * b);
break;
case 4:
printf("\na / b = %f\n", a / b);
break;
default:
printf("\nEnter valid option\n");
}
} while(i != 0);
}
Output :
Enter Number 1 :--> 8

Enter Number 2 :--> 3

Enter 1 for '+'


Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 1

a + b = 11

Enter 1 for '+'


Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 2

a-b=5

Enter 1 for '+'


Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 3

a * b = 24

Enter 1 for '+'


Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 4

a / b = 0.000000

Enter 1 for '+'


Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 0

PRACTICAL -12

OBJECTIVE: To read the marks of 10 students in an array and calculate their average
PROGRAM:
#include <stdio.h>
void main()
{
int i,n,sum=0;
float avg;
printf("Input the 10 numbers : \n");
for (i=1;i<=10;i++)
{
printf("Number-%d :",i);

scanf("%d",&n);
sum +=n;
}
avg=sum/10.0;
printf("The sum of 10 no is : %d\nThe Average is : %f\n",sum,avg);

Output:

Input the 10 numbers :


Number-1 :1
Number-2 :2
Number-3 :3
Number-4 :4
Number-5 :5
Number-6 :6
Number-7 :7
Number-8 :8
Number-9 :9
Number-10 :10
The sum of 10 no is : 55
The Average is : 5.500000

PRACTICAL -13

OBJECTIVE: To read two matrices and compute their sum using 2-Dimensional arrays.

PROGRAM:
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

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


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

Output :

Enter the number of rows (between 1 and 100): 2


Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:

Enter element a11: 2


Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3

Sum of two matrices:


-2 8 7

10 8 6
PRACTICAL -14

OBJECTIVE: To reverse the characters of a given string.

PROGRAM:

#include <stdio.h>
#include <string.h>
int main()
{
char str[40]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);

// use strrev() function to reverse a string


printf (" \n After the reverse of a string: %s ", strrev(str));
return 0;
}

Output

Enter a string to be reversed: AMBULANCE


After the reverse of a string: ECNALUBMA
PRACTICAL -15

OBJECTIVE: To demonstrate strlen(), strcat(), strcmp() functions.

PROGRAM:
#include<stdio.h>
#include<strings.h>
#include<conio.h>
main()
{
int n;

char name[20], age[20], temp[20], verify[5]="Ayan";

puts("Enter verified user Name : ");

gets (name);
puts("Enter verified user Age: ");
gets (age);
strlen(name);
printf("\nName Length is %d character.\n",n);

strcpy(temp, name);
puts(temp);

puts("Name Copy Completed.\n");


strcat (name, age);
puts (name);
puts("Name and age join completed.\n");
if(strcmp(verify, temp)=='\0')
puts(name);
puts("This user is verified.");

else
{
puts(name);
puts("This user is not verified.");

Output:
PRACTICAL -16

OBJECTIVE: To swap values of two variables using a function.

PROGRAM:
#include<stdio.h>
void swap(int *,int *);
int main()
{

int n1,n2;
printf("\n\n Function : swap two numbers using function :\
n");
printf("------------------------------------------------\n");

printf("Input 1st number : ");


scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);

printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);


//pass the address of both variables to the function.
swap(&n1,&n2);

printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);


return 0;
}

void swap(int *p,int *q)


{

int tmp;
tmp = *p;
*p=*q;
*q=tmp;
}

Output:

Input 1st number : 2


Input 2nd number : 4
Before swapping: n1 = 2, n2 = 4
After swapping: n1 = 4, n2 = 2

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