W.A.P To Generate The Following Patterns Using Switch. I)
W.A.P To Generate The Following Patterns Using Switch. I)
W.A.P To Generate The Following Patterns Using Switch. I)
i) *
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
getch();
}
ii) 1
121
12321
1234321
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1;i<=5;i++)
{
for(int j=5;j>=i;j--)
{
printf(" ");
}
for(j=1;j<=1;j++)
{
printf("%d",j);
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
1
printf("\n");
}
getch();
}
iii) A
B B
C C C
D D D D
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=65;i<=69;i++)
{
for(int j=65;j<=i;j++)
{
printf(" %c ",j);
}
printf("\n");
}
getch();
}
2. W.A.P which reads marks of N students in an array and adds a grace of 10 marks to
every element of the array.
#include<stdio.h>
int main()
{
int marks[10],i;
printf("\nEnter the marks of Students(10 max.)");
for(i=0;i<10;i++)
{
printf("\nEnter maks of %d student",i+1);
scanf("%d",&marks[i]);
}
for(i=0;i<10;i++)
{
printf("\nEntered maks of %d student",marks[i]);
}
for(i=0;i<10;i++)
{
marks[i]+=10;
}
for(i=0;i<10;i++)
2
{
printf("\nMarks after adding grace marks for %d student=%d",i+1,marks[i]);
}
return 0;
}
3. W.A.P that finds the maximum number and it’s position in a list of N numbers.
#include<stdio.h>
int main()
{
int list[10],i,j,k;
printf("\nEnter the distinct numbers(10 max.)");
for(i=0;i<10;i++)
{
printf("\nEnter %d number",i+1);
scanf("%d",&list[i]);
}
for(i=0;i<10;i++)
{
printf("\nEntered number at position %d = %d",i+1,list[i]);
}
j=list[0];
k=1;
for(i=1;i<10;i++)
{
if (j<list[i])
{
j=list[i];
k=i;
}
}
3
4. W.A.P to replace the duplicate elements of an array with zero.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],n;
printf("\n \n Enter the number of terms : ");
scanf("%d",&n);
printf("\n \n Enter the elements of the array : ");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n \n The array entered is : ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n \n The Array after removal of duplicate elemnets is : ");
i=0;
while(i<n)
{
for(int k=1;k<n;k++)
{
if(a[i]==a[i+k])
{
a[i]=a[i]-a[i+k];
}
}
printf(" \n ");
printf("\t %d",a[i]);
i++;
}
getch();
}
4
5. W.A.P to find the average of elements entered by the user in an array.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float sum=0.0,av;
int n,a[20];
printf("\n \n Enter the number of elements to be entered : ");
scanf("%d",&n);
printf("\n \n enter the elements : ");
for(int i=0;i<n;i++)
{
scanf("%f",&a[i]);
sum = sum+a[i];
}
av=sum/n;
printf("\n \n average of %d numbers are : %f ",n,av);
getch();
}
6. W.A.P to display the position of the elements entered by the user in a string along with
the addresses of those elements.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{ char list[30];
int len1,i;
printf("\n enter the string ");
scanf("%s",list);
len1=strlen(list);
printf("\n length of the string is : %d ",len1);
for(i=0;i<len1;i++)
printf("\n %c is at %d location & it's address is %u ",list[i],i+1,&list[i]);
getch();
return 0; }
5
7. W.A.P which inserts a value in an array at a particular position.
#include<stdio.h>
int main()
{
int list[10],i,j,k;
for(i=0;i<10;i++)
list[i]=0;
list[j-1]=i;
for(i=0;i<10;i++)
printf("\n%d value=%d",i+1,list[i]);
return 0;
}
8. W.A.P that computes the sum of the diagonal elements of a square matrix.
#include<stdio.h>
#include<conio.h>
main()
{ int n,a[20][20],temp=0,i,j;
printf("\n Enter the size of the square matrix ");
scanf("%d",&n);
printf("\n enter the elements of the matrix");
for(i=0;i<n;i++)
{
for( j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n The matrix entered is : \n ");
for( i=0;i<n;i++)
{
for( j=0;j<n;j++)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
6
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
temp=temp+a[i][j];
}
}
printf("\n the sum of daigonal elements is : %d ",temp);
getch();
return 0; }
9. Given a set of randomly ordered numbers, sort them in ascending order by selection
sort method.
#include<stdio.h>
#include<conio.h>
main( )
{
int arr[10],n,temp ;
printf("\n enter the number of terms ");
scanf("%d",&n);
printf("\n enter the numbers to be sorted ");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for ( int i = 0 ; i <= n - 2 ; i++ )
{
for ( int j = i + 1 ; j <= n - 1 ; j++ )
{
if ( arr[i] > arr[j] )
{
temp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = temp ;
}}}
7
10. W.A.P that reads a string and counts the number of vowels, words and white spaces
present in the string.
#include<stdio.h>
int main()
{
char c,str[100];
int vowel=0,word=0,ws=0,i=0;
printf("Pls enter the String\n");
while((str[i]=getche())!='\r')
{
if(str[i]==' ')ws++;
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O')
vowel++;
i++;
}
word=ws+1;
//printf("\ntest\n");
printf("\nword=%d\nvowel=%d\nws=%d",word,vowel,ws);
return 0;
}
11. W.A.P that allows the user to enter a character until he inputs the character ‘q’. Each
time a character is entered, the program displays whether it is an alphabet or not.
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
int d;
do
{ printf("\n enter the character");
scanf(" %c",&ch);
d=ch;
if(d>65&&d<122)
printf("\n the number is an alphabet");
else
printf("\n the number is not an alphabet");
}
while(ch!='q');
getch();
return 0; }
8
12. Write a menu driven program to do the following :
a) find factorial of a number entered by the user using functions.
b) to swap to numbers using call by value.
c) to reverse a number using functions.
#include<stdio.h>
#include<conio.h>
int fact(int);
int swap(int,int);
int reverse(int);
main( )
{
int num1,num2,f,r;
char choice;
printf("\n a) find factorial of a number entered by the user using functions. \n b)
to swap to numbers using call by value.\n c) to reverse a number using functions.");
printf("\n Enter your choice : ");
scanf("%c",&choice);
printf("\n \n
******************************************************************\n \n");
switch(choice)
{
case 'a': printf("\n enter the number whose factorial needs to be determined
");
scanf("%d",&num1);
f=fact(num1);
printf("\n the factorial of %d is : %d ",num1,f);
break;
case 'b': printf("\n enter two numbers to be swapped ");
scanf("%d %d",&num1,&num2);
swap(num1,num2);
break;
case 'c': printf("\n enter the number to be reversed");
scanf("%d",&num1);
r=reverse(num1);
printf("\n the after reversal is : %d ",r);
break;
}
getch();
return 0;
}
9
return fac;
}
int reverse(int n)
{
int rev=0,d;
while(n!=0)
{
rev=rev*10;
d=n%10;
n=n/10;
rev=rev+d;
}
return rev;
}
#include<stdio.h>
#include<conio.h>
main()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
printf(" %d ",i);
}
printf("\n");
}
getch();
return 0;
}
10
14. Write functions to add, subtract, multiply and divide two complex numbers (x+iy)
and (a+ib) Also write the main program.
#include<stdio.h>
#include<conio.h>
#include<math.h>
11
case 2:
x=sub(a,c);
y=subi(b,d);
printf("\n \n The difference of the complex numbers is : %d + i(%d) ",x,y);
break;
default : printf("\n \n wrong choice ");
}
getch();
}
int main()
{
int a[30],i,j,beg=0,end,num,n,mid,flag;
printf("\n enter the number of elements in an array : ");
scanf("%d",&n);
printf("\n enter the elemnets of an array in sorted form : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the element to be searched : ");
scanf("%d",&num);
end=n-1;
for(mid=(beg+end)/2;beg<=end;mid=(beg+end)/2)
{
if(a[mid]==num)
{
printf("\n number is at the position : %d ",mid+1);
flag=0;
break;
}
if(a[mid]>num)
end=mid-1;
else
beg=mid+1;
}
if(flag)
printf("\n element not found ");
getch();
return 0;
}
12
16. WAP for searching and sorting
#include<stdio.h>
#include<conio.h>
main()
{ int ch,ch1,ch2,x[20],n,y,i,k,temp,a[20],b[20],count=0,l,h,m,flag=0;
printf("\n enter your choice of operation ");
printf("\n \n 1. SEARCHING \n \n 2. SORTING");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n enter your choice of operation ");
printf("\n \n 1. LINEAR SEARCH \n \n 2. BINARY SEARCH");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
13
l=0;
h=n-1;
while(l<=h)
{
m=(l+h)/2;
if(x==a[m])
{
flag=1;
printf("\n \n the number to be searched is at %d position ",m+1);
break;
}
else
if(x>a[m])
l=m+1;
else
if(x<a[m])
h=m-1;
} }
case 2 : printf("\n enter your choice of operation ");
printf("\n \n 1. SELECTION SORT \n \n 2. INSERTION SORT");
scanf("%d",&ch2);
switch(ch2)
{
case 2:
for(int i=0;i<20;i++)
x[i]=0;
printf("\n \n Enter the number of terms to be sorted : ");
scanf("%d",&n);
printf("\n \n Input the %d terms : ",n);
for(k=0;k<n;k++)
{
scanf("%d",&x[k]);
y=x[k];
for(int i=k-1;i>=0 && y<x[i];i--)
x[i+1]=x[i];
x[i+1]=y;
}
printf("\n \n The sorted numbers are : ");
for(i=0;i<n;i++)
printf("\n \n %d ",x[i]);
break;
case 1 : break;
}
}
getch(); return 0 ;
14
////////////////////////////////////////transpose of a matrix//////////////////////////////////////
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[20][20];
int r,col,i,j;
15
//////////////////////////////////////wap for addition of two matrices///////////////////////////
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[20][20],b[20][20],s[20][20];
int r,col,i,j;
16
for(j=0;j<col;j++)
{
printf("%d",b[i][j]);
printf("\t");
}
printf("\n");
}
for(i=0;i<=r-1;i++)
{
for(j=0;j<=col-1;j++)
{
s[i][j]=a[i][j]+b[i][j];
}
}
printf("\n \n The sum of the two matrix is : ");
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
printf("%d",s[i][j]);
printf("\t");
}
printf("\n");
}
getch();
}
17
/////////////////////////////////WAP for multiplication of two matrices///////////////////////////
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[20][20],b[20][20],s[20][20];
int r,col,i,j;
18
{
printf("%d",b[i][j]);
printf("\t");
}
printf("\n");
}
for(i=0;i<=r-1;i++)
{
for(j=0;j<=col-1;j++)
{
s[i][j]=0;
for(int e=0;e<=r-1;e++)
{
s[i][j]=s[i][j]+(a[i][e]*b[e][j]);
}
}
printf("\n \n The product of the two matrix is : ");
for(i=0;i<r;i++)
{
for(j=0;j<col;j++)
{
printf("%d",s[i][j]);
printf("\t");
}
printf("\n");
}
getch();
}
}
19