You are on page 1of 48

Week-1

1 a) Write a c program to calculate the area of triangle using the formula


Area=(s(s-a)(s-b)(s-c))1/2 where s=(a+b+c)/2
Source code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,s,area;
printf("Enter a value");
scanf("%f",&a);
printf("Enter b value");
scanf("%f",&b);
printf("Enter c value");
scanf("%f",&c);
s=(a+b+c)/2;
printf("The s value is %f \n",s);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(" The area value is %f",area);
getch();
}
Output:
Enter a value4
Enter b value5
Enter c value6
The s value is 7.500000
The area value is 9.921567
1 b)Write a C program to find the largest of the three numbers using ternary operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf("Enter a value \n");
scanf("%d",&a);
printf("Enter b value\n");
scanf("%d",&b);
printf("Enter c value\n");
scanf("%d",&c);
max=a>b?(a>c?a:c):(b>c?b:c);
printf("The maximum value among three numbers %d",max);

getch();}
output:
Enter a value
4
Enter b value
5
Enter c value
6
The maximum value among three numbers 6
1 c)Write a C program to swap two numbers without using a temporary variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a value\n");
scanf("%d",&a);
printf("Enter b value\n");
scanf("%d",&b);
printf("\nValues of a and b before swap %d %d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("The a value after swap %d\n",a);
printf("The b value after swap %d",b);
getch();
}
Output:
Enter a value
4
Enter b value
5
Values of a and b before swap 4 5
The a value after swap 5
The b value after swap 4

Week-2
2a)/* 2s complement of a number is obtained by scanning it from right to left and
complementing all the bits after the first appearance of a 1. Thus 2s complement of 11100 is
00100.Write a C program to find the 2s complement of a binary number.*/
#include <stdio.h>
#include<conio.h>
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0'; i++)
{
if (a[i]!='0' && a[i]!='1')
{
printf("The number entered is not a binary number. Enter the correct number");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{

if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}
Output:
Enter the binary number101
The 2's complement is 011
b) Write a C program to find the roots of a quadratic equation.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,root1,root2,root3,desc,real,num,imag;
int k;
clrscr();
printf(" Enter the values of a,b,c");
scanf("%f%f%f",&a,&b,&c);
desc=(b*b)-(4*a*c);
if(desc<0)
k=1;

else if(desc==0)
k=2;
else
k=3;
switch(k)
{
case 1:
printf("\n roots are imaginary\n");
real=-b/(2*a);
desc=-desc;
num=pow((double)desc,(double)0.5);
imag=num/(2*a);
printf("\n root1=%f +j %f \n",real,imag);
printf("\n root2=%f -j %f \n",real,imag);
break;
case 2:
printf("\n roots are real and equal\n");
root1=-b/(2*a);
printf("\n root1 = root2 = %f\n",root1);
break;
case 3:
printf("\n roots are real and unequal\n");
root1=(-b+sqrt((double)desc))/(2*a);
root2=(-b-sqrt((double)desc))/(2*a);
printf("\n root1=%f root2=%f\n",root1,root2);
break;
default:
break;
}
getch();
}
OUTPUT1:
Enter the values of a,b,c 2 4 2
roots are real and equal
root1 = root2 = -1.000000
OUTPUT2:
Enter the values of a,b,c 2 6 4
roots are real and unequal

root1=-1.000000 root2=-2.000000
OUTPUT3:
Enter the values of a,b,c 4 2 4
roots are imaginary
root1=-0.250000 +j 0.968246
root2=-0.250000 -j 0.968246
C) Write a program ,which takes two integer operands and one operator from the user
,performs the operation and then prints the result.(Consider the operators +,-,*,/,% and use
Switch Statement);
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char ch,op;
clrscr();
printf("Enter a value\n");
scanf("%d",&a);
printf("Enter b value \n");
scanf("%d",&b);
printf("enter the any one of these operators(+,-,*,/,%) \n" );
scanf("\n%c",&op);
switch(op)
{
case '+':printf("Addition of two numbers %d \n",(a+b));
break;
case '-':printf("Subtraction of two numbers %d \n",(a-b));
break;
case '*':printf(" Multiplication of two numbers %d \n",(a*b));
break;
case '/':printf(" Division of two numbers %d \n",(a/b));
break;
case '%':printf(" Modulo operation of two numbers %d",(a%b));
break;
default:printf("please enter proper option");
}
getch();
}

Output1:
Enter a value
4
Enter b value
6
enter the any one of these operators(+,-,*,/,%)
+
Addition of two numbers 10
Output2:
Enter a value
4
Enter b value
5
enter the any one of these operators(+,-,*,/,%)
*
Multiplication of two numbers 20
Week3:
3 a)Write a C program to find the sum of individual digits of a positive integer and find
the reverse of the given number ex:456 sum=15 reverse=654
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,temp1,temp2,sum=0,rev=0;
clrscr();
printf("Enter the n value ");
scanf("%d",&n);
temp1=n;
temp2=n;
while(temp1>0)
{
r=temp1%10;
sum=sum+r;
temp1=temp1/10;
}
printf(" The sum value of individual digits of a number %d\n ",sum);
printf("The reverse of the number is");
while(temp2>0)
{
r=temp2%10;
//printf("%d",r);
rev=rev*10+r;
temp2=temp2/10;
}

printf(the reverse of a no is:%d,rev);


getch();
}
Output:
Enter the n value 123
The sum value of individual digits of a number 6
The reverse of the number isthe reverse of a no is:321
3b) Write a C program to generate the first n terms of the sequenceUse the summing
series method to compute the value of SIN(x),cos(x)
#include<stdio.h>
#include<conio.h>
void main()
{
f loat base,pwr,sum,c=1,m=2,i=3,g,h;
clrscr();
printf("enter the base value");
scanf("%f",&base);
printf("enter the power value");
scanf("%f",&pwr);
sum=base;
ab:
m=m*i;
i=i+2;
c++;
m=m*(i-1);
if(i<=pwr)
goto ab;
printf("sum=%f",sum);
getch();
}
Output:
enter the base value4
enter the power value2
sum=4.000000
----------------------------cos(x)--------------------include<stdio.h>
#include<math.h>
void main()
{
int n,x1,i,j;
float x,sign,cosx,fact;

printf("Enter the number of the terms in aseries\n");


scanf("%d",&n);
printf("Enter the value of x(in degrees)\n");
scanf("%f",&x);
x1=x;
x=x*(3.142/180.0);
cosx=1;
sign=-1;
for(i=2;i<=n;i=i+2)
{
fact=1;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
cosx=cosx+(pow(x,i)/fact)*sign;
sign=sign*(-1);
}
printf("Sum of the cosine series
= %7.2f\n",cosx);
printf("The value of cos(%d) using library function = %f\n",x1,cos(x));
}

/*End of main() */

/*-------------------------------------------------------Output
Enter the number of the terms in aseries
5
Enter the value of x(in degrees)
60
Sum of the cosine series
= 0.50
The value of cos(60) using library function = 0.499882
--------

3c)Write a c program to generate all prime numbers between 1 and n, where n is a value
supplied by user
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,j,i,count=0;
clrscr();
printf("Enter n value");
scanf("%d",&n);
printf(" prime numbers %d %d" ,1,2);
for(i=3;i<=n;i++)
{
if((i%2)!=0)
{
for(j=1;j<=i;j++)
{
if((i%j)==0)
{
count++;
}
}
if(count==2)
{
printf("%d\t",i);
}
count=0;
}
}
getch();
}
Output:
Enter n value10
prime numbers 1 23

WEEK-4
4a)Write a C program to print the multiplication table of a given number n up to given
value when n is entered by user.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i;
clrscr();
printf("Enter multiplication table value");
scanf("%d",&n);
printf("Enter upto which table to be display");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
printf("%d%c%d%c%d\n",i,'*',n,'=',(n*i));
}
getch();
}
Output:
Enter multiplication table value4
Enter upto which table to be display10
1*4=4
2*4=8
3*4=12
4*4=16
5*4=20
6*4=24
7*4=28
8*4=32
9*4=36
10*4=40
4 b) Write a C program to decimal -binary
#include<stdio.h>
#include<conio.h>
void main()
{
int dec,que;
int bin[100] ;
int i=1,j;
clrscr();
printf("Enter the deciaml value ");
scanf("%d",&dec);
que=dec;
while(que!=0)
{
bin[i++]=que%2;

que=que/2;
}
Printf(the binaryvalue is);
for(j=i-1;j>0;j--)
{
printf(" %d",bin[j]);
}
getch();
}
Output:
Enter the deciaml value 5
the binary value101
4 c) Write a C program to check the given number is Armstrong or not Ex:153
#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,r,sum=0;
clrscr();
printf("Enter number\n ");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=(n%10);
sum=sum+(r*r*r);
n=n/10;
}
printf("sum value %d",sum);
if(temp==sum)
{
printf("The number is Amstrong ");
}
else
{
printf("The number is not Amstrong number");
}
getch(); }}
output1:
Enter number
153
sum value 153The number is Amstrong

output2:
Enter number
333
sum value 81The number is not Amstrong number
Week-5
5a) Write a C program to find min max of an array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,min,max,n;
clrscr();
printf("enter array size");
scanf("%d",&n);
printf("Enter elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The elements are");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
min=a[0];
for(i=0;i<n;i++)
{
if(min>a[i])
{
min=a[i];
}
}
printf("\n The minimum element is %d",min);
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
printf("\n The maximum element is%d",max);
getch();
}

Output:
enter array size5
Enter elements1
2
3
4
5
The elements are12345
The minimum element is 1
The maximum element is5
5B) Write a C program to Linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[1],n,i,x,count=0;
clrscr();
printf("enter size of the array");
scanf("%d",&n);
printf(" enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("array elements are");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("enter element to search");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(x==a[i])
{
printf("element is found");
count++;
}
}
if(count==0)
{
printf("element not found");

}
getch();
}
Output1:
enter size of the array5
enter array elements4
2
6
7
3
array elements are42673enter element to search3
element is found
output2:
enter size of the array5
enter array elements2
5
1
6
3
array elements are25163enter element to search4
element not found
5c) Write a C program to binary search
#include<stdio.h>
void main(){
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;

break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
getch();
}
Output1:
Enter the size of an array:
4
Enter the elements in ascending order: 1
2
3
4
Enter the number to be search: 5
The number is not found.
Output2:
Enter the size of an array: 5
Enter the elements in ascending order: 1
2
3
4
5
Enter the number to be search: 3
The number is found.
Week6

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


#include <stdio.h>
void main()
{

int array[100], n, c, d, swap;


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]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
for(c=0;c<n;c++)
printf(%d,array[c]);
getch();
}
Output:
Enter number of elements5
Enter 5 integers3 1 5 2 6
12356

6b)Write a C program that uses functions to perform the following:


i) Addition of Two Matrices
ii) Multiplication of Two Matrices

#include<stdio.h>
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();
printf("************************************");
printf("\n\t\tMENU");
printf("\n**********************************");
printf("\n[1]ADDITION OF TWO MATRICES");
printf("\n[2]MULTIPLICATION OF TWO MATRICES");
printf("\n[0]EXIT");
printf("\n**********************************");
printf("\n\tEnter your choice:\n");
scanf("%d",&ch);
if(ch<=2 & ch>0)
{
printf("Valid Choice\n");
}
switch(ch)
{
case 1:
printf("Input number of rows and columns of A & B Matrix:");
scanf("%d%d",&r1,&c1);
printf("Enter elements of matrix A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter elements of matrix B:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&b[i][j]);
}
printf("\n =====Matrix Addition=====\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",a[i][j]+b[i][j]);
printf("\n");

}
break;
case 2:
printf("Input rows and columns of A matrix:");
scanf("%d%d",&m,&n);
printf("Input rows and columns of B matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("matrices can be multiplied\n");
printf("resultant matrix is %d*%d\n",m,q);
printf("Input A matrix\n");
read_matrix(a,m,n);
printf("Input B matrix\n");
/*Function call to read the matrix*/
read_matrix(b,p,q);
/*Function for Multiplication of two matrices*/
printf("\n =====Matrix Multiplication=====\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("Resultant of two matrices:\n");
write_matrix(c,m,q);
}
/*end if*/
else
{
printf("Matrices cannot be multiplied.");
}
/*end else*/
break;
case 0:
printf("\n Choice Terminated");
exit();
break;
default:
printf("\n Invalid Choice");
}

getch();
}
/*Function read matrix*/
int read_matrix(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
return 0;
}
/*Function to write the matrix*/
int write_matrix(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
return 0;
}
Output1:
**********************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
**********************************
Enter your choice:
1
Valid Choice
Input number of rows and columns of A & B Matrix:2
2
Enter elements of matrix A:
1
2
3
4
Enter elements of matrix B:
1
2
3
4

=====Matrix Addition=====
2 4
6 8
Output2:
Enter your choice:
2
Valid Choice
Input rows and columns of A matrix:2
2
Input rows and columns of B matrix:2
2
matrices can be multiplied
resultant matrix is 2*2
Input A matrix
1
2
3
4
Input B matrix
1
2
3
4
=====Matrix Multiplication=====
Resultant of two matrices:
7 10
15 22
Week7:
a) Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to given main string from a given position.
ii) To delete n Characters from a given position in a given string.
------------------ insert a sub-string -------------------------.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;

int t=0;
int x,g,s,n,o;
clrscr();
puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;
// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;
// Adding the sub-string
for(i=p;i<s;i++)
{
x = c[i];
if(t<n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
getch();
}
Output:
Enter First String:
sony
Enter Second String:
krishna
Enter the position where the item has to be inserted: 3

Sonkrishnay
---------------- To delete n Characters ----------------#include <stdio.h>
#include <conio.h>
#include <string.h>
void delchar(char *x,int a, int b);
void main()
{
char string[10];
int n,pos,p;
clrscr();
puts("Enter the string");
gets(string);
printf("Enter the position from where to delete");
scanf("%d",&pos);
printf("Enter the number of characters to be deleted");
scanf("%d",&n);
delchar(string, n,pos);
getch();
}
// Function to delete n characters
void delchar(char *x,int a, int b)
{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
puts(x);
}
}
Output:
Enter the string
sonykrishna
Enter the position from where to delete3
Enter the number of characters to be deleted5
Soshna
iii. To replace a character of string either from beginning or ending or at a specified
location
#include <stdio.h>
#include <conio.h>

#include<string.h>
void main()
{
char str[10];
int i,m,n,pos;
char ch;
clrscr();
printf("enter string");
gets(str);
printf("enter cahracter");
scanf("%c",ch);
printf("enter posi");
scanf("%d",pos);
m=strlen(str);
for(i=0;i<=m;i++)
{
if(i==pos)
{
str[pos]=ch;
}
puts(str);
}
puts(str);
getch();
}
output:
enter stringsonykrishna
enter cahracterp
enter posi1
spnykrishna
Week8:
Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.)
-------------------OPERATIONS ON CIOMPLEX NUMBERS-----------------#include<stdio.h>
#include<math.h>

void arithmetic(int opern);


struct comp
{
double realpart;
double imgpart;
};
void main()
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your
Option [ ]\b\b");
scanf("%d",&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main();
}
}
void arithmetic(int opern)
{
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:");
scanf("%lf",&w2.imgpart);

switch(opern)
{
/*addition of complex number*/
case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;
/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch();
main();
}
Output1:
***** MAIN MENU *****
Select your option:
1 : ADD
2 : MULTIPLY
0 : EXIT
Enter your Option [ 1]
Enter two Complex Numbers (x+iy):
Real Part of First Number:3
Imaginary Part of First Number:2
Real Part of Second Number:3
Imaginary Part of Second Number:2
Answer = 6.000000+4.000000i

Output2:
***** MAIN MENU *****
Select your option:
1 : ADD
2 : MULTIPLY
0 : EXIT
Enter your Option [ 2]
Enter two Complex Numbers (x+iy):
Real Part of First Number:2
Imaginary Part of First Number:3
Real Part of Second Number:2
Imaginary Part of Second Number:3
Answer = -5.000000+12.000000i
Week9:
/** Program to Compare Two Strings Without using strcmp() **/
#include<stdio.h>
main()
{
char string1[5],string2[5];
int i,temp = 0;
printf("Enter the string1 value:\n");
gets(string1);
printf("\nEnter the String2 value:\n");
gets(string2);

for(i=0; string1[i]!='\0'; i++)


{

if(string1[i] == string2[i])
temp = 1;
else
temp = 0;
}
if(temp == 1)
printf("Both strings are same.");
else
printf("Both string not same.");
getch();
}
Output1:
Enter the string1 value:
sony
Enter the String2 value:
krishna
Both string not same.
Output2:
Enter the string1 value:
sony
Enter the String2 value:
sony
Both strings are same.
/* Program to Concatenate Two Strings without using strcat() */
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char string1[30], string2[20];
int i, length=0, temp;
printf("Enter the Value of String1: \n");
gets(string1);
printf("\nEnter the Value of String2: \n");
gets(string2);

for(i=0; string1[i]!='\0'; i++)


length++;
temp = length;
for(i=0; string2[i]!='\0'; i++)
{
string1[temp] = string2[i];
temp++;
}
string1[temp] = '\0';
printf("\nThe concatenated string is:\n");
puts(string1);
getch();
}
Output:
Enter the string1 value:
luck
Enter the String2 value:
krishna
The concatenated string is:
luckkrishna
/* Program to append Stringat aposition */
#include<stdio.h>
#include<conio.h>
void main()
{
char source[20],destination[20],i,j,m,n;
int position;
clrscr();
printf("enter the sorce string");
gets(sorce);
printf("the sorce string is");
puts(source);
printf("enter the destination string");
gets(detsination);
printf("the destination string is");
puts(destination);
printf("enter the position");
scanf("%d",&position);

m=strlen(destination);
n=strlen(source);
for(i=0;i<=m;i++)
{
if(i==position)
{
for(j=0;j<=n;j++,position++)
{
detsination[position]=source[j];
}
}
}
puts(detsination);
getch();
}
Week10:
/*** Program to Find Length of a String Without using strlen() ***/
#include <stdio.h>
main()
{
char str[20];
int i = 0;
printf("\nEnter any string: ");
gets(str);
while (str[i] != '\0')
i++;
printf("\nLength of string: %d", i);
getch();
}
Output:
Enter any string: luck
Length of string: 4
/* Program to Find Whether a String is Palindrome or Not without using String
Functions */

#include <stdio.h>
#include <string.h>
main()
{
char s1[20];
int i, j, len=0, flag=0;
printf("\nEnter any string: ");
gets(s1);
for (i=0; s1[i]!='\0'; i++)
len++;
i = 0;
j = len-1;
while (i < len)
{
if (s1[i] != s1[j])
{
flag = 1;
break;
}
i++;
j--;
}
if (flag == 0)
printf("\nString is palindrome");
else
printf("\nString is not palindrome");
getch(); }
output1:
Enter any string: lucky
String is not palindrome
Output2:
Enter any string: liril
String is palindrome
Week11:
a) Write a C functions to find both the largest and smallest number of an array of integers.
#include<stdio.h>
#include<conio.h>

void large(int);
void main()
{
int limit;
clrscr();
printf("Enter the limit");
scanf("%d",&limit);
large(limit);
getch();
}
//function begin..
void large(int limit)
{
int a[50],i,temp,j;
printf("Enter the numbers");
for(i=0;i<limit;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<limit;i++)
{
for(j=i+1;j<limit;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("big=%d\nsecond big=%d\nsmall=%d\nsecond small=%d\n",a[limit-1],a[limit-2],a[0],a[1]);
}
Output:
Enter the limit5
Enter the numbers2
3

1
6
7
big=7
second big=6
small=1
second small=2
b) Write C programs illustrating call by value and call by reference cncepts.
#include<stdio.h>
#include<conio.h>
int swap(int , int);

// Declaration of function

void main( )
{
int a = 10, b = 20 ;

// call by value

printf ( "\n before swap a = %d b = %d", a, b ) ;


swap(a,b);

// a and b are actual

parameters
printf ( "\n after swapa = %d b = %d", a, b ) ;
getch();
}
int swap( int x, int y )
parameters
{
int t ;
t=x;
x=y;

// x and y are formal

y=t;
printf ( "\nx = %d y = %d", x, y ) ;
}
Output:
before swap a = 10 b = 20
x = 20 y = 10
after swapa = 10 b = 20
-------------------call by reference-----#include<stdio.h>
#include<conio.h>
void swap( int *x, int *y )

// call by refrence

{
int t ;
t = *x ;
*x = *y ;
*y = t ;
printf( "\nx = %d y = %d", *x,*y);
}
Void main( )
{
int a = 10, b = 20 ;
printf ( "\n before swap a = %d b = %d", a, b ) ;
swap ( &a, &b ) ;

// passing the address of values

to be swapped
printf ( "\n after swap a = %d b = %d", a, b ) ;
getch();
}
output:

before swap a = 10 b = 20
x = 20 y = 10
after swap a = 20 b = 10

Week12:
/** Program to Find Factorial of a Number using Recursion **/
#include <stdio.h>
long fact(int);
main()
{
int n;
long f;
printf("\nEnter number to find factorial: ");
scanf("%d", &n);
f = fact(n);
printf("\nFactorial: %ld", f);
getch();
}
long fact(int n)
{
int m;
if (n == 1)
return n;
else
{
m = n * fact(n-1);
return m;
}
}
Output:
Enter number to find factorial: 5
Factorial: 120

/* Write C programs that use both recursive and non-recursive functions To find the GCD
(greatest common divisor) of two given integers */
#include<stdio.h>
#include<math.h>
unsigned int GcdRecursive(unsigned m, unsigned n);
unsigned int GcdNonRecursive(unsigned p,unsigned q);
int main(void)
{
int a,b;
printf("Enter the two numbers whose GCD is to be found: ");
scanf("%d%d",&a,&b);
printf("GCD of %d and %d Using Recursive Function is %d\n",a,b,GcdRecursive(a,b));
printf("GCD of %d and %d Using Non-Recursive Function is %d\n",a,b,GcdNonRecursive(a,b));
return 0;
}
/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
if(n>m)
return GcdRecursive(n,m);
if(n==0)
return m;
else
return GcdRecursive(n,m%n);
}
/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{

unsigned remainder;
remainder = p-(p/q*q);
if(remainder==0)
return q;
else
GcdRecursive(q,remainder);
}
Output:
Enter the two numbers whose GCD is to be found: 3
4
GCD of 3 and 4 Using Recursive Function is 1
GCD of 3 and 4 Using Non-Recursive Function is 1
/* Program to Find Factorial of a Number without using Recursion */
#include <stdio.h>
main()
{
int n, i;
long fact=1;
printf("\nEnter any number: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
fact = fact*i;
printf("\nFactorial = %ld", fact);
getch();
}
Output:
Enter number to find factorial: 5
Factorial: 120

/** Program to Print Fibonacci Series without Recursion **/


#include <stdio.h>
main()
{
int x, y, z, n, i;
x=0; y=1;
printf("\nEnter value of n: ");
scanf("%d", &n);
printf("\nFibonacci Series:\n\n");
printf("\n%d", x);
printf("\t%d", y);
for( i=0; i<n-2; i++)
{
z = x+y;
printf("\t%d", z);
x = y;
y = z;
}
getch(); }
output:
Enter value of n: 5
Fibonacci Series:
0

/*** Program to Print Fibonacci Series using Recursion ***/


#include <stdio.h>
int fibbo(int, int, int, int);
main()
{
int n, f, x=0, y=1, i=3;
printf("\nEnter value of n: ");
scanf("%d", &n);
printf("\n%d\t%d", x, y);
fibbo(x, y, n, i);
getch();
}
fibbo(int x, int y, int n, int i)
{
int z;
if (i <= n)

{
z = x + y;
printf("\t%d", z);
x = y;
y = z;
i++;
fibbo(x,y,n,i);
}
}
output:
Enter value of n: 5
Fibonacci Series:
0

3
Week13:

a)

Write C Program to comapre a string using pointers


#include <stdio.h>
void main()
{
int compare_function(char*,char*);
int result;
char inputA[20],inputB[20];
printf(\nEnter string A : );
gets(inputA);
printf(\nEnter string B : );
gets(inputB);
result = compare_function(inputA,inputB);
if (result == 0)
printf(\nStrings are same);
else
printf(\nStrings are different);
}
int compare_function(char *a,char *b)
{
while (*a != \0)
{
if (*a == *b)

{
a++;
b++;
}
else
return 1;
}
return 0;
}
Output:
Enter string A : sony
Enter string B : sony
Strings are same
Enter string A : krish
Enter string B : sony

Strings are different


a) Write C Program to reverse a string using pointers
#include <stdio.h>
int main()
{
char str[1000], *ptr;
int i, len;
printf("Enter a string:
");
gets(str);

ptr = str;
for(i=0;i<1000;i++){
if(*ptr == '\0')
break;
ptr++;
}
len = i;
ptr--;
printf("Reversed String:
");
for(i=len; i>0; i--){
printf("%c",*ptr--);
}
return 0;
}
Output:
Enter a string: sony
Reversed String: ynos

Week14:
a) Write C Program to swap numbers using pointers
#include<stdio.h>
#include<conio.h>
void swap( int *x, int *y )
{
int t ;
t = *x ;
*x = *y ;

// call by refrence

*y = t ;
printf( "\nx = %d y = %d", *x,*y);
}
Void main( )
{
int a = 10, b = 20 ;
printf ( "\n before swap a = %d b = %d", a, b ) ;
swap ( &a, &b ) ;

// passing the address of values

to be swapped
printf ( "\n after swap a = %d b = %d", a, b ) ;
getch();
}
output:
before swap a = 10 b = 20
x = 20 y = 10
after swap a = 20 b = 10

Week15:
Write a C program on-enum#include<stdio.h>
#include<conio.h>
void main()
{
enum weeks{sun=1,mon,tue,wed,thur,fri,sat};
enum weeks day;
int a;
clrscr();
printf("Enter any weekday number i.e; from 1 to 7:");
scanf("%d",&a);
day=a;
if(day==1||day==7)
printf("Week ends");

else
printf("Not week ends");
printf("\n Day number is %d",day);
getch();
}
Output:
Enter any weekday number i.e; from 1 to 7:5
Not week ends
Day number is 5
Write a C program union
#include<stdio.h>
#include<conio.h>
union student
{
int sno;
char sname[16];
};
void main()
{
union student s;
clrscr();
printf("\nEnter student no:");
scanf("%d",&s.sno);
printf("\nStudent Number is:%d",s.sno);
printf("\nEnter student name:");
scanf("%s",s.sname);
//printf("\nStudent Number is:%d",s.sno);
printf("\nStudent Name is:%s",s.sname);
printf("\nSize of student:%d",sizeof(s));
getch();
}
Output:
Enter student no:4
Student Number is:4
Enter student name:luck
Student Name is:luck
Size of student:16
Write a C program structure
#include<stdio.h>
#include<conio.h>
struct student
{

int sno;
char sna[15];
char add[25];
}*s;
void main()
{
clrscr();
printf("Enter student no,name and address:");
scanf("%d%s%s",&s->sno,s->sna,s->add);
printf("\nStudent no,name and address are:");
printf("%d\t%s\t%s",(*s).sno,(*s).sna,(*s).add);
getch();
}
Output:
Enter student no,name and address:4
luck
benz
Student no,name and address are:4

luck

benz

Week16:
/* Program which copies one file to another */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
char ch,fname1[30],fname2[30];
clrscr();
printf("\n\n Enter Source File Name");
scanf("%s",fname1);
printf("\n\n Enter Destination File Name");
scanf("%s",fname2);
fp1=fopen(fname1,"r");
fp2=fopen(fname2,"w");
if(fp1==NULL)
{
printf(" Cannot open the file %s for reading",fname1);
getch();
exit();
}
else
{

ch=getc(fp1);
while(ch!=EOF)
{
putc(ch,fp2);
ch=getc(fp1);
}
printf(" \n\n Files Successfully copied");
}
fclose(fp1);
fclose(fp2);
printf("\n\n\n Reading Data from copied file %s \n\n",fname2);
fp1=fopen(fname2,"r");
while(!feof(fp1))
printf("%c",getc(fp1));
fclose(fp1);
getch();
}
Output:
Enter Source File Name2a.c
Enter Destination File Name3a.c
Files Successfully copied
Reading Data from copied file 3a.c
#include<stdio.h>
#include<conio.h>
struct student
{
int sno;
char sna[15];
char add[25];
}*s;
void main()
{
clrscr();
printf("Enter student no,name and address:");
scanf("%d%s%s",&s->sno,s->sna,s->add);
printf("\nStudent no,name and address are:");
printf("%d\t%s\t%s",(*s).sno,(*s).sna,(*s).add);
getch();
}

b) Write a C program to count the number of characters and number of lines in a file.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
FILE *f;
char ch;
int line=0,word=0,i=0;
clrscr();
f=fopen("student","w");
printf("Enter text
press
ctrol+z to quit\n");
do
{
ch=getchar();
putc(ch,f);
}
while(ch!=EOF);
fclose(f);
f=fopen("student","r");
while((ch=getc(f))!=EOF)
{i++;
if(ch=='\n')
line++;
if(isspace(ch)||ch=='\t'||ch=='\n')
word++;
putchar(ch);
}
fclose(f);
printf(\n no of charactyers=:%d,i-1);
printf("\n no of line=%d\n",line);
printf("no of word=%d\n",word);
getch();
}
Output:
Enter text press ctrol+z to quit
hii aliet
^Z
hii aliet
charcarcter9
no of line=1
no of word=2

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>
void main(int argc,char *argv[]){
FILE *fmrg,*fd1,*fd2;
int data1[]={1,3,9,15,18,20,30};
int data2[]={2,3,5,8,9,23,28};
int i,data,tmp1,tmp2;
clrscr();
fd1 = fopen("data1.txt","w");
for(i=0;i<7;i++)s
putw(data1[i],fd1);
fclose(fd1);
fd2 = fopen("data2.txt","w");
for(i=0;i<7;i++)
putw(data2[i],fd2);
fclose(fd2);
fmrg = fopen(argv[1],"w");
fd1 = fopen("data1.txt","r");
fd2 = fopen("data2.txt","r");
while(!feof(fd1)){
data = getw(fd1);
putw(data,fmrg);
}
while(!feof(fd2)){
data = getw(fd2);
putw(data,fmrg);
}
fclose(fd1);
fclose(fd2);
fclose(fmrg);
printf("\n\n*** Merging Files sucessfully***\n\n");
getch();
}
Output:
Hii.txt
Merging Files sucessfully

You might also like