• No results found

IISem Lab Manual (1)

N/A
N/A
Protected

Academic year: 2020

Share "IISem Lab Manual (1)"

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)

Programming in ‘C’ Lab Manual

Group: I BSc

Semester-II

1. Write a C program to check whether the given number is perfect or not. Aim: To write a C- program to check whether the given number is perfect or not.

Definition:

Perfect Number Definition:

A perfect number: a number is perfect when the sum of its divisors (except the number itself) equals the given number.

Examples of perfect numbers

 6 : The divisors of 6 are 1,2,3 & 6. To show that this is a perfect number we could all the divisors except the number itself

o 1+2+3 = 6  28

o 1 + 2 + 4 + 7 + 14 =28

 496 and 8128 are also perfect number

Algorithm

1. Start 2. Read n 3. Sum=0;

4. For i=1 to n/2 do

a. If (n%i==0) b. sum=sum+i End for

5. If sum==n goto step6 else goto step 7

6. Write “Given number is Perfect number” and goto step 8 7. Write “Given number is not a Perfect number”

(2)

Flow Chart

(3)

Program: /*Perfect1.c*/ #include<stdio.h> #include<conio.h> void main() { int n,i,sum=0; clrscr();

printf("\nEnter an integer:"); scanf("%d",&n); for(i=1;i<=n/2;i++) { if(n%i==0) sum=sum+i; } if(sum==n) {

printf("\n %d is Perfect Number",n); }

else {

printf(" %d is not a Perfect Number",n); }

getch(); }

Output1:

Enter an integer: 28

28 is Perfect Number

Output2:

Enter an integer: 23

(4)

2. Write a C program to check whether a number is Armstrong or not.

Aim: To write a C-program to check whether a number is Armstrong or not.

Definition:

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

For example, 371 is an Armstrong number since 33 + 73 + 13 = 371.

Algorithm:

Step 1: Start the program.

Step 2: Read a positive integer n from the keyboard.

Step 3: Store the value of n in a temp variable and initialize the sum variable to 0.

Step 4: Repeat the following steps until n>0 return false value. a. Find the reminder of n i.e., rem=n%10.

b. Add the cube value of rem to sum i.e., sum=sum+ rem*rem*rem. c. Divide n with 10 i.e., n=n/10.

Step 5: If the temp is equal to sum then print the given number is an Armstrong number Otherwise print it is not an Armstrong number.

Step 6: Stop the program.

(5)
(6)

Program:

/* C program to check whether a number entered by user is Armstrong or not. */

#include <stdio.h> #include<conio.h> int main() { int n,m,rem,sum=0; clrscr();

printf("Enter a positive integer: ");

scanf("%d", &n); m=n; while(n>0) { rem=n%10; sum=sum+rem*rem*rem; n=n/10; } if(sum==m)

printf("%d is an Armstrong number.",m);

else

printf("%d is not an Armstrong number.",m);

getch();

}

(7)

Output1:

Enter a positive integer: 153

153 is an Armstrong number.

Output2:

Enter a positive integer: 171

171 is not an Armstrong number.

3. Write a C program to find the sum of individual digits of a positive integer. Aim: To write a C-program to find the sum of individual digits of a positive integer.

Example:

Consider n=123

 1+2+3=6

Algorithm:

Step 1: Start the program.

Step 2: Read a positive integer n from the keyboard.

Step 3: Initialize the sum variable to 0.

Step 4: Repeat the following steps until n>0 return false value. a. Find the reminder of n i.e., rem=n%10. b. Add the rem to sum i.e., sum=sum+ rem. c. Divide n with 10 i.e., n=n/10.

Step 5: Print the sum value as output.

(8)

Flow chart:

(9)

Program:

#include<stdio.h>

#include<conio.h>

void main()

{

int n,rem,sum=0;

clrscr();

printf("\nEnter a +ve integer:");

scanf("%d",&n);

while(n>0)

{

rem=n%10;

sum=sum+rem;

n=n/10;

}

printf("\nThe sum of digits is:%d",sum);

getch();

}

Output:

Enter a +ve integer: 1234

(10)

4. A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence.

Aim: To write a C-Program to display Fibonacci sequence up to the given ragnge.

Algorithm: Step 1: Start.

Step 2: Read n value.

Step 3: Assign 0 to a.

Step 4: Assign 1 to b.

Step 5: Write a.

Step 6: Write b.

Step 7: Calculate sum of a and b store in c.

Step 8: Repeat steps (i) to (iv) until c <= n.

Step i: Write c.

Step ii: Assign b to a.

Step iii: Assign c to b.

Step iv: Calculate sum of a and b store in c.

Step 9: End.

(11)
(12)

Program: #include<stdio.h> #include<conio.h> void main() { int a,b,c,n; clrscr();

printf("Enter n value \n"); scanf("%d",&n);

a=0; b=1;

printf(“\nThe Fibonacci series is….\n”); printf("%d",a); printf("%3d",b); c=a+b; while(c<=n) { printf("%3d",c); a=b; b=c; c=a+b; } getch(); } Output:

Enter n value 10 The Fibonacci series is

0 1 1 2 3 5 8

(13)

5. Write a C program to check whether the given number is Prime or not. Aim: To write a C-program to check whether the given number is Prime or not.

Definition:

If the total number of divisors of the given number is equals to 2 (Including 1 and itself), then it is called as a Prime number.

In other words, if the total number of divisors of the given number is equals to 0 (Excluding 1 and itself), then it is called as a Prime number.

Example:

Consider, n=7

It is called as a Prime number since, the total number of divisors of 7 are equals to 2 (1 and 7).

Algorithm:

Step 1: Start the program.

Step 2: Read n value from the keyboard.

Step 3: Initialize cnt equals to 2.

Step 4: Repeat the following until i<=n/2 starts from i=2

a. Check whether n%i==0 or not, if it is true increase cnt by 1 b. Increase i value by 1.

Step 5: Check whether cnt==2 or not, if it is true print n is prime otherwise print n is not a prime.

(14)

Flow chart:

(15)

Program:

/*Prime.c*/

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,cnt=2;

clrscr();

printf("\nEnter an integer:");

scanf("%d",&n);

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

{

if(n%i==0)

cnt++;

}

if(cnt==2)

{

printf("\n %d is Prime Number",n);

}

else

(16)

Output 1:

Enter an integer: 7

7 is Prime Number

Output 2:

Enter an integer: 10

10 is not a Prime Number

6. Write a C program to find both the largest and smallest number in a list of integers. Aim: To write a C-program to find both the largest and smallest number in a list of integers.

Algorithm:

Step 1: Start the program.

Step 2: Read array size n value from the keyboard.

Step 3: Read elements into the array.

Step 4: Initialize both max and min variables to a[0].

Step 5: Repeat the following steps until i<=n-1 starts from i=1

a. Check whether min>a[i] or not. If it is true assign min=a[i]. b. Check whether max<a[i] or not. If it is true assign max=a[i].

Step 6: Print min and max values on the console as output.

Step 7: Stop the program.

(17)
(18)

Program: #include<stdio.h> #include<conio.h> void main() { int a[10],n,i,min,max; clrscr();

printf("Enter array size:");

scanf("%d",&n);

printf("\nEnter elements into the array:");

for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } min=max=a[0]; for(i=1;i<=n-1;i++) { if(min>a[i]) { min=a[i]; } if(max<a[i]) { max=a[i]; }

(19)

}

printf("\nMax value:%d",max);

printf("\nMin value:%d",min);

getch();

}

Output:

Enter array size: 5

Enter elements into the array: 10 20 30 40 50

Max value: 50

Min value: 10

7. Write a C program to perform addition of Two Matrices

Aim: To write a C program to perform addition of Two Matrices

Algorithm:

Step-1: Start the program.

Step-2: Enter the row and column of matrix. Step-3: Enter the elements of the matrix. Step-4: Enter the elements of the b matrix. Step-5: Print the a matrix in the matrix form. Step-6: Print the b matrix in the matrix form. Step-7: Set a loop up to the row.

Step-8: Set a inner loop up to the column

(20)
(21)

Program:

/*MatAdd.c*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10][10],b[10][10],c[10][10];

int r1,c1,r2,c2,i,j;

clrscr();

printf("\nEnter order of first matrix:");

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

printf("\nEnter order of second matrix:");

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

if(c1==c2&&r1==r2)

{

printf("\nEnter elements into first matrix\n");

for(i=0;i<=r1-1;i++)

{

for(j=0;j<=c1-1;j++)

(22)

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

/*Logic for matrix addition*/

for(i=0;i<=r1-1;i++) { for(j=0;j<=c1-1;j++) { c[i][j]=a[i][j]+b[i][j]; } }

printf("\nAddition of matrices is....\n\n");

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

(23)

else

{

printf("\nMatrix addition not possible");

}

getch();

}

Output 1:

(24)

8. Write a C program to perform multiplication of Two Matrices Aim: To write a C-program to perform multiplication of Two Matrices

Algorithm: Step 1: Start.

Step 2: Read the row size r1 and column size c1 of matrix a.

Step 3: Read the row size r2 and column size c2 of matrix b.

Step 4: If c1 ==r2 then proceed else go to step 11.

Step 5: Input a[i][j] and b[i][j].

Step 6: i =0.

Step 7: Repeat steps i to iii until i < =r1-1.

Step i: j = 0.

Step ii: Repeat steps a to d until j <=c2-1.

Step a: Initialize c[i][j]=0

Step b: Initialize k=0

Step c: Repeat steps I & II until k <=r2-1.

Step I: Calculate c[i][j]=c[i][j]+a[i][k]*b[k][j].

Step II: Increment k by 1.

Step d: Increment j by 1.

Step iii: Increment i by 1.

Step 8: i =0.

Step 9: Repeat steps i to iii until i <=r1-1.

Step i: j = 0.

Step ii: Repeat steps a & b until j <=c2-1.

Step a: Write c[i][j].

Step b: Increment j by 1.

Step iii: Increment i by 1.

Step 10: go to step 12.

Step 11: Write “Matrix Multiplication is not possible”

Step 12: End.

(25)
(26)

Program: /*MatMul.c*/ #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10]; int r1,c1,r2,c2,i,j,k; clrscr();

printf("\nEnter order of first matrix:");

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

printf("\nEnter order of second matrix:");

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

if(c1==r2)

{

printf("\nEnter elements into first matrix\n");

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

printf("\nEnter elements into second matrix\n");

(27)

for(i=0;i<=r2-1;i++)

{

for(j=0;j<=c2-1;j++)

{

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

}

}

/*Logic for matrix multiplication*/

for(i=0;i<=r1-1;i++)

{

for(j=0;j<=c2-1;j++)

{

c[i][j]=0;

}

}

for(i=0;i<=r1-1;i++)

{

for(j=0;j<=c2-1;j++)

{

for(k=0;k<=c1-1;k++)

(28)

printf("\nMultiplication of matrices is....\n\n");

for(i=0;i<=r1-1;i++)

{

for(j=0;j<=c2-1;j++)

{

printf("%d \t",c[i][j]);

}

printf("\n\n");

}

}

else

{

printf("\nMatrix multiplication not posible");

}

getch();

}

(29)

Output1:

Output 2:

9. Write a C program to check whether the given string is Palindrome or not Aim: To write a C program to check whether the given string is Palindrome or not

Algorithm: Step 1: Start.

Step 2: Write “Enter string”.

(30)

Flow chart

Program:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char s1[20],s2[20];

clrscr();

printf("\nEnter a string:");

(31)

gets(s1);

strcpy(s2,s1);

strrev(s1);

if(stricmp(s1,s2)==0)

{

printf("%s is Palindrome",s2);

}

else

{

printf("\n%s is not a Palindrome",s2);

}

getch();

}

Output1:

Enter a string: Hello

Hello is not a Palindrome

Output2:

Enter a string: Madam

(32)

10. Write C program that implements searching of given item in given list Aim: To write a C program to search the given element in an array of elements.

Description:

The linear search is most simple searching method. It does not expect the list to be sorted. The key which is to be searched is compared with each element of the list one by one. If a match exists, the search is terminated. If the end of list is reached it means that the search has failed and key has no matching in the list.

Algorithm:

Step 1: Start the program.

Step 2: Input n as size of the array and initialize flag=0 Step 3: Input array a[i] and search element s

Step 4: Repeat the following steps until i<=n-1 starts from i=0

a. Check whether a[i]==s or not, if it is true do the following otherwise increase i value by 1.

i. flag=1 ii. goto step 5

Step 5: Check whether flag==1 or not, if it is true print “Search element is found” otherwise print “Search element is not found”

Step 6: Stop the program.

(33)
(34)

Program: #include<stdio.h> #include<conio.h> void main() { int a[10],n,s,i,flag=0; clrscr();

printf("\nEnter array size:");

scanf("%d",&n);

printf("\nEnter elements into the array\n");

for(i=0;i<=n-1;i++)

{

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

}

printf("\nEnter search element:");

scanf("%d",&s);

/* Logic for Searching */

for(i=0;i<=n-1;i++) { if(s==a[i]) { flag=1; break; } }

(35)

if(flag==1)

{

printf("\nThe search element %d is found at %d position",s,(i+1));

}

else

{

printf("\nThe search element %d not found",s);

}

getch();

}

Output 1:

(36)

11. Write a C-program to sort the given array elements in ascending order. Aim: To write a C-program to sort the given array elements in ascending order.

Algorithm: Step 1: Start.

Step 2: Read the value of n.

Step 3: Input array a[i] from the keyboard Step 4: Before sorting print a[i]

Step 5: Initializing i to 0.

Step 6: Repeat steps a to c until i < =n-1 start from i=0.

Step a: Initialize j to i+1.

Step b: Repeat steps i & ii until j <= n-1.

Step i: If a[i] > a[j] then,

temp=a[i]

a[i]=a[j].

a[j]=temp.

Step ii: Increment j by 1.

Step c: Increment i by 1.

Step 7: After sorting print the array a[i]

Step 8: Stop the program.

(37)
(38)

Program: #include<stdio.h> #include<conio.h> void main() { int a[10],n,i,j,temp; clrscr();

printf("\nEnter the array size:");

scanf("%d",&n);

printf("\nEnter the elements into the array:");

for(i=0;i<=n-1;i++)

{

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

}

printf("\nBefore sorting the array elements are:");

for(i=0;i<=n-1;i++)

{

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

}

/* Logic for sorting */

for(i=0;i<=n-1;i++)

{

for(j=i+1;j<=n-1;j++)

{

if(a[i]>a[j])

(39)

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

printf("\nAfter sorting the array elements are....\n\n");

for(i=0;i<=n-1;i++)

{

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

}

getch();

}

References

Related documents

[r]

No obstante, la presencia constituye casi una condición previa para la existencia de controversia, y a pesar de haber obtenido resultados antagónicos para ambas

We all have areas in our lives where we feel stuck, at least one area that we cannot change, but look for what you can change, look for what you can learn, look for what God

This article illustrates how the WSJ can be used in AIS courses to reinforce learning in key areas, such as information privacy, data security, identity theft, and

위의 예는 생산특화(specialization)를 통한 경제개방의 효과를 설명한 것으로, 지식확 산(knowledge spillover) 경로를 통해 경제개방이 숙련 및 미숙련 부문의

Influence of the Depth on the Shape and Thickness of Nacre Tablets of Pinctada margaritifera Pearl Oyster, and on Oxygen Isotopic Composition... Because of the highly organized

Martin Ingram, ‘ Ridings, rough music and mocking rhymes in early modern England ’, in Barry Reay, ed., Popular culture in seventeenth-century England (London,  ), pp..

Note: Service from the Start programs must be purchased up front or within 30 days of the product purchase.