Professional Documents
Culture Documents
CS Iyr P II Practical 2019
CS Iyr P II Practical 2019
Step 8:end
FLOWCHART:
#include <stdio.h>
#include<conio.h>
void main()
{
int num1, num2, num3;
clrscr();
printf("Enter the values of num1, num2 and num3\n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2)
{
if (num1 > num3)
printf("%d is the greatest among three \n",num1);
else
printf("%d is greatest among three \n",num3);
}
else
{
if (num2 > num3)
printf("%d is the greatest among three \n",num2);
else
printf("%d is the greatest among three \n",num3);
}
getch();
}
OUTPUT:
89
65
78
OUTPUT:
Factorial of 5 = 120
OUTPUT:
How many rows and columns? 2
Enter the first matrix:
56
23
Enter second matrix:
46
24
Matrix after addition:
9 12
47
4) Write a C program to create a single dimensional array of numbers and display the contents and
arrange this single dimensional array of numbers into descending order
#include <stdio.h>
#include<conio.h>
#define MAX 100
int main()
{
int arr[MAX],n,i,j;
int temp;
clrscr();
printf("Enter total number of elements: ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i< n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
printf("The elements of array before sorting are :\n");
for(i=0;i<n;i++)
printf("%d\n",arr[i]);
for(i=0;i< n;i++)
{
for(j=i+1;j< n;j++)
{
if(arr[i]<arr[j])
{
temp =arr[i];
arr[i] =arr[j];
arr[j] =temp;
}
}
}
printf("\nArray elements after sorting are:\n");
for(i=0;i< n;i++)
{
printf("%d\n",arr[i]);
}
getch();
return 0;
}
OUTPUT:
Enter total number of elements: 6
Enter array elements:
Enter element 1: 25
Enter element 1: 35
Enter element 1: 84
Enter element 1: 12
Enter element 1: 36
Enter element 1: 98
The elements of array before sorting are :
25
35
84
12
36
98
Array elements after sorting are:
98
84
36
35
25
12
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter rows and columns of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of second matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("\nEnter first matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
printf("\nEnter second matrix:\n");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&b[i][j]);
printf("\Multiplication of Matrices is :\n");
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf("%d ",c[i][j]);
}
printf("\n");
}
}
else
printf("\nSorry!!!! Matrix multiplication can't be done");
getch();
return 0;
}
OUTPUT:
Multiplication of Matrices is
30 24 18
84 69 54
138 114 90
6 ) write a C program to Generate a multiplication Table of given number and given limit using
for loop
#include <stdio.h>
#include<conio.h>
int main()
{
int n, i,l;
clrscr();
printf("Enter a number and limit for multiplication table: ");
scanf("%d %d",&n, &l);
for(i=1; i<=l; i++)
{
printf("%d * %d = %d \n", n, i, n*i);
}
getch();
return 0;
}
OUTPUT
7) write a C program to generate Fibonacci series using Recursion for given limit.
#include<stdio.h>
#include<conio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
clrscr();
printf("Enter the limit for fibonacci series : ");
scanf("%d",&n);
printf("Fibonacci series :\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
getch();
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
OUTPUT
0
1
1
2
3
5
8
13
21
34
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
float p,r,SI,CI;
int n;
printf("Enter the value of Principal p = ");
scanf("%f",&p);
printf("Enter the value of Rate r = ");
scanf("%f",&r);
printf("Enter the value of Period in year n = ");
scanf("%d",&n);
SI = ((p*r*n)/100);
printf("Simple Interest SI=%f \n",SI);
ci=p*pow(1+r/100,n);
printf("Compound Interest CI=%f \n",CI);
getch();
return 0;
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
int main()
{
float celsius, fahrenheit;
clrscr();
printf("Please Enter temperature in Celsius: \n");
scanf("%f", &celsius);
fahrenheit = ((celsius * 9)/5) + 32;
printf("\n %.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);
getch();
return 0;
}
OUTPUT:
#include <stdio.h>
#include<conio.h>
int main()
{
int i, num, isPrime;
isPrime = 1;
clrscr();
printf("Enter any number to check prime: ");
scanf("%d", &num);
for(i=2; i<=num/2; i++)
{
if(num%i==0)
{
isPrime = 0;
break;
}
}
if(isPrime == 1)
{
printf("%d is prime number", num);
}
else
{
printf("%d is composite number", num);
}
getch();
return 0;
}
OUTPUT:
9 b) write a function and call function to perform add, subtract and multiply two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter Two Values :");
scanf("%d%d",&a,&b);
sum(a,b);
mult(a,b);
sub(a,b);
getch();
}
sum(int x,int y)
{
int z;
z=x+y;
printf("Addtion : %d\n",z);
return 0;
}
mult(int x,int y)
{
int z;
z=x*y;
printf("Multiply : %d\n",z);
return 0;
}
sub(int x,int y)
{
int z;
z=x-y;
printf("Subtraction : %d\n",z);
return 0;
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
clrscr();
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if (year%400 == 0)
printf("%d is a leap year.\n", year);
else if (year%100 == 0)
printf("%d isn't a leap year.\n", year);
else if (year%4 == 0)
printf("%d is a leap year.\n", year);
else
printf("%d isn't a leap year.\n", year);
getch();
return 0;
}
OUTPUT:
Link Section : This part of the code is used to declare all the header files that will be used in the
program. This leads to the compiler being told to link the header files to the system libraries.
Ex:- #include<stdio.h>
Definition Section : In this section, we define different constants. The keyword define is used in this
part.
Ex: #define PI=3.14
Global Declaration Section : This part of the code is the part where the global variables are
declared. All the global variable used are declared in this part. The user-defined functions are also
declared in this part of the code.
Ex: float area(float r);
int a=7;
Main Function Section : Every C-programs needs to have the main function. Each main function
contains 2 parts.
1) A declaration part : The declaration part is the part where all the variables are declared.
2) Execution part. The execution part begins with the curly brackets and ends with the curly
close bracket. Both the declaration and execution part are inside the curly braces.
Ex : void main()
{
Local declarations( ex int I;) Declaration part
Statement1;
Statement 2;
.
. Execution part
.
Statement n;
Sub Program Section : All the user-defined functions are defined in this section of the program.
Ex : int add(int a, int b)
{
return a+b;
}
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
clrscr();
strcpy(str3, str1);
printf("The copied string is : %s\n", str3 );
strcat( str1, str2);
printf("The concatenated string is: %s\n", str1 );
len = strlen(str1);
printf("Length of the string is : %d\n", len );
strrev(str3);
printf("The reversed string is : %s\n", str3 );
getch();
return 0;
}
OUTPUT:
Ans) Structure: Structure is a collection of variables (can be of different data types) stored
under a single name.
Union: A union is a special data type available in C that allows to store different data types in
the same memory location. You can define a union with many members, but only one member
can contain a value at any given time.