Skip to main content

Posts

Showing posts with the label program

WAP to multiplication of two 2x2 matrix.

//WAP to multiplication of two 2x2 matrix. #include <stdio.h> #include <conio.h> void main() { int i,j,k,a[2][2],b[2][2],c[2][2],sum; clrscr(); printf( "Enter any element of 1st matrix:\n" ); for (i=0;i<2;i++) { for (j=0;j<2;j++) scanf( "%d" ,&a[i][j]); } printf( "Entre any element of 2nd matrix:\n" ); for (i=0;i<2;i++) { for (j=0;j<2;j++) scanf( "%d" ,&b[i][j]); } //Multiplication logic for (i=0;i<2;i++) { sum=0; for (k=0;k<2;k++) { sum = sum + a[i][k] * b[i][j]; c[i][j]=sum; } } printf( "Result matrix:" ); for (i=0;i<2;i++) { for (j=0;j<2;j++) printf( "%d\t" ,c[i][j]); printf( "\n" ); } getch(); }

WAP to read element of two 2x2 matrix and perform addition

//WAP to read element of two 2x2 matrix and perform addition #include <stdio.h> #include <conio.h> void main() { int i,j,a[2][2],b[2][2],c[2][2]; clrscr(); printf( "Enter any element of 1st matrix:\n" ); for (i=0;i<2;i++) { for (j=0;j<2;j++) scanf( "%d" ,&a[i][j]); } printf( "Entre any element of 2nd matrix:\n" ); for (i=0;i<2;i++) { for (j=0;j<2;j++) scanf( "%d" ,&b[i][j]); } for (i=0;i<2;i++) { for (j=0;j<2;j++) c[i][j]=a[i][j]+b[i][j]; } printf( "Element of Result matrix:" ); for (i=0;i<2;i++) { for (j=0;j<2;j++) printf( "%d\t" ,c[i][j]); } getch(); }

WAP to ask n number from user sort them in assending order and display

//WAP to ask n number from user sort them in assending order and display #include <stdio.h> #include <conio.h> void main() { int i,j,n,num[100],temp; clrscr(); printf( "Enter any value of n:\n" ); scanf( "%d" ,&n); printf( "Entre any value in array:\n" ); scanf( "%d" ,&num[i]); for (i=0;i<n;i++) scanf( "%d" ,&num[i]); for (i=0;i<n;i++) { for (j=i+1;j<n;j++) { if (num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } } } printf( "Sorting element of array:" ); for (i=0;i<n;i++) printf( "%d" ,num[i]); getch(); }

WAP to input n number in array & find largest number

//WAP to input n number in array & find largest number #include <stdio.h> #include <conio.h> void main() { int i,num[100],g,n; clrscr(); printf( "Enter any value of n:\n" ); scanf( "%d" ,&n); printf( "Entre any value in array:\n" ); scanf( "%d" ,&num[i]); for (i=0;i<n;i++) scanf( "%d" ,&num[i]); g=num[0]; for (i=0;i<n;i++) { if (num[i]>g) g=num[i]; } printf( "Greater num = %d" ,g); getch(); }

WAP to input 30 number in array & count how many are even and how many are odd.

//WAP to input 30 number in array & count how many are even and how many are odd. #include <stdio.h> #include <conio.h> void main() { int i,num[30],ec=0,oc=0; clrscr(); printf( "Enter value in array:\n" ); for (i=0;i<30;i++) scanf( "%d" ,&num[i]); for (i=0;i<30;i++) { if (num[i]%2==0) ec++; else oc++; } printf( "No of even = %d" ,ec); printf( "No of odd = %d" ,oc); getch(); }

Write a program to check the number is prime or not

//Write a program to check the number is prime or not #include <stdio.h> #include <conio.h> void main() { int i,n,count=0; clrscr(); printf( "Enter any number:\n" ); scanf( "%d" ,&n); for (i=2;i<=n/2;i++) { if (n%i==0) { count++; break ; } } if (coutn==0) printf( "%d is prime number" ,n); else printf( "%d is not prime number" ,n); getch(); }

Write a program to calculate n term of Fibonacci series using for loop 0,1,1,2,3,5,8.....

//Write a program to calculate n term of Fibonacci series using for loop 0,1,1,2,3,5,8,..... #include <stdio.h> #include <conio.h> void main() { int n,i,a,b,c=0; clrscr(); printf( "Enter value of n:" ); scanf( "%d" ,&n); a=0; b=1; if (n=1) printf( "0" ); else if (n>=2) { printf( "0,1" ); for (i=1;i<n-1;i++) { c=a+b; printf( "%d" ,c); a=b; b=c; } } getch(); }

Write a program to calculate factorial of any number

//Write a program to calculate factorial of any number #include <stdio.h> #include <conio.h> void main() { int i,n,fact=1; clrscr(); printf( "Enter any number:\n" ); scanf( "%d" ,&n); for (i=1;i<=n;i++) { fact = fact * i; } printf( "Factorial of %d = %d" ,n,fact); getch(); } /*if input number is 4 then 1x2 1x2x3 1x2x3x4 output is 24*/

Write a program to print sum of all number between 100 to 350 which is divisible by 9

//Write a program to print sum of all number between 100 to 350 which is divisible by 9 #include <stdio.h> #include <conio.h> void main() { int i,sum=0; clrscr(); for (i=100;i<=350;i=++) //run loop from 100 to 350 { if (i%9==0) //add only if the number is divisible by 9 { sum = sum + i; } } printf( "Sum = %d" ,sum); getch(); }

Write a program to print series of n term of natural number & also find sum using for loop

//Write a program to print series of n term of natural number & also find sum using for loop #include <stdio.h> #include <conio.h> void main() { int i,n,sum=0; clrscr(); printf( "Enter any term of number:\n" ); scanf( "%d" ,&n); for (i=1;i<=n;i++) { printf( "%d" ,i); sum=sum+i; } printf( "Sum = %d" ,sum); getch(); }

Write a program to check a number is palindrom or not

//Write a program to check a number is palindrom or not #include <stdio.h> #include <conio.h> void main() { int n,a,r=0,temp; clrscr(); printf( "Enter any number:\n" ); scanf( "%d" &n); temp = n; // copy number to temp while (n) //run loop until n have some value { a=n%10; //from this we get last digit r=r*10+a; n=n/10; } if (temp==r) printf( "%d is Palindrom" ,temp); else printf( "%d is not Palindrom" ,temp); getch(); } /*For example we input a number 121 then a = 121%10 = 1 r = 0*10 + 1 = 1 n = 121/10 = 12 a = 12%10 = 2 r = 1*10 + 2 = 12 n = 12/10 = 1 a = 1%10 = 1 r = 12*10 + 1 = 121 n = 1/10 = 0 temp is equal to r, so that 121 is Palindrom*/

Write a program to print reverse number of given number using while loop

//Write a program to print reverse number of given number using while loop #include <stdio.h> #include <conio.h> void main() { int n,a,r=0; clrscr(); printf( "Enter any number:\n" ); scanf( "%d" &n); while (n) //run loop until n have some value { a=n%10; //from this we get last digit r=r*10+a; n=n/10; } printf( "Reverse = %d" ,r); getch(); } /*For example we input a number 123 then a = 123%10 = 3 r = 0*10 + 3 = 3 n = 123/10 = 12 a = 12%10 = 2 r = 3*10 + 2 = 32 n = 12/10 = 1 a = 1%10 = 1 r = 32*10 + 1 = 321 n = 1/10 = 0 while n become zero while loop stop then print r=321 which is reverse of 123.*/