DEPARTMENT OF ELECTRONICS HINDUSTHAN COLLEGE OF ARTS AND SCIENCE (Autonomous) (Affiliated to Bharathiar University, Coimbatore-46) Coimbatore-641028. HICAS 2018-2019 II-B.Sc., Electronics and Communication Systems Name :………………………………………………… Reg. No :…………………………………………………. Practical III: C Programming Lab (17ELU11)
43
Embed
Practical III: C Programming Lab (17ELU11)€¦ · FIND THE GIVEN NUMBER IS PRIME OR NOT AIM: To write a ‘C’ program to find whether the given number is prime number or not. ALGORITHM:
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
DEPARTMENT OF ELECTRONICS
HINDUSTHAN COLLEGE OF ARTS AND SCIENCE (Autonomous)
(Affiliated to Bharathiar University, Coimbatore-46)
Coimbatore-641028.
HICAS
2018-2019
II-B.Sc., Electronics and Communication Systems
Name :…………………………………………………
Reg. No :………………………………………………….
Practical III: C Programming Lab (17ELU11)
HINDUSTHAN COLLEGE OF ARTS AND SCIENCE (Autonomous)
(Affiliated to Bharathiar University, Coimbatore)
Coimbatore-641028
Reg. No:…………………………………
CERTIFICATE
Certified that this is the bona-fide record of work done by
Mr./Ms…………………………………………………….……. of the III semester of B.Sc., ECS during the academic year 2018-2019.
FACULTY HOD
Submitted for the Practical Examination held on ……………………………at
Hindusthan College of Arts and Science (Autonomous), Coimbatore-28.
Internal Examiner External Examiner
DEPARTMENT OF ELECTRONICS
S.
No
Date Name of the Programs Page
Number
Signature
of the
Faculty
INDEX
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
FIND THE GIVEN NUMBER IS ODD OR EVEN
AIM:
To write a ‘c’ program to find whether the given number is odd or
even using if else statement.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the variable ‘a’ as integer.
STEP-3: Read the value for the variable a.
STEP-4: Find whether the entered number is odd or even number
using if statement and % operator.
STEP-5: Check the condition using ‘IF’ statement, if the condition is
true display given number is EVEN. Otherwise display the given
number is ODD.
STEP-6: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter The Given Number \n");
scanf("%d",&a);
if(a%2==0)
{
printf("The Even Number is:%d\n",a);
}
else
{
printf("The Odd Number is:%d\n",a);
}
getch();
}
RESULT:
The ‘C’ program to find whether the given number is Odd or
Even is developed using If statement and its output is verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
FIND THE GIVEN NUMBER IS PRIME OR NOT
AIM:
To write a ‘C’ program to find whether the given number is prime
number or not.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the variable n as integer.
STEP-3: Read the value for the variable ‘n’.
STEP-4: Find whether the entered number is PRIME NUMBER or
NOT
STEP-5: Display the Corresponding result.
STEP-6: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
{
printf("Given Number is Prime Number\n");
}
else
{
printf("Given Number is Not a Prime Number\n");
}
getch();
}
RESULT:
The ‘C’ program to find whether the given number is prime or not is
performed using entry controlled looping statement ‘FOR’ and its
output are verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
FIND THE SUM OF GIVEN DIGITS
AIM:
To write a ‘c’ program to find the sum of given digits.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the variables n, i, sum=0.
STEP-3: Read the values for the variables.
STEP-4: Using while statement find the sum of individual digits of
given number.
STEP-5: Display the result.
STEP-6: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("Enter the Number\n");
scanf("%d",&n);
while(n!=0)
{
i=(n%10);
sum=sum+i;
n=n/10;
}
printf("The Sum of Given all Digits is:%d\n",sum);
getch();
}
RESULT:
The ‘C’ program to predict the sum given digits are performed and its
output is verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
FIND WHETHER THE GIVEN NUMBER IS
ARMSTRONG OR NOT
AIM:
To write a ‘C’ program to find whether the given number is
Armstrong or not.
ALGORITHIM:
STEP-1: Start the process.
STEP-2: Declare the variables n, num, temp, sum, rem, cube as
integer.
STEP-3: Assign the initial value 0 to sum and cube.
STEP-4: Using while iteration scheme find whether the given number
is Armstrong number or not.
STEP-5: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,num,temp,sum=0,rem=0,cube=0;
clrscr();
printf("Enter a Number :\n");
scanf("%d",&num);
temp=num;
while(num!=0)
{
rem=num%10;
cube=pow(rem,3);
sum=sum+cube;
num=num/10;
}
if(sum==temp)
printf("The Given Number is Armstrong\n");
else
printf("Given Number is Not an Armstrong\n");
getch();
}
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
RESULT:
The ‘C’ program to find whether the given number is Armstrong or
not is developed and its output is verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
GREATEST AMONG THREE NUMBERS
AIM:
To write a ‘C’ program to find the greatest number among three
numbers using conditional operators.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the variable a, b and c result as integer.
STEP-3: Enter the three numbers.
STEP-4: Predict the greatest number using the conditional operators.
STEP-5: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,r1,r2;
clrscr();
printf("Enter the Value of a,b & c Respectively\n");
scanf("%d%d%d",&a,&b,&c);
r1=(a>b)?a:b;
r2=(r1>c)?r1:c;
printf("Among the Three Numbers %d is Greatest Number\n",r2);
getch();
}
RESULT:
The greatest number among given three numbers was predicted using
conditional operators.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
FIBONACCI SERIES
AIM:
To write a ‘C’ program to generate the Fibonacci series of ‘n’ terms.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the local variables n, f1, f2, fib and i as integers.
STEP-3: Assign the initial values 0 to f1 and 1 to f2.
STEP-4: Using ‘for’ iteration scheme generate and print the Fibonacci
series for N terms.
STEP-5: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
// Fibonacci Series Generation for N terms
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f1,f2,fib,i;
f1=0;
f2=1;
clrscr();
printf("enter the n th terms of fibonacci series \n");
scanf("%d",&n);
printf("\n\n fibonacci series \n");
printf("%d\n",f1);
printf("%d\n",f2);
for(i=2;i<=n;i++)
{
fib=f1+f2;
f1=f2;
f2=fib;
printf("%d\n",fib);
}
getch();
}
RESULT:
The Fibonacci series is generated for N terms and its output is
verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
SORT GIVEN SET OF NUMBER
AIM:
To write a ‘c’ program to sort the given set of numbers in ascending
order.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the required variable along with the array variable.
STEP-3: Read how many values you will going to sort.
STEP-4: Get the value the value for array variable.
STEP-5: Compare the first element to all elements in the array, If the
first element is greater than the second, interchange the value by using
the temporary variable.
STEP-6: Continue the above statement for the entire element in the
array.
STEP-7: Arrange the given numbers in ascending order in the array.
STEP-8: Print the values of array elements.
STEP-9: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,t;
clrscr();
printf("Enter the nth value of Number\n");
scanf("%d",&n);
printf("Enter the %d\t numbers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(j=i+1;j<n-1;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
clrscr();
printf("The Given set of number be sorted in Ascending Order\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
RESULT:
The given set of numbers sorted in ascending order and its output is
verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
SORTING SET OF STRINGS
AIM:
To write a ‘C’ program to sort given set of strings in ascending /
descending order.
ALOGORITHM:
STEP-1: Start the process.
STEP-2: Declare the variables i,j,a,t.
STEP-3: Declare a one dimensional character arrays a and t with the
size of 10.
STEP-4: Using “for” iteration scheme sort given set of strings.
STEP-5: Display the sorted strings.
STEP-6: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n;
char a[10][10],t[10];
clrscr();
printf("\n Enter the number of string....\n");
scanf("%d",&n);
printf("Enter %d Names by.....\n",n);
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(a[i],a[j])>0)
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
{
strcpy(t,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],t);
}
}
}
clrscr();
printf("The sorted Strings list is.....\n");
for(i=0;i<n;i++)
{
printf("%s\n",a[i]);
}
getch();
}
RESULT:
The given set of strings were sorted in either ascending order or
descending order and compiled out.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
FIND THE GIVEN STRING IS PALINDROME
OR NOT
AIM:
To write a ‘C’ program to determine whether given string is
palindrome or not.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the variable as character type.
STEP-3: STRCPY the b to a.
STEP-4: STRREV the b.
STEP-5: Use if….else statement and use the condition (STRCMP
(a,b)==0)
STEP-6: If the enter number is PALINDROME it displays the result
“GIVEN STRING IS A PALINDROME”.
STEP-7: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[20];
clrscr();
printf("Enter a String\n");
scanf("%s",&a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
{
printf("The Given String is PALINDROME\n");
}
else
{
printf("The Given String is not a PALINDROME\n");
}
getch();
}
RESULT:
The C program for predicting the given string is palindrome or not is
determined using string related functions and its output is verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
MATRIX ADDITION
AIM:
To write a ‘C’ program to add two m × n matrix using two
dimensional arrays.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the required variables and the function to manipulate
the matrix addition.
STEP-3: Get the value for 2 array of m × n matrix.
STEP-4: Add two Matrix.
STEP-5: Print the resultant Matrix.
STEP-6: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,n;
clrscr();
printf("Enter the Matrix Dimension\n");
scanf("%d",&n);
clrscr();
printf("Enter the values for Matrix a\n");
for (i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
clrscr();
printf("Enter the values for Matrix b\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
clrscr();
printf("The Resultant Matrix....\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
RESULT:
The ‘C’ program for adding two m × n matrix is performed and its
output is verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
MATRIX SUBTRACTION
AIM:
To write a ‘C’ program to subtract one m × n matrix from other m ×n
matrix using two dimensional arrays.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the required variables and the function to manipulate
the matrix subtraction.
STEP-3: Get the value for 2arrays of m × n matrix.
STEP-4: Subtract one Matrix from other.
STEP-5: Print the resultant Matrix.
STEP-6: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,n;
clrscr();
printf("Enter the Matrix Dimension\n");
scanf("%d",&n);
clrscr();
printf("Enter the values for Matrix a\n");
for (i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
clrscr();
printf("Enter the values for Matrix b\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
clrscr();
printf("The Resultent Matrix....\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
RESULT:
The ‘C’ program for subtracting one m × n Matrix from other m × n
matrix is performed and its output is verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
MATRIX MULTIPLICATION
AIM:
To write a c program to find the matrix multiplication for the given set
of numbers.
ALGORTHIM:
STEP-1: Start the process.
STEP-2: Declared the required variables and the function to
manipulate the matrix multiplication.
STEP-3: Get the values for 2 arrays of m × n matrix.
STEP-4: Multiply the two Matrix.
STEP-5: Print the resultant matrix.
STEP-6: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,n;
clrscr();
printf("Enter the Matrix Dimension\n");
scanf("%d",&n);
clrscr();
printf("Enter the values for Matrix a\n");
for (i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
clrscr();
printf("Enter the values for Matrix b\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]*b[i][j];
}
}
clrscr();
printf("The Resultant Matrix....\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
RESULT:
The ‘C’ program for multiplying two m × n is performed and its
output is verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
FACTORIAL FOR THE GIVEN NUMBER
AIM:
To write a ‘C’ program to find the factorial for the given number.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the variable ‘a’ as integer.
STEP-3: Enter the number to the variable to which we want to print
the factorial.
STEP-4: Again declare the variables fact, count=1 as long integer.
STEP-5: Use while loop and give the condition count<=a.
STEP-6: If the condition is true ,the fact value is multiplied with count
and count is incremented by 1.
STEP-7: Above process is repeated until the condition becomes true.
STEP-8: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void fact(int a);
void main()
{
int a,t;
clrscr();
printf("Enter The Numbeer\n");
scanf("%d",&a);
fact(a);
getch();
}
void fact (int a)
{
int fact=1,count=1;
clrscr();
while(count<=a)
{
fact=fact*count;
count++;
}
printf("The Factorial of the Given Number is: %d!=%d\n",a,fact);
return;
}
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
RESULT:
The factorial for the given number determined using C function.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
SUM AND AVERAGE OF SET OF NUMBERS
AIM:
To write a ‘C’ program to compute the sum and average of given set
of numbers.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Declare the variable n, i as integer type and average, sum=0
as float type.
STEP-3: Enter a set of numbers.
STEP-4: Using a for loop find the sum and average of set of numbers.
STEP-5: Display the sum and average for given set of numbers.
STEP-6: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num[20],i;
float avg,sum=0;
clrscr();
printf("Enter the n term of number \n");
scanf("%d",&n);
printf("Enter %d Numbers One After Another\n",n);
for (i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
for (i=0;i<n;i++)
{
sum=sum+num[i];
}
avg=sum/n;
printf("The Sum of Given Set of Numbers are:%f\n",sum);
printf("The Average of Given Set of Numbers are:%f\n",avg);
getch();
}
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
RESULT:
The sum and average of the given number is displayed and its
output is verified.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
MARK SHEET PREPARATION USING
STRUCTURE
AIM:
To write a ‘C’ program to display the mark sheet details of a set of
students.
ALGORITHM:
STEP-1: Start the process.
STEP-2: Create structure named students.
STEP-3: Declare the variable name as char, roll no and subjects as
into and tot & avg as float.
STEP-4: Create an array of structure for 10 students.
STEP-5: Enter the student details one after another.
STEP-6: Use for iteration scheme determine sum and average.
STEP-7: The mark sheet details for set of students are displayed.
STEP-8: Stop the process.
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
struct students
{
char name[10];
int rollno;
int ec,de,pe,c;
float tot,avg;
};
struct students s[10];
int n,i;
clrscr();
printf("Enter Total no of Students in the Class\n");
scanf("%d",&n);
printf("Enter The %d no of Students Details One After Another \n",n);
for(i=0;i<n;i++)
{
printf("Enter The %d Student Details \n",i+1);
printf("Enter The Name of the Student\n");
scanf("%s",s[i].name);
printf("Enter The Roll no of the Student\n");
Ex. No: Date:
Department of Electronics, HICAS, CBE-28. Page No:
scanf("%d",&s[i].rollno);
printf("Enter the Marks of 1.EC, 2.DE, 3.PE, 4.C respectively\n");