You are on page 1of 33

LOKANTHALLI, BHAKTAPUR

C Programming Practical

SUBMITTED BY: SUBMITTED TO:


Sudhan regmi
Write a program to arrange number in descending order
Algorithm
Step 1: Start
Step 2: Declare the integer a, n, i, j, and number
Step 3: Define the value of n and number
Step 4: Initialize the variables
Step 5: if (number [i] < number [j])
a = number [i]
number [i] = number [j]
number [j] = a
Step 6: Store the output of the step 5.
Step 7: Print the numbers arranged in descending order.
Step 8: End

Flow chart

Start

int a, n, i, j, number

if(number[i]<nu
mber[j])

a=number[i]
number[i]=number[j]
number[j]=a

print the number arranged in descending order

End
//Write a program to arrange number in descending order.
#include<stdio.h>
main(void)
{
int i, j, a, n, number[20];
printf ("\n Enter the number of N:");
scanf ("%d", &n);
printf ("\n Enter the numbers:");
for (i=0; i < n; ++i)
scanf ("%d",& number [i]);
for (i=0; i<n; ++i)

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

{
if (number[i]<number[j])

{
a=number[i];
number [i]=number[j];
number [j]=a;
}
}
}

printf ("\n The number arranged in descending order.");


for (i=0;i<n;++i)
{
printf ("\n %d", number [i]);
}
}

Output result
Write a program to find factorial of given number using
recursion.
Algorithm
Step 1: Start
Step 2: Declare the integer 'factorial', 'a' and 'n'.
Step 3: Define the value of 'n'.
Step 4: Call the factorial (n).
Step 5: if (n = = 1);a(n-1)*n
Step 6: Store the output of step 5.
Step 7: Print the factorial of the input given.
Step 8: End

Flow chart

Start

int factorial, a

call the factorial=a(n)


Yes

if (n==1) a(n-1)*n
No

print the factorial

End

//Write a program to find factorial of given number using recursion.


#include < stdio.h >
int a (int);
main ( )
{
int factorial, n;
printf ("\n Enter the number:");
scanf ("%d",&n);
factorial = a(n);
printf ("\n The factorial of %d is %d ",n, factorial);
}
int a (int n)
{
if (n= =1)
return 1;
else
return(a(n-1)*n);
}

Output result

Write a program to reverse user input integer.


Algorithm
Step 1: Start
Step 2: Declare the integer 'a' and 'b'.
Step 3: Define the value of 'a'
Step 4: while (a!=0) true continue else go to step 7.
Step 5: Set b = b * 10, b = b + a % 10, a = a / 10
Step 6: Store the output of step 4.
Step 7Print the reverse of the given input.
Step 8: End

Flow chart

Start

int a, b

a!=0
False

True

b=b*10, b=b+a%10, a=a/10

print the reverse of given input

End

//Write a program to reverse user input integer.


#include<stdio.h>
main()
{
int a, b = 0;
printf("\n Enter the number:");
scanf("%d",&a);
while(a!=0)
{
b=b*10;
b=b+a%10;
a=a/10;
}
printf("\n Reverse of the given number:%d",b);
}

Output result

Write a program to reverse the user input string.


Algorithm
Step 1: Start
Step 2: Declare the character 'a'.
Step 3: Define the character 'a'
Step 4: Store the output of step 3.
Step 5: Print the reverse of the given string.
Step 6: End

Flow chart

Start

char a

gets (a), sterev (a)

print the reverse of given input

End

//Write a program to reverse the user input using string.


#include<stdio.h>
#include<string.h>
main()
{
char a[50];
printf("\nEnter the string:");
gets (a);

strrev(a);
printf("\nThe reverse of the given string:%s",a);
}

Output result

Write a program to find palindrome.


Algorithm
Step 1: Start
Step 2: Declare variables 'a' and 'b'.
Step 3: Initialize variables 'a' and 'b'.
Step 4: If (strmp (a,b) ==0)
The entered string is palindrome
else
The entered string is not palindrome
Step 5: Print the output of step 4.
Step 6: Stop

Flow chart

Start

char a, b

False
if print the entered string
(strcmp (a,b)==0) is not a palindrome

True

print entered string is a


palindrome

End

//Write a program to find palindrome.


#include<stdio.h>
#include<string.h>
main()
{
char a[50],b[50];
printf("\nEnter the string to check if it is palindorme or not.");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("\nThe entered string is a palindorme.");
else
printf("\n The entered string is not a palindorme.");

Output result

Write a program to count the number of words in a sentence.


Algorithm
Step 1: Start
Step 2: Declare the character 'a' and integer 'count', 'i'.
Step 3: Define the character 'a'.
Step 4: Provide an expression for counting the words.
Step 5: If(a[i]==' ')
count++
Step 6: Store the output of step 5.
Step 7: Print the number of words in the given sentence.
Step 8: End

Flow chart

Start

char a[200], int count=0, i

if (a[i] ==' ')

count the word

End

// Write a program to count the number of words in a sentence.


#include<stdio.h>
#include<string.h>
main()
{
char a[200];
int count=0,i;
printf("Write a sentence below.\n");
scanf("%[^\n]s\n",a);
for(i=0; a[i]!='\0';++i)
{
if (a[i]==' ')
count++;
}
printf("\nThe number of words in the given sentence=%d",count+1);

Output result

Write a program to multiply two 2*2 matrix number.


Algorithm
Step 1: Start
Step 2: Declare the integer 'r=2', 'c=2', 'k', 'a', 'b', 'first', 'second', 'multiply'
Step 3: Define and Initialize the variables 'b', 'k', 'first', 'second' and 'sum'
Step 4: Multiply the two matrix
Step 5: Store the value of step 4.
Step 6: Print the addition of two matrix.
Step 7: End

Output result

Start

int r, c, b, a, first, second, multiply

Enter the element of first matrix

Enter the element of second matrix

sum = sum + first[a][k]*second[k][d]


multiply[a][d] = sum

print the multiplication of matrix

End

//Write a program in c to multiply two 2*2 matrix


#include<stdio.h>
main ()
{
int r=2, c=2, a, b, k, sum=0, first[50][50], second[50][50], multiply[50]
[50];

printf ("Enter the number for first matrix:\n");


for(a=0; a<r; a++)
for(b=0; b<c; b++)
scanf("%d", &first[a][b]);

printf("Enter the number of the second matrix:\n");


for(a=0; a<r; a++)
for(b=0; b<c; b++)
scanf("%d", &second[a][b]);

for (a = 0; a < r; a++)


{
for (b = 0; b < c; b++)
{
for (k = 0; k < r; k++)
{
sum = sum + first[a][k]*second[k][b];
}
multiply[a][b] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (a = 0; a < r; a++)
{
for (b = 0; b < c; b++)
{
printf("%d\t", multiply[a][b]);
}
printf("\n");
}
return 0;
}

Output result

Write a program to preform call by value.


Algorithm
Step 1: Start
Step 2: Declare the integer 'a', 'b', 'x' and 'y'.
Step 3: Define the value of 'a' and 'b'.
Step 4: Swapping the value by calling swap (int x, int y)
Step 5: Store the output of step 4.
Step 6: Print the swap value.
Step 7: End

Flow chart

Start

int a, b, x, y

print the values before swapping

calling the swap Define the swap

print the value after swap

End

//The call by value.


#include<stdio.h>
void swap(int x, int y);
main()
{
int a=100;
int b=200;
printf("\nBefore swap value of a:%d",a);
printf("\nBefore swap value of b:%d",b);
swap(a,b);
printf("\nAfter swap value of a:%d",a);
printf("\nAfter swap value of b:%d",b);
}
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
}

Output result

Write a program to perform call by reference.


Algorithm
Step 1: Start
Step 2: Declare the integer 'a=100', 'b=200', '*x' and '*y'.
Step 3: Define the value of 'a' and 'b'.
Step 4: Swapping the value by calling swap (int *x, int *y)
Step 5: Store the output of step 4.
Step 6: Print the swap value.
Step 7: End

Flow chart

Start

int a, b, *x, *y

print the values before swapping

calling the swap Define the swap

print the value after


swap

End

//the call by reference.


#include<stdio.h>
void swap(int *x, int *y);
main()
{
int a=100;
int b=200;
printf("\nBefore swap value of a:%d",a);
printf("\nBefore swap value of b:%d",b);
swap(&a,&b);
printf("\nAfter swap the value of a:%d",a);
printf("\nAfter swap the value of b:%d",b);
}
void swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

Output result
Write a program to find vowel sound or letter in a sentence
using while loop.
Algorithm
Step 1: Start
Step 2: Declare the character 'q' and integer 'i' and count_vowel
Step 3: Initialize the variable
Step 4: if(q[i]=='a'||q[i]=='e'||q[i]=='i'||q[i]=='o'||q[i]=='u')
count_vowel++
i++
Step 5: Store the output of step 4.
Step 6: Print the counted number of vowel letter
Step 7: End

Flow chart

Start

char q, i, count_vowel

if
(q[i]=='a'||q[i]=='e'||
q[i]=='i'||q[i]=='o'|| print no vowel letter
q[i]=='u')

print the counted number of vowel letter

End
//Write a c program to find vowel sound or letter in a sentence
#include<stdio.h>
int main()
{
char q[100];
int i=0;
int count_vowel=0;
printf("Enter the sentence:\n");
gets(q);
while (i<100)
{
if(q[i]=='a'||q[i]=='e'||q[i]=='i'||q[i]=='o'||q[i]=='u')
count_vowel++;
i++;

}
printf("\nthere is %d vowel sound.",count_vowel);

Output result
Write a program to add two matrix.
Algorithm
Step 1: Start
Step 2: Declare the integer 'r', 'c', 'd', 'e', 'first', 'second', and 'sum'
Step 3: Define and Initialize the variables 'r', 'c', 'd', 'e', 'first', 'second'.
Step 4: Sum the matrix 'first' and 'second'.
Step 5: Store the value of step 4.
Step 6: Print the addition of two matrix.
Step 7: End

Flow chart

Start

int r, c, d, e, first, second, aum

Enter the first matrix

Enter the second matrix

sum[d][e]=first[d][e]+second[d][e]

print the multiplication of matrix

End
//Write a program to add two matrix
#include<stdio.h>
main()
{
int r,c,d,e,first[100][100],second[100][100],sum[100][100];
printf("Enter the two number for matrix row and column:\n");
scanf("%d%d",&r,&c);

printf("\nEnter the first matrix number:");


for (d=0; d<r; ++d)
for(e=0; e<c; ++e)
{
printf ("\nenter the number first %d%d:",d+1,e+1);
scanf("\n%d",&first[d][e]);
}
printf("\nEnter the second matrix number:");
for (d=0;d<r;++d)
for(e=0;e<c;++e)
{
printf("\nEnter the number second %d%d: ",d+1,e+1);
scanf("%d",&second[d][e]);
}
printf("the sum of two matrix:\n");
for (d=0;d<r;++d){
for(e=0;e<c;++e)
{
sum[d][e]=first[d][e]+second[d][e];
printf("%d\t", sum[d][e]);
}
printf("\n");
}

}
Output result
Write a program to multiply two matrix.
Algorithm
Step 1: Start
Step 2: Declare the integer 'r', 'c', 'd', 'k', 'p', 'q', 'first', 'second', 'multiply'
Step 3: Define and Initialize the variables 'r', 'c', 'd' , 'q', 'p', 'first'.
Step 4: if (c! = p)
The matrix can't be multiplied with each other
else
Enter the matrix of second number
Step 5: Define and Initialize 'k', 'second' and 'sum'
Step 6: Multiply the two matrix
Step 7: Store the value of step 4.
Step 8: Print the addition of two matrix.
Step 9: End

Flow chart

Start

int r, c, d, k, p, q, first, second, multiply

print matrix cannot be


if (c != p)
multiplied

Enter the element of second matrix

sum = sum + first[a][k]*second[k][d]


multiply[a][d] = sum

print the multiplication of matrix

End
//Write a program to multiply two matrix.
#include <stdio.h>
int main()
{
int r, c, p, q, a, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter number of rows and columns of first matrix\n");


scanf("%d%d", &r, &c);
printf("Enter elements of first matrix\n");

for (a = 0; a < r; a++)


for (d = 0; d < c; d++)
scanf("%d", &first[a][d]);

printf("Enter number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);
if (c != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
for (a = 0; a < p; a++)
for (d = 0; d < q; d++)
scanf("%d", &second[a][d]);

for (a = 0; a < r; a++) {


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[a][k]*second[k][d];
}
multiply[a][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (a = 0; a < r; a++)
{
for (d = 0; d < q; d++)
{
printf("%d\t", multiply[a][d]);
}
printf("\n");
}
return 0;
}
}

Output result
Write a program to read Fibonacci series.
Algorithm
Step 1: Start
Step 2: Declare variables first, second, n, next, and c.
Step 3: Define the value of 'n' and initialize variables first , next, and second.
Step 4: Display first_term and second_term
Step 5: If (c<=1)
next =c
else
next = first + second
first = second
second = next
Step 6: Store the output of step 5.
Step 7: Print the Fibonacci series
Step 8: End

Flow chart

Start

int n, first, second, next, c

print terms of Fibonacci series

False
if (c<=1) next=c

True

next = first + second, first = second


second = next

print the fibonicca

End
/* Fibonacci series program in C language */
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d", &n);
printf("First %d terms of Fibonacci series are:\n", n);
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
}
return 0;
}

Output result
Write a program to display pattern.
Algorithm
Step 1: Start
Step 2: Declare the integer 'row', 'i', 'j'.
Step 3: Define the value of 'a' and initialize the value of 'row', 's'
Step 4: Provide the expression for the pattern which repeats the process.
Step 5: Store the output of the step 4.
Step 6: Print the result of the input
Step 7: End

Output result

Start

int I, j, rows

expression for half pyramid display

print the result

End
//Write a program in c to show half pyraimd.
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=rows; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}

Output result

You might also like