0% found this document useful (0 votes)
133 views41 pages

C Programming Complete Lab 1-17

Raptor is a popular tool for designing and evaluating flowcharts. It was originally developed by the US Air Force Academy for computer science education and is now used in over 28 countries. Raptor has six basic symbols - Input, Output, Assignment, Selection, Loops, and Call - that each represent a unique type of instruction. Key features of Raptor include allowing students to prepare flowcharts, visualize algorithms, perform flowchart tracing, and generate code like C++ and Java from flowcharts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
133 views41 pages

C Programming Complete Lab 1-17

Raptor is a popular tool for designing and evaluating flowcharts. It was originally developed by the US Air Force Academy for computer science education and is now used in over 28 countries. Raptor has six basic symbols - Input, Output, Assignment, Selection, Loops, and Call - that each represent a unique type of instruction. Key features of Raptor include allowing students to prepare flowcharts, visualize algorithms, perform flowchart tracing, and generate code like C++ and Java from flowcharts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 41

Raptor

RAPTOR is one of the popular tools to design and evaluate Flowchart.


RAPTOR was originally developed by and for the US Air Force Academy, Department of Computer Science,
but its use has spread and RAPTOR is now used for CS education in over 28 countries.
RAPTOR has six symbols, where each symbol represents a unique type of instruction. The basic symbols
are Assignment, Call, Input, Output, Selection and Loops.
1) Input symbol allows user to enter data. Such data is stored in some variable.
2) Output symbol is used to display value of a variable.
3) Assignment symbol can be used for assigning and processing values of variables.
4) Selection symbol allows you to make "decisions" in your flowchart. It is similar to if, if…else and switch
statement of C language. A selection statement requires an expression that can be evaluated into a
"Yes/No". Based on evaluation answer, flowchart will take further action.
5) Loop symbol allows us to repeat one or more statements until some given condition becomes true. It is
similar to while, do….while and for loop of C language.

Key features of RAPTOR


1) Student can prepare flowchart. RAPTOR is a flowchart-based programming environment.
2) Student can visualize their algorithms.
3) Flowchart tracing is possible in RAPTOR.
4) RAPTOR can generate C++, Java code from the given Flowchart.
============================================================================

Exercise 1
Construct Flowcharts for the following through Raptor:
I) Develop a calculator to convert time, distance, area, volume and temperature from one unit to another.

II) Calculate simple and compound interest for various parameters specified by the user.
Simple Interest
Compound Interest

III) Calculate the average of n numbers.


===============================================================================

Exercise 2
A) Write a c program to calculate the area of triangle using the formula
Area= (s*(s-a) *(s-b) *(s-c))/2 Where s= (a+b+c)/2
#include <stdio.h>
#include <math.h>
void main()
{
int s, a, b, c, area;
  clrscr();
printf("Enter the values of a, b and c \n");
scanf("%d %d %d", &a, &b, &c);
/* compute s */
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("Area of a triangle = %d \n", area);
getch();
}
Output:
Enter the values of a, b and c
10 12 14
Area of a triangle = 58

B) Write a c program to find the largest of three numbers using ternary operator.
# include <stdio.h>
# include <conio.h>
void main() // program execution starts from here
{
int a, b, c, big ; //variable declaration
clrscr() ;
printf("Enter three numbers : ") ; //asking user to enter 3 numbers
scanf("%d %d %d", &a, &b, &c) ; //reading user entered 3 numbers
//Ternary operator code logic to find the biggest number
big = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c) ;
printf("\nThe biggest number is : %d", big) ; //printing biggest number
getch() ;
}
Output:
Enter three numbers: 77 55 01
The biggest number is: 77

C) Write a c program to swap two numbers without using a temporary variable.


#include <stdio.h>
#include<conio.h>
void main()
{
int firstNumber, secondNumber;
printf("Enter first number: ");
scanf("%d", &firstNumber);
printf("Enter second number: ");
scanf("%d",&secondNumber);
// Swapping process
firstNumber = firstNumber - secondNumber;
secondNumber = firstNumber + secondNumber;
firstNumber = secondNumber - firstNumber;
printf("\nAfter swapping, firstNumber = %d", firstNumber);
printf("After swapping, secondNumber = %d", secondNumber);
getch();
}
Output:
Enter first number: 10
Enter second number: 15
After swapping, firstnumber= 15
After swapping, secondnumber= 10
===============================================================================

Exercise 3
A) Write a C Program to Find Roots of a Quadratic Equation
An equation of degree 2 is known as a quadratic equation. If you’re not familiar with the term ‘degree’, it is an
order of an equation and its value is equal to the largest exponent present in an equation.
Degree Equation
1 Linear
2 Quadratic
3 Cubic

// Program to find Quadratic Equation


#include <stdio.h>
#include<conio.h>
#include <math.h>
void main()
{
int a,b,c,d;
float x1,x2;
clrscr();
printf("Input the value of a,b & c : ");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;

if(d==0)
{
printf("Both roots are equal.\n");
x1=-b/(2.0*a);
x2=x1;
printf("First Root Root1= %f\n",x1);
printf("Second Root Root2= %f\n",x2);
}

else if(d>0)
{
printf("Both roots are real and diff-2\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("First Root Root1= %f\n",x1);
printf("Second Root root2= %f\n",x2);
}
else
printf("Root are imeainary;\n");
getch();
}
Output:
Input the value of a,b & c : 1 5 7
Root are imaginary;

B) Write a C program which takes two integer operands and one operator from user and perform the
operation and prints the result (Consider the operators + - * / % and use switch statement
// program using switch statement
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);
switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}
Output:
Enter you operator (+, -, /, *, %)
+
Enter the values of a and b
13
addition of two numbers is 4
===============================================================================

Exercise 4
A) Write a C program to find the sum of individual digits of a positive integer and find the reverse
of the given number.
#include<stdio.h>
#include<conio.h>
void main( )
{
int num,dup,sum,rev;
printf("Enter a +ve integer: ");
scanf("%d”, &num);
dup=num;
sum=0;
while(num>0)
{
sum+=num%10;
num/=10;
}
printf("Sum of individual digits is: %d\n",sum);
rev=0;
while(dup>0)
{
rev=rev*10+(dup%10);
dup/=10;
}
printf("reverse of the given number: %d\n",rev);
Output:
Enter a +ve integer: 123
Sum of individual digits is:6
Reverse of the given number
is:321

B) Draw the flow chart using Raptor and write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Flowchart
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, count;
clrscr();
printf("Prime no.series\n");
printf("Enter any number\n");
scanf("%d", &n);
printf("The prime numbers between 1 to %d\n",n);
for(i = 1; i <= n; i++)
{
count = 0;
for(j = 1; j <=i; j++)
if(i % j == 0)
{
count++;
}
if(count == 2)
{
printf("%d\t", i);
}
}
getch();
}

Output:
Prime no. series
Enter any number
10
The prime numbers between 1 to 10
2 3 5 7
==============================================================================

Exercise 5
A) Draw the flow chart using Raptor and write a C Program to print the multiplication table of a
given number n up to a given value, where n is entered by the user.
Flowchart

#include <stdio.h>
int main()
{
int number, i = 1;
printf(" Enter the Number:");
scanf("%d", &number);
printf("Multiplication table of %d:\n ", number);
printf("--------------------------\n");
while (i <= 10)
{
printf(" %d x %d = %d \n ", number, i, number * i);
i++;
}
return 0;
}
Output:
Enter the Number:6
Multiplication table of 6:
--------------------------
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

B) Write a C Program to enter a decimal number, and calculate and display the binary equivalent
of that number.
#include <stdio.h>
int main()
{
int number, n, remainder, binary = 0, place = 1;
printf("Enter a number :");
scanf("%d", &number);
n = number;
while (n > 0)
{
remainder = n % 2;
binary += remainder * place;
place *= 10;
n /= 2;
}
printf("Binary equivalent of %d is %d", number, binary);
return 0;
}

C) Write a C Program to check whether the given number is Armstrong number or not.
Eg: 153 = (1)3 + (5)3 + (3)3
153 = 1 + 125 + 27
153 = 153
#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}
Output:
Enter a three digit integer: 371
371 is an Armstrong number.
==============================================================================
Exercise 6
A) Write a C program to interchange the largest and smallest numbers in the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],max,min,maxpos,minpos,i,temp;
printf("Enter 5 integers: ");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
maxpos=0;
minpos=0;
for(i=1;i<5;i++)
{
if(a[i]>max)
{
max=a[i];
maxpos=i;
}
if(a[i]<min)
{
min=a[i];
minpos=i;
}
}
temp=a[maxpos];
a[maxpos]=a[minpos];
a[minpos]=temp;
printf("After interchange array elemnts are: ");
for(i=0;i<5;i++)
printf("%d ",a[i]);
getch();
}
Output:
Enter 5 integers: 2 5 4 1 3
After interchange array elements are: 2 1 4 5 3

B) Write a C program to input two m x n matrices, check the compatibility and perform addition
and multiplication of them.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3]={0}, d[3][3]={0};
int i,j,k,m,n,p,q;
printf("Enter no. of rows and columns in matrix A: ");
scanf("%d%d",&m,&n);
printf("Enter no. of rows and columns in matrix B: ");
scanf("%d%d",&p,&q);
if(m!=p || n!=q)
{
printf("Matrix Addition is not possible");
return;
}
else if(n!=p)
{
printf("Matrix Multiplication is not possible");
return;
}
else
{
printf("Enter elements of matrix A: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
printf("Enter elements of matrix B: ");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d", &b[i][j]);
//Matrix Addition
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j] = a[i][j] + b[i][j];
printf("\nResult of Matirx Addition:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ", c[i][j]);
printf("\n");
}
//Matrix Multiplication
for(i=0;i<m;i++)
for(j=0;j<q;j++)
d[i][j]=0;
for(k=0;k<p;k++)
d[i][j] += a[i][k]*b[k][j];
printf("\nResult of Matirx Multiplication:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d ", d[i][j]);
printf("\n");
}
}
getch();
}
Output:
1
2
3
4
5
6
Enter no. of rows and columns in matrix A: 3 3
7
Enter no. of rows and columns in matrix B: 3 3
8
Enter elements of matrix A:
9
111
1
222
0
333
1
Enter elements of matrix B:
1
222
1
222
2
222
1
 
3
Result of Matirx Addition:
1
333
4
444
1
555
5
 
1
Result of Matirx Multiplication:
6
666
1
12 12 12
7
18 18 18
1
8
1
9
2
0
===============================================================================

Exercise 7
A) Draw the flow chart using Raptor and write a C Program to find both the largest and smallest number
of an array of integers
Flowchart

#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
  for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
  return 0; }

Output:
How many elements: 5
Enter the Array: 1 8 12 4 6
The largest element is 12
The smallest element is 1

B) Write a C Program to find transpose of a matrix.


For example, consider the following 3 X 2 matrix:
12
34
56
Transpose of the matrix:
135
246

#include <stdio.h>
int main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter elements of the matrix\n");
   for (c = 0; c < m; c++)
      for(d = 0; d < n; d++)
         scanf("%d", &matrix[c][d]);
   for (c = 0; c < m; c++)
      for( d = 0 ; d < n ; d++ )
         transpose[d][c] = matrix[c][d];
   printf("Transpose of the matrix:\n");
   for (c = 0; c < n; c++) {
      for (d = 0; d < m; d++)
         printf("%d\t", transpose[c][d]);
      printf("\n");
   }    return 0; }
Output:

Exercise 8
Draw the flow chart using Raptor and write C programs that use both recursive and non-recursive
functions for the following
A) To find the factorial of a given integer.
Flowchart Using Recursion

Flowchart without Using Recursion


#include <stdio.h>
#include <conio.h>
void main()
{
int n, a, b;
clrscr();
printf("Enter any number\n");
scanf("%d", &n);
a = recfactorial(n);
printf("The factorial of a given number using recursion is %d \n", a);
b = nonrecfactorial(n);
printf("The factorial of a given number using nonrecursion is %d ", b);
getch();
}
int recfactorial(int x)
{
int f;
if(x == 0)
{
return(1);
}
else
{
f = x * recfactorial(x - 1);
return(f);
}
}
int nonrecfactorial(int x)
{
int i, f = 1;
for(i = 1;i <= x; i++)
{
f = f * i;
}
return(f);
}
Output:
Enter any number
5
The factorial of a given number using recursion is 120
The factorial of a given number using nonrecursion is 120

B) To find the GCD (greatest common divisor) of two given integers.


Flowchart Using Recursion

Flowchart using Non Recursion


#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
int gcd(int num1, int num2)
{
int i, hcf;
if(num1 < num2)
{
for(i = 1; i <= num1; i++)
{
if((num1 % i == 0) && (num2 % i == 0))
hcf = i;
}
}
else
{
for(i = 1; i <= num2; i++)
{
if((num1 % i == 0) && (num2 % i == 0))
hcf = i;
}
}
return hcf;
}

int rgcd(int x, int y)


{
if(x>y)
rgcd(x-y,y);

else if(y>x)
rgcd(x,y-x);
else
return x;
}
int main(int argc, char **argv)
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
printf("\nGCD of %d and %d using non-recursion is: %d", num1, num2, gcd(num1, num2));
printf("\nGCD of %d and %d using recursion is: %d", num1, num2, rgcd(num1, num2));
    getch();
    return 0;
}
Output:

Enter two numbers: 24 6


GCD of 24 and 6 using non-recursion is: 6
GCD of 24 and 6 using recursion is: 6
================================================================================
Exercise 9
Draw the flow chart using Raptor and write a C Program for the following.
A) To find Fibonacci sequence
Flowchart
#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z,num;
 printf("Enter any last number of Fibonacci series : ");
 scanf("%d",&num);
 x=0;
 y=1;
 while(num>=x)
 {
    z=x+y;
    printf("%d\t",x);
    x=y;
    y=z;
 }
 getch();
 return 0;
}
Output:
Enter last number where goes to end Fibonacci series: 70
0  1  1  2  3  5  8  13  21  34  55

B) Write C programs illustrating call by value and call by reference concepts.


Call by Value
#include <stdio.h>
main ( )
{
int x = 2, y = 3;
clrscr();
void swap(int, int);
printf (“\n Values before swapping are %d %d”, x, y);
swap (x, y);
printf (“\n Values after swapping are %d %d”, x, y);
getch();
}
void swap( int a, int b )
{
int t;
t = a;
a = b;
b = t;
}

Call by reference
#include<stdio.h>
#include<conio.h>
void swaping(int *x, int *y);
int main()
{
 int n1,n2;
 printf("Enter first number (n1) : ");
 scanf("%d",&n1);
 printf("Enter second number (n2) : "); 
 scanf("%d",&n2);
 printf("\nBefore swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 swaping(&n1,&n2);
 printf("\nAfter swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 getch();
 return 0;
}
void swaping(int *x, int *y)
{
  int z;
  z=*x;
  *x=*y;
  *y=z;
}
Exercise 10
Write C Programs for the following string operations without using the built in functions to
concatenate two strings
A) To append a string to another string
#include <stdio.h>
#include <string.h>
int main()
{
char Str1[100], Str2[100];
int i, j;
printf("\n Please Enter the First String : ");
gets(Str1);
printf("\n Please Enter the Second String : ");
gets(Str2);
// To iterate First String from Start to end
for (i = 0; Str1[i]!='\0'; i++);
// Concatenating Str2 into Str1
for (j = 0; Str2[j]!='\0'; j++, i++)
{
Str1[i] = Str2[j];
}
Str1[i] = '\0';
printf("\n String after the Concatenate = %s", Str1);
return 0;
}
Output:
Please enter first string: Hello
Please enter the second string: World
String after the concatenate= HelloWorld

B) To compare two strings


#include <stdio.h>
#include <string.h>
int main()
{
char Str1[100], Str2[100];
int result, i;
printf("\n Please Enter the First String : ");
gets(Str1);
printf("\n Please Enter the Second String : ");
gets(Str2);
for(i = 0; Str1[i] == Str2[i] && Str1[i] == '\0'; i++);
if(Str1[i] < Str2[i])
{
printf("\n str1 is Less than str2");
}
else if(Str1[i] > Str2[i])
{
printf("\n str2 is Less than str1");
}
else
{
printf("\n str1 is Equal to str2");
}
return 0;
}
Output:
Please Enter the First String: hello
Please Enter the Second String: world
str1 is Less than str2

Exercise 11
Write C Programs for the following string operations without using the built in functions
A) To find whether a given string is palindrome or not
#include <stdio.h>
int main()
{
char text[100];
int begin, middle, end, length = 0;
printf("Enter a string:");
gets(text);
while (text[length] != '\0')
length++;
end = length - 1;
middle = length/2;
for (begin = 0; begin < middle; begin++)
{
if (text[begin] != text[end])
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if (begin == middle)
printf("Palindrome.\n");
return 0;
}
Output:
1) Enter a string: mam
Palindrome

2) Enter a string: sir


Not a palindrome

Exercise 12
Write a C program that uses functions to perform the following operations:
A) To insert a sub-string in to given main string from a given position.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20], str2[20];
int l1, l2, n, i;
clrscr();
puts("Enter the string 1\n");
gets(str1);
l1 = strlen(str1);
puts("Enter the string 2\n");
gets(str2);
l2 = strlen(str2);
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
for(i = n; i < l1; i++)
{
str1[i + l2] = str1[i];
}
for(i = 0; i < l2; i++)
{
str1[n + i] = str2[i];
}
str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
getch();
}
Output:
Enter the string 1
sachin
Enter the string 2
tendulkar
Enter the position where the string is to be inserted
4
After inserting the string is sachtendulkarin

B) To delete n Characters from a given position in a given string.


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
int i, n, l, pos;
clrscr();
puts("Enter the string\n");
gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
for(i = pos + n; i < l; i++)
{
str[i - n] = str[i];
}
str[i - n] = '\0';
printf("The string is %s", str);
getch();
}
Output:
Enter the string
sachin
Enter the position where characters are to be deleted
2
Enter the number of characters to be deleted
2
The string is sain

C) To replace a character of string either from beginning or ending or at a specified location


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char string[100], orginal, replace;
clrscr();

printf("Enter the String : ");


gets(string);
printf("\nEnter the character to be replace from orginal string : ");
orginal = getchar();
getchar();
printf("\nEnter the new value : ");
replace = getchar();
for (i=0;i<strlen(string);i++)
{
if(string[i]==orginal)
string[i]=replace;
}
printf("\nThe new value of string is %s",string);
getch();
}
Output:

Exercise 13
A) Write a C program to implement Taylor series method
#include<stdio.h>
#include<conio.h>
#include<math.h>
long int factorial(int n);
int main()
{
 int x,i;
 float s,r;
 char c;
printf("You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!..x^x/x!");
 printf("To which term you want its sum of ?  ");
 scanf("%d",&x);
 s=0;
 for (i=1;i<=x;i++)
  {   s=s+((float)pow(x,i)/(float)factorial(i));
}
 printf("The sum of %d terms is %f",x,1+s);
 fflush(stdin);
      getch();
}
long int factorial(int n)
 {
  if (n<=1)
 return(1);
  else
 n=n*factorial(n-1);
 return(n);
 }
Output:
You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!..x^x/x!
To which term you want its sum of?  5                         
The sum of 5 terms is 91.416664 

B) Write a C program to implement Euler’s method


#include<stdio.h>
#include <math.h>
#include<conio.h>
//dy/dx = xy
#define F(x,y) (x)*(y)
void main()
{
double y1,y2,x1,a,n,h;
int j;
clrscr();
printf("\nEnter the value of range: ");
scanf("%lf %lf",&a,&n);
printf("\nEnter the value of y1: ");
scanf("%lf",&y1);
printf("\n\nEnter the h: ");
scanf("%lf",&h);
printf("\n\n y1 = %.3lf ",y1);
for(x1=a,j=2; x1<=n+h; x1=x1+h,j++)
{
y2= y1 + h * F(x1,y1);
printf("\n\n x = %.3lf => y%d = %.3lf ",x1,j,y2);
y1=y2;
}
getch();
}
Output:
Enter the value of range: 1  1.5                                                                                                 
Enter the value of y1: 5                                                                                                        
Enter the h: 0.1                                                                                                                
                                                                          
  y1 = 5.000                                                                                                                    
                                                                                                                                
  x = 1.000 => y2 = 5.500                                                                                                       
                                                                                                                                
  x = 1.100 => y3 = 6.105                                                                                                       
                                                                                                                                
  x = 1.200 => y4 = 6.838                                                                                                       
                                                                                                                                
  x = 1.300 => y5 = 7.726                                                                                                       
                                                                                                                                
  x = 1.400 => y6 = 8.808                                                                                                       
                                                                                                                                
  x = 1.500 => y7 = 10.129   

C) Write a C program to implement Runge kutta method


#include<stdio.h>
#include <math.h>
#include<conio.h>
//dy/dx = y - x
#define F(x,y) (y)-(x)
void main()
{
double y0,x0,y1,n,h,f,f1,k1,k2;
int j;
clrscr();
printf("\nEnter the value of x0: ");
scanf("%lf",&x0);
printf("\nEnter the value of y0: ");
scanf("%lf",&y0);
printf("\nEnter the value of h: ");
scanf("%lf",&h);
printf("\nEnter the value of last point: ");
scanf("%lf",&n);
for(; x0<n; x0=x0+h)
{
f=F(x0,y0);
k1 = h * f;
f1 = F(x0+h,y0+k1);
k2 = h * f1;
y1 = y0 + ( k1 + k2)/2;
printf("\n\n k1 = %.4lf ",k1);
printf("\n\n k2 = %.4lf ",k2);
printf("\n\n y(%.4lf) = %.3lf ",x0+h,y1);
y0=y1;
}
getch();
}
Output:
Enter the value of x0: 0                                                                                                        
                                                                                                                                
Enter the value of y0: 2                                                                                                        
                                                                                                                                
Enter the value of h: 0.05                                                                                                      
                                                                                                                                
Enter the value of last point: 0.1                                                                                              
                                                                                                                     
  k1 = 0.1000                                                                                                                   
                                                                                                                                
  k2 = 0.1025                                                                                                                   
                                                                                                                                
  y(0.0500) = 2.101                                                                                                             
                                                                                                                                
  k1 = 0.1026                                                                                                                   
                                                                                                                                
  k2 = 0.1052                                                                                                                   
                                                                                                                                
  y(0.1000) = 2.205 
Exercise 14
A) Draw the flow chart using Raptor and write a C program to implement a linear search.
Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all
items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise
the search continues till the end of the data collection.

Flowchart
#include <stdio.h>
#include<conio.h>
void main()
{
  int array[100], search, c, n;
clrscr( ); 
  printf("Enter number of elements in array\n");
  scanf("%d", &n);
  printf("Enter %d integer(s)\n", n);
  for (c = 0; c < n; c++)
  scanf("%d", &array[c]);
  printf("Enter a number to search\n");
  scanf("%d", &search);
  for (c = 0; c < n; c++)
  {
    if (array[c] == search)    /* If required element is found */
    {
      printf("%d is present at location %d.\n", search, c+1);
      break;
    }
  }
  if (c == n)
  printf("%d isn't present in the array.\n", search);
  getch( );
}
Output:
Enter number of elements in array                                                                                               
5                                                                                                                               
Enter 5 integer(s)                                                                                                              
15 77 49 13 1                                                                                                                   
Enter a number to search                                                                                                        
13                                                                                                                              
13 is present at location 4.                                                                                                    
                              
B) Draw the flow chart using Raptor and write a C program to implement binary search
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or
the interval is empty.

Flowchart
#include <stdio.h>
#include<conio.h> 
void main()
{
   int c, first, last, middle, n, search, array[100];
  Clrscr();
   printf("Enter number of elements\n");
   scanf("%d",&n);
 printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
  printf("Enter value to find\n");
  scanf("%d", &search);
  first = 0;
  last = n - 1;
  middle = (first+last)/2;
  while (first <= last) 
{
     if (array[middle] < search)
          first = middle + 1;    
  else if (array[middle] == search) 
{
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
  else
         last = middle - 1;
        middle = (first + last)/2;
   }
   if (first > last)
      printf("Not found! %d isn't present in the list.\n", search);
    getch();  
}
Output:
Enter number of elements                                                                                                        
10                                                                                                                              
Enter 10 integers                                                                                                               
1 4 6 7 9 10 15 17 19 20                                                                                                        
Enter value to find                                                                                                             
19                                                                                                                              
19 found at location 9. 

C) Write a C program to implement sorting of an array of elements


#include <stdio.h>
#include<conio.h>
void main()
{
  int i, j, a, n, number[30];
clrscr( );
printf("Enter the value of N \n");
scanf("%d", &n);
  printf("Enter the numbers \n");
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("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
  getch( );
}
Output:
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780

Exercise 15
A) Write C Program to reverse a string using pointers
#include <stdio.h>
#include<conio.h>
void main()
{
char str[50], *ptr;
int i, len;
clrscr( );
printf("Enter a string: ");
gets(str);
ptr = str;
for(i=0;i<50;i++){
if(*ptr == '\0') break;
ptr++;
}
len = i;
ptr--;
printf("Reversed String: ");
for(i=len; i>0; i--){
printf("%c",*ptr--);
}
getch( );
}
Output:
Enter a string: compiler
Reversed string: relipmoc

B) Write a C Program to compare two arrays using pointers


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],n,i,check=0;
int *p,*q;
clrscr();
printf("Enter the Array Size(common for A and B):");
scanf("%d",&n);
printf("\nEnter the Array Elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the Array Elements\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
p=&a[0];
q=&b[0];
for(i=0;i<n;i++)
if((*(p+i))!=(*(q+i)))
{
printf("Two Arrays are not equal\n");
check=1;
break;
}
if(check==0)
printf("Two Arrays are Equal\n");
getch();
}
Output: -
Enter the Array Size (common for A and B):5
Enter the Array Elements
1 2 3 4 5
Enter the Array Elements
1 2 3 4 6
Two Arrays are not equal

C) Write a C program to swap two numbers using pointers


#include<stdio.h>
#include<conio.h>
void change(int *, int *);
void main()
{
int x,y;
clrscr();
printf("enter values of x & y \n");
scanf("%d %d", &x,&y);
change(&x,&y);
printf("in main() x=%d, y=%d \n",x,y);
getch();
}
void change(int *a,int *b)
{
int *temp;
*temp=*a;
*a=*b;
*b=*temp;
printf("in change() x=%d,y=%d \n",*a,*b);
}
Output: -
Enter values of x & y
5 512
in change() x=512,y=5
in main() x=512, y=5

Exercise 16
Examples which explores the use of structures, union and other user defined variables
#include<stdio.h>
#include<conio.h>
struct xyz
{
char name[10];
int age;
float salary;
char des [10];
}x;
main()
{
clrscr();
printf("enter name,age,des and salary");
scanf("%s\n%d\n%s\n%f",x.name,&x.age,x.des,&x.salary);
printf("name of employee = %s\nage= %d\ndesignation=%s\nsalary= %f",x.name,x.age,x.des,x.salary);
}
Output:
Enter name, age, des and salary
ravi
23
Lecturer
10000
Name of employee = ravi
Age= 23
Designation=lecturer
Salary= 10000.000000

Union
#include<stdio.h>
#include<conio.h>
union xyz
{
char name[20];
int age;
float salary;
char des[10];
}x;
main()
{
clrscr();
printf("enter name");
scanf("%s",&x.name);
printf("%s",x.name);

printf("\nENTER age");
scanf("%d",&x.age);
printf("%d",x.age);

printf("\nenter designation");
scanf("%s",&x.des);
printf("%s",x.des);

printf("\nenter salary");
scanf("%d",&x.salary);
printf("%d",x.salary);
getch();
}
Output:
enter name,age,des and salary
ravi
23
Lecturer
10000
Name of employee = ravi
Age= 23
Designation=lecturer
Salary= 10000.000000

Exercise 17
A) Write a C program which copies one file to another.
#include <stdio.h>
#include <stdlib.h>
void main()
{
char ch, source_file[20], target_file[20];
FILE *source, *target;
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
if( source == NULL )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if( target == NULL )
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
getch();
}
Output:
Enter name of file to copy
copy.c
Enter name of target file
copy2.c
File copied successfully

B) Write a C program to count the number of characters and number of lines in a file.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
int l=1,w=0,chars=1;
char c;
clrscr();
printf(“\n********Terminate String by pressing the Tab Key followed by Enter Key ********”);
printf(“\n\nEnter the String :\n\n”);
c=getchar();
while(c!= ‘\t’)
{
c=getchar();
chars++;
if(c==’ ‘|| c==’\n’||c==’.’)
w++;
if(c==’\n’)
l++;
if(c==’\t’)
chars--;
}

printf(“\r\n-------------------------- “);
printf(“\r\nNumber of Lines =%d “,l);
printf(“\r\nNumber of Characters =%d “,chars);
printf(“\r\n-------------------------- “);
getch();
}
Output:
********Terminate String by pressing the Tab Key followed by Enter Key ********
Enter the String:
Welcome
TO the
World of c
Number of Lines =3
Number of Characters =25

C) Write a C Program to merge two files into a third file. The names of the files must be entered
using command line arguments
#include <stdio.h>
#include<conio.h>
int main(int argc , char *argv[])
{
FILE *src1, *src2, *target;
char ch;
if(argc != 4)
{
printf("\nToo few arguments. You must specify the filenames.");
return 1;
}
src1 = fopen(argv[1], "rb");
src2 = fopen(argv[2], "rb");
target = fopen(argv[3], "wb");
if(src1 == NULL || src2 == NULL || target == NULL)
{
printf("\nUnable to open \'%s\'",
src1 ? src2 ? argv[3] : argv[2] : argv[1]);
if(src1) fclose(src1);
if(src2) fclose(src2);
if(target) fclose(target);
return 1;
}
while(!feof(src1))
{
ch = fgetc(src1);
fputc(ch, target);
}
while(!feof(src2))
{
ch = fgetc(src2);
fputc(ch, target);
}
printf("\n\'%s\' and \'%s\' concatenated to \'%s\' successfully", argv[1], argv[2], argv[3]);
fclose(src1);
fclose(src2);
fclose(target);
return 0;
}
Output:
C:\TURBOC2>merge.exe hello.c n.c seshu.c

'hello.c' and 'n.c' concatenated to 'seshu.c' successfully


C:\TURBOC2

You might also like