Program No. 16: WAP to accept 'n' integers from user into an array and display the count of even and odd numbers of these?
Ans:
#include
#include
void main()
{
int n,i,a[100],even=0;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++)
{
if (a[i]%2==0)
even++;
}
printf("The count of even numbers is %d and that of odd numbers is %d",even,(n-even));
getch();
}
Program No. 17: Write a program to find an element in an array and display the index of the element using a function. OR Write a program to implement sequential search algorithm.
#include
#include
void main()
{
int n,i,a[100],x,index;
int search (int a[], int n, int x);
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&x);
index=search(a,n,x);
if(index==n)
printf("Not Found");
else
printf("The element %d is found in the index %d",x,index);
getch();
}
int search (int a[], int n, int x)
{
int i;
for(i=0;i<=n-1;i++)
{
if(x==a[i])
break;
}
return i;
}
Program 18:Write a program to sort numbers in ascending order. R Write a program to implement bubble sorting algorithm for sorting numbers in ascending order.
#include
#include
void main()
{
int n,i,a[100],x,index;
void ascend (int a[], int n);
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
ascend(a,n);
getch();
}
void ascend (int a[], int n)
{
int i,j,temp;
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}
Program 19:Write a program to add two matrices of size m x n.
#include
#include
void main()
{
int m,n,i,j,a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter the number of rows and columns:");
scanf("%d %d",&m,&n);
printf("Enter the elements of Matrix 1\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of Matrix 2\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("The sum of two matrices is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Program 20:WAP to perform following string operation without using string function:
a) Copying a string and display it.
b) concatenate two string and display it.
c)reverse a string
d) check whether given string is palindrome or not.
a) #include
#include
void main()
{
char str1[10],str2[10];
int i,j;
printf("\nEnter the string");
gets(str1);
for(i=0;str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("\n copied String=%s",str2);
getch();
}
b)
#include
void main()
{
char str1[20],str2[10];
int i,l,j=0;
printf("\nEnter First string");
gets(str1);
printf("\nEnter Second string");
gets(str2);
l=strlen(str1);
for(i=l;str2[j]!='\0';j++,i++)
{
str1[i]=str2[j];
}
str1[i]='\0';
printf("\nString After Concatenation=%s",str1);
getch();
}
c)
#include
#include
void main()
{
int n=0,i;
char a[100],temp;
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<=(n-1)/2;i++)
{
temp=a[n-i-1];
a[n-i-1]=a[i];
a[i]=temp;
}
printf("The reverse of this string is: %s",a);
getch();
}
d)
#include
#include
void main()
{
int n=0,i;
char a[100],rev[100];
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<=(n-1);i++)
{
rev[n-i-1]=a[i];
}
for(i=0;i<=n-1;i++)
{
if(a[i]!=rev[i])
break;
}
if(i==n)
printf("The string is palindrome.");
else
printf("The string is not palindrome.");
getch();
}
Program 21:
Write a program to find the sum of each row andcolumn elements of a 2 dimensional MxN array
#include
#include
void main()
{
int m,n,i,j,a[10][10],sum=0;
clrscr();
printf("Enter the number of rows / columns:");
scanf("%d",&m);
n=m;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
sum+=a[i][j];
}
printf("\nsum of row%d=%d",i+1,sum);
}
for(i=0;i<=m-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
sum+=a[j][i];
}
printf("\nsum of col%d=%d",i+1,sum);
}
getch();
}
Program 22:Write a program to swap two numbers using a function. Pss the values to be swapped to this function using call-by-value and call-by-reference method.
call by value
#include
#include
void main()
{
int a,b;
void swap (int a, int b);
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
printf("The values of a and b in the main function before calling the swap function are %d and %d\n",a,b);
swap(a,b);
printf("The values of a and b in main function after calling the swap function are %d and %d\n",a,b);
getch();
}
void swap (int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("The values of a and b in the swap function after swapping are %d and %d\n",a,b);
}
call-by-reference
#include
#include
void main()
{
int a,b;
void swap (int *p1, int *p2);
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
printf("The values of a and b in the main function before calling the swap function are %d and %d\n",a,b);
swap(&a,&b);
printf("The values of a and b in main function after calling the swap function are %d and %d\n",a,b);
getch();
}
void swap (int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
printf("The values of a and b in the swap function after swapping are %d and %d\n",*p1,*p2);
}
program 23: Define structure within structure consisting of following elements:
i. Employee Code
ii. Employee Name
iii. Employee Salary and
iv. Employee Date_of_joining
Write a C program to read at least 10 records and display them.
struct emp
{
int empcode,esal;
char ename[10],doj[15];
};
main()
{
struct emp e[10];
int n,i;
clrscr();
printf("\n enter the records(empcode,name,doj,sal):");
for(i=0;i<10 br="" i="">scanf("%d%s%s%d",&e[i].empcode,e[i].ename,doj,&e[i].esal);
printf("\n entered records are");
for(i=0;i<10 br="" i="">printf("\n %d\n %s \n %s\n %d",e[i].empcode,e[i].ename,e[i].esal);
getch();
}10>10>
program 24: Write a program to accept a set of 10 numbers and print the numbers using pointers. Find average of these integers.
#include
#include
void main()
{
int i,a[]={10,20,30,4,5,6,7,8,9,20},sum=0;
float avg;
clrscr();
printf("\nElement Using Pointer\n");
for(i=0;i<=9;i++)
{
printf("%d\t",*(a+i));
sum+=*(a+i);
}
avg=sum/2.0;
printf("\nAvg=%f",avg);
getch();
}
Ans:
#include
#include
void main()
{
int n,i,a[100],even=0;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++)
{
if (a[i]%2==0)
even++;
}
printf("The count of even numbers is %d and that of odd numbers is %d",even,(n-even));
getch();
}
Program No. 17: Write a program to find an element in an array and display the index of the element using a function. OR Write a program to implement sequential search algorithm.
#include
#include
void main()
{
int n,i,a[100],x,index;
int search (int a[], int n, int x);
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&x);
index=search(a,n,x);
if(index==n)
printf("Not Found");
else
printf("The element %d is found in the index %d",x,index);
getch();
}
int search (int a[], int n, int x)
{
int i;
for(i=0;i<=n-1;i++)
{
if(x==a[i])
break;
}
return i;
}
Program 18:Write a program to sort numbers in ascending order. R Write a program to implement bubble sorting algorithm for sorting numbers in ascending order.
#include
#include
void main()
{
int n,i,a[100],x,index;
void ascend (int a[], int n);
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
ascend(a,n);
getch();
}
void ascend (int a[], int n)
{
int i,j,temp;
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}
Program 19:Write a program to add two matrices of size m x n.
#include
#include
void main()
{
int m,n,i,j,a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter the number of rows and columns:");
scanf("%d %d",&m,&n);
printf("Enter the elements of Matrix 1\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of Matrix 2\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("The sum of two matrices is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Program 20:WAP to perform following string operation without using string function:
a) Copying a string and display it.
b) concatenate two string and display it.
c)reverse a string
d) check whether given string is palindrome or not.
a) #include
#include
void main()
{
char str1[10],str2[10];
int i,j;
printf("\nEnter the string");
gets(str1);
for(i=0;str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("\n copied String=%s",str2);
getch();
}
b)
#include
void main()
{
char str1[20],str2[10];
int i,l,j=0;
printf("\nEnter First string");
gets(str1);
printf("\nEnter Second string");
gets(str2);
l=strlen(str1);
for(i=l;str2[j]!='\0';j++,i++)
{
str1[i]=str2[j];
}
str1[i]='\0';
printf("\nString After Concatenation=%s",str1);
getch();
}
c)
#include
#include
void main()
{
int n=0,i;
char a[100],temp;
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<=(n-1)/2;i++)
{
temp=a[n-i-1];
a[n-i-1]=a[i];
a[i]=temp;
}
printf("The reverse of this string is: %s",a);
getch();
}
d)
#include
#include
void main()
{
int n=0,i;
char a[100],rev[100];
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<=(n-1);i++)
{
rev[n-i-1]=a[i];
}
for(i=0;i<=n-1;i++)
{
if(a[i]!=rev[i])
break;
}
if(i==n)
printf("The string is palindrome.");
else
printf("The string is not palindrome.");
getch();
}
Program 21:
Write a program to find the sum of each row andcolumn elements of a 2 dimensional MxN array
#include
#include
void main()
{
int m,n,i,j,a[10][10],sum=0;
clrscr();
printf("Enter the number of rows / columns:");
scanf("%d",&m);
n=m;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("Enter a value:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
sum+=a[i][j];
}
printf("\nsum of row%d=%d",i+1,sum);
}
for(i=0;i<=m-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
sum+=a[j][i];
}
printf("\nsum of col%d=%d",i+1,sum);
}
getch();
}
Program 22:Write a program to swap two numbers using a function. Pss the values to be swapped to this function using call-by-value and call-by-reference method.
call by value
#include
#include
void main()
{
int a,b;
void swap (int a, int b);
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
printf("The values of a and b in the main function before calling the swap function are %d and %d\n",a,b);
swap(a,b);
printf("The values of a and b in main function after calling the swap function are %d and %d\n",a,b);
getch();
}
void swap (int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("The values of a and b in the swap function after swapping are %d and %d\n",a,b);
}
call-by-reference
#include
#include
void main()
{
int a,b;
void swap (int *p1, int *p2);
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
printf("The values of a and b in the main function before calling the swap function are %d and %d\n",a,b);
swap(&a,&b);
printf("The values of a and b in main function after calling the swap function are %d and %d\n",a,b);
getch();
}
void swap (int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
printf("The values of a and b in the swap function after swapping are %d and %d\n",*p1,*p2);
}
program 23: Define structure within structure consisting of following elements:
i. Employee Code
ii. Employee Name
iii. Employee Salary and
iv. Employee Date_of_joining
Write a C program to read at least 10 records and display them.
struct emp
{
int empcode,esal;
char ename[10],doj[15];
};
main()
{
struct emp e[10];
int n,i;
clrscr();
printf("\n enter the records(empcode,name,doj,sal):");
for(i=0;i<10 br="" i="">scanf("%d%s%s%d",&e[i].empcode,e[i].ename,doj,&e[i].esal);
printf("\n entered records are");
for(i=0;i<10 br="" i="">printf("\n %d\n %s \n %s\n %d",e[i].empcode,e[i].ename,e[i].esal);
getch();
}10>10>
program 24: Write a program to accept a set of 10 numbers and print the numbers using pointers. Find average of these integers.
#include
#include
void main()
{
int i,a[]={10,20,30,4,5,6,7,8,9,20},sum=0;
float avg;
clrscr();
printf("\nElement Using Pointer\n");
for(i=0;i<=9;i++)
{
printf("%d\t",*(a+i));
sum+=*(a+i);
}
avg=sum/2.0;
printf("\nAvg=%f",avg);
getch();
}
No comments:
Post a Comment