Skip to main content

Posts

Showing posts with the label If Else

Spiral Matrix

#include <stdio.h> #define R 3 #define C 3 void spiralPrint(int m, int n, int a[R][C]) { int i, k = 0, l = 0; /* k - starting row index m - ending row index l - starting column index n - ending column index i - iterator */ while (k < m && l < n) { /* Print the first row from the remaining rows */ for (i = l; i < n; ++i) { printf("%d ", a[k][i]); } k++; /* Print the last column from the remaining columns */ for (i = k; i < m; ++i) { printf("%d ", a[i][n-1]); } n--; /* Print the last row from the remaining rows */ if ( k < m) { for (i = n-1; i >= l; --i) { printf("%d ", a[m-1][i]); } m--; } /* Print the first column from the remaining columns */ if (l < n)

Find the maximum occurrence of a character in a string

#include<stdio.h> #include<conio.h> int main() { char str[20], ch; int count = 0, i,j=0 , max; int arr[10]; printf("\nEnter a string : "); scanf("%s", &str); printf("\nEnter the character to be searched : "); scanf("%c", &ch); for (i = 0; str[i] != '\0'; i++) { if (str[i] == ch) count++; else if(count != 0) { arr[j] = count; j++; count = 0; } } arr[j]=count; max = 0; for(i=0;i<=j;i++) if(max<arr[i]) max=arr[i]; if (max == 0) printf("\nCharacter '%c'is not present", ch); else { printf("\nMaximum occurrence of character '%c' : %d", ch, max); } return (0); } Output   Print   Output Enter a string : aabbbaaab Enter the character to be searched : Maximum occurrence of character 'a' : 3

Solve Quadratic Equation

#include <stdio.h> #include <math.h> void main() { float a,b,c,d,temp,r1,r2; printf("\nEnter the value of a\t:"); scanf("%f",&a); printf("Enter the value of b\t:"); scanf("%f",&b); printf("Enter the value of c\t:"); scanf("%f",&c); d=((b*b)-(4*c*a)); if(d==0) { printf("\nRoots are real and equal\n"); r1=-b/(2*a); printf("\t X1=%f \n \t X2=%f ", r1, r1 ); } else if(d>0) { printf("\nRoots are real and distinct\n"); temp=sqrt(d); r1=(-b+temp)/(2*a); r2=(-b-temp)/(2*a); printf("\n\tX1=%f\n\tX2=%f",r1,r2); } else { printf("\nRoots are Imaginary\n"); d*=-1; temp=sqrt(d); r1=-b/(2*a); r2=temp/(2*a); printf("\n\tX1=(%f + %fi)\n\tX2=(%f - %fi)",r1,r2,r1,r2); } } Output   Print   Download Code Output Enter the value of a :1 Enter the value of b :-11 Enter the value of c :30 Roots are real and d

Check whether an alphabet is Vowel or not

#include <stdio.h> void main() { char in; printf("Enter Character: "); scanf("%c", &in); if(in>=65&&in<=90 || in>=97&& in<=122) { if(in<97) in=in+32; if(in=='a'||in=='e'||in=='i'||in=='o'||in=='u') printf("vowel"); else printf("consonent"); } else printf("Not an valid Alphabet"); } Output   Print   Download Code Output Enter Character: A vowel

Print the Truth Table

#include<stdio.h> int OR(int,int); int AND(int,int); int XOR(int,int); int NOT(int); void main() { int a,b,ch,o; printf("\n-:MENU:-\n1.AND\n2.OR\n3.NOT\n4.XOR\n0.Exit\n"); do{ printf("\nEnter Choice:"); scanf("%d",&ch); switch(ch) { case 1: printf ("\nTruth Table for AND\nA B \tO"); for(a=0;a<=1;a++) { for(b=0;b<=1;b++) { o=AND(a,b); printf("\n%d %d \t%d",a,b,o); } } break; case 2: printf ("\nTruth Table for OR\nA B \tO"); for(a=0;a<=1;a++) { for(b=0;b<=1;b++) { o=OR(a,b); printf("\n%d %d \t%d",a,b,o); } } break; case 3: printf ("\nTruth Table for NOT\

Find the Greatest of Three Numbers

#include<stdio.h> void main() { int x,y,z; printf("Enter values of x, y and z : "); scanf("%d,%d,%d",&x,&y,&z); if(x>=y && x>=z) printf("\n%d is greatest",x); else if(y>=z) printf("\n%d is greatest",y); else printf("\n%d is greatest",z); } Output   Print   Download Code Output Enter values of x, y and z : 4, 9, 2 9 is greatest