Skip to main content

Posts

Showing posts from November, 2023

C++ = Write a program that takes n numbers as input. It displays total positive and negative numbers.

Write a program that takes n numbers as input. It displays total positive and negative numbers. Code: #include <iostream> using namespace std; int main() {  int n,num,pos=0,neg=0;  cout<<"Enter number of times of number will be entered : "; cin>>n; cout<<"Enter numbers for "<<n<<" times : "<<endl; for (int i=1;i<=n;i++) { cin>>num; if (num>0) { pos+=num; }     else if (num<0)     {     neg+=num; }     else     {     break; } } cout<<"The sum of positive integer numbers you entered are "<<pos<<endl; cout<<"The sum of negative integer numbers you entered are "<<neg<<endl; return 0; } Result: 

C++ = Write a program that will ask user with four possible answers. The question should be asked 5 times. After the input is gathered, the program should output the number of times each question was selected.

Write a program that will ask user with four possible answers. The question should be asked 5 times. After the input is gathered, the program should output the number of times each question was selected. Code: #include <iostream> using namespace std; int main() {      int ques1=0,ques2=0,ques3=0,ques4=0,num=0;   cout<<"Enter the no: of desired question "<<endl;   cout<<"Press 1 for question 1 "<<endl;   cout<<"Press 2 for question 2 "<<endl;   cout<<"Press 3 for question 3 "<<endl;   cout<<"Press 4 for question 4 "<<endl;      cout<<endl;   for (int i=1;i<=5;i++)   {   cout<<"Enter no: of Question = ";   cin>>num;   if (num==1)   {   ques1=ques1+1;   }   else if (num==2)   {   ques2+=1;   }     else if (num==3)     {     ques3+=1;...

C++ = Two 2x2 ordered matrix multiplication

Create a program which contain M and N matrices with the order of 2x2  then assign them any value After that multiply both matrices and print the  result matrix on console.  Code: #include <iostream> using namespace std; int main() {     int m[2][2],n[2][2];          cout<<"Assign values to first matrix : "<<endl;          for (int i=0;i<2;i++)     {     for (int j=0;j<2;j++)     {     cin>>m[i][j]; } } cout<<"Assign values to second matrix : "<<endl; for (int i=0;i<2;i++) { for (int j=0;j<2;j++) { cin>>n[i][j]; } } int l[2][2]={0}; for (int i=0;i<2;i++) { for (int j=0;j<2;j++) { for (int k=0;k<2;k++) { l[i][j] += m[i][k] * n[k][j]; } } } cout<<"The matrix after multiplication is given below : "<<endl; for (int i=0;i...

C++ = Arrays multiplication and printing on screen

Create a matrix of order 4x3 in program that prompt user his roll no: then multiply 2023 with that created matrix then print it on the screen.  CODE: #include <iostream> using namespace std; int main() {     int a[4][3],b[4][3];          cout<<"Enter single digit of your roll number : ";          for (int i=0;i<4;i++)     {     for (int j=0;j<3;j++)     {     cin>>a[i][j]; } } for (int i=0;i<4;i++) { for (int j=0;j<3;j++) { b[i][j]=a[i][j]*2023; } } cout<<"The matrix of 4x3 after multiplication with 2023 is "<<endl; for (int i=0;i<4;i++) { for (int j=0;j<3;j++) { cout<<b[i][j]<<"      "; }   cout<<endl;  } return 0; } RESULT:

C++ = Printing diamond with asterik

 CODE: #include <iostream> using namespace std; int main() {     for (int i=1;i<=5;i++)     {     for (int j=1;j<=5-i;j++)     {     cout<<" "; }     for (int k=1;k<=(2*i)-1;k++)     {     cout<<"*"; }   cout<<endl; } for (int i=4;i>=1;i--)     { for (int j=1;j<=5-i;j++)     {     cout<<" "; }     for (int k=1;k<=(2*i)-1;k++)     {     cout<<"*"; }        cout<<endl; } return 0; } RESULT:

C++ = Loop2

 CODE: #include <iostream> using namespace std; int main() {     for (int i=1;i<=5;i++)     {     for (int j=1;j<=5-i;j++)     {     cout<<" "; }     for (int k=1;k<=i;k++)     {     cout<<k; }   cout<<endl; } return 0; } RESULT:

C++ = Write a program that takes n numbers as input. It displays total positive and negative numbers.

 Code: #include <iostream> using namespace std; int main() { int num,pos=0,neg=0; cout<<"Enter the number of numbers to be input : "; cin>>num; for (int i=1;i<=num;i++) //1 //2 { int n; cin>>n; if (n>0) { pos++; } else  { neg++; } } cout<<"The number of positive numbers you entered are "<<pos<<endl; cout<<"The number of negative numbers you entered are "<<neg<<endl; return 0; } Result:

C++: Code for Calculator.

Code no: 1 = Using different function  #include <iostream> using namespace std; int sum(int a, int b) { int sum; sum=a+b; return sum; } int subtract(int a, int b) { int subtract; subtract=a-b; return subtract; } float divide(int a,int b) {     float divide; divide=a/b; return divide; } float multiply(int a,int b) { float multiply; multiply=a*b; return multiply; } int main() { int a,b,input,s,sb; float m,d; cout<<"Enter digit one : "; cin>>a; cout<<"Enter digit two : "; cin>>b; cout<<endl; cout<<"1. Press 1 to add both numbers"<<endl<<"2. Press 2 to subtract both numbers"<<endl<<"3. Press 3 to multiply both numbers"<<endl<<"4. Press 4 to divide both numbers"<<endl;     cout<<endl<<"Enter your choice = "; cin>>input;          s=sum(a,b);     sb=subtract(a,b);     m=multiply(a,b);...

C++ = Code to find number in an arrray.

Consider the following integer array of size 10, int array[10]={1,2,3,5,8,10,12,23,28,15} Write a c++ program that takes an integer value from the user at runtime and finds that value in the given array. If the value is found display message "Number is found" else display message "number does not found".   Code: #include <iostream> using namespace std; int main() {     for (int i=1;i<=2;i++)     { int array[10]={1,2,3,5,8,10,12,23,28,15};     int num;     cout<<"Enter any digit : ";     cin>>num;     bool found=false;          for (int i=1;i<=10;i++)     {     if (array[i]==num)     {     found=true;     break; } }          if (found)     {     cout<<"Number found"<<endl; }     else      {     cout<<...

C++ = Code for checking any digit palindrome number

Code no: 1 = Using only main function to perform task. #include <iostream> using namespace std; int main() { int number,original,digit,reverse=0; cout<<"Enter any number : "; cin>>number; original=number; while (number!=0) { digit=number%10; reverse=(reverse*10)+digit; number=number/10; } if (original==reverse) { cout<<"This "<<original<<" is Palindrome number."; } else { cout<<"The "<<original<<" is not Palindrome number."; } } Result: Code no: 2 = Using two functions to perform same task. #include <iostream> using namespace std; int isprime(int num) { int digit=0,reverse=0; while (num!=0) { digit=num%10; reverse=(reverse*10)+digit;  num/=10; } return reverse; } int main() { int num; cout<<"Enter any digit to find its sum = "; cin>>num;          int original=num;          if (i...

C++ = How to sum of digits of any number code.

Code no: 1 = By using array.  #include <iostream> using namespace std; int main()  { int number; cout<<"Enter the n"; cin>>number; int a[number]; int sum=0; cout<<"Enter a digit : "<<endl; for (int i=1;i<=number;i++) { cin>>a[i]; } for (int i=1;i<=number;i++) { sum+=a[i]; } cout<<"The sum of the number : "<<sum; return 0; } Result: Code no: 2 = By using some logic without using array and all the code is in one function. #include <iostream> using namespace std; int main() { int num,origional,sum=0,digit=0; origional=num; cout<<"Enter any no: to finds its sum = "; cin>>num; while(num!=0) { digit=num%10; sum+=digit;  num/=10;  } cout<<"The sum of the digits is "<<sum; return 0; } Result: Code no: 3 = Using same code as of code no: second but using different function to perform same ta...

C++ = Loops no:1