Posts

Showing posts from March, 2020

General code 2

Image
CODE NO:- 14 Q) Write a program to calculate the sum of n natural numbers. #LOGIC Here we need to use the mathematical formula for the sum of n natural numbers i.e (n*(n+1)) /2. #Program( In C++): In    C++ language we use  ' // '  for Single line comment and /*---*/ for multi line comment #include<iostream>  using namespace std; int main() { int n,re; cout<<"\n enter the val of n upto which we want the sum:"; cin>>n; re=(n*(n+1))/2; cout<<"\n result is:"<<re; return 0; } We got this output by using DEV C++ (IDE) Link: For other General Problems

General code 1

Image
CODE NO:- 13 Q) Write a program to calculate the power of a number.  #LOGIC Repeatedly multiply the given no. for p times where p is the given power. For example: given(i/p) no.=2 and given(i/p) power=5                        then the result will be 32 as 2^5=32                        or (2*2*2*2*2=32). #Program( In C++): In    C++ language we use  ' // ' for Single line comment and /*---*/ for multi line comment 1st approach : Using for loop /**** Using for loop ***/ #include<iostream> // header file for i/p and o/p using namespace std; int main() { int n,m,i,re=1; cout<<"\n enter no. and power:"; cin>>n>>m; for(i=0;i<m;i++) { re=re*n; } cout<<"\n result is:"<<re; return 0; } We got this output by using DEV C++ (IDE) 2nd app...

Pattern Printing coding 10

Image
CODE NO:- 12 Print the below pattern #LOGIC Take two variables and assign it with value (((n*(n+1))/2)+2)-(n-1)   and 0(zero)  here n means the no. of entered rows and  (((n*(n+1))/2)+2)-(n-1)  is the first element of the pattern. ex: if n is 4 then   (((n*(n+1))/2)+2)-(n-1)  results 9.  Now copy the value   (((n*(n+1))/2)+2)-(n-1)  in other variable and increment it by 1 in the inner loop whereas in the outer loop decrement it by value (n-i-1). Proceed in the same manner to print the pattern. #Program( In C): In    C language we use  ' // '  for Single line comment and /*---*/ for multi line comment #include<stdio.h>// header file for i/p and o/p int main() { int n,i,k,a,b=0; printf("\n enter the no.of rows:"); scanf("%d",&n); a= (((n*(n+1))/2)+2)-(n-1) ; for(i=0;i<n;i++) {            b=a; ...

Pattern Printing coding 9

Image
CODE NO:- 11 Print the below pattern #LOGIC Take a variable and assign it with value 2 then keep updating it in the inner loop and print the pattern. #Program( In C): In    C language we use  ' // '  for Single line comment and /*---*/ for multi line comment #include<stdio.h> int main() { int n,i,j,k,a=2; printf("\n enter the no.of rows:"); scanf("%d",&n); for(i=0;i<n;i++) {        for(k=0;k<=i;k++)   { printf("%d ",++a); // updating the variable ++a is same as a=a+1        } printf("\n"); } return 0; } We got this output by using DEV C++ (IDE) Link: For other pattern printing problems

Pattern Printing coding 8

Image
CODE NO:- 10 Print the below pattern #LOGIC Take a variable and assign it with value   (n*(n+1))/2   then keep decrementing it in the inner loop and print the pattern. Here n is the no. of entered rows whereas  (n*(n+1))/2 is the first value of pattern. ex: if n=4 then  (n*(n+1))/2 results 10. #Program( In C): In    C language we use  ' // '  for Single line comment and /*---*/ for multi line comment #include<stdio.h>// header file for i/p and o/p int main() { int n,i,k,a; printf("\n enter the no.of rows:"); scanf("%d",&n); a=(n*(n+1))/2; for(i=0;i<n;i++) {              for(k=0;k<n-i;k++)   { printf("%d ",a--); // post decrementing the value              } printf("\n"); } return 0; } We got this output by using ...

Pattern Printing coding 7

Image
CODE NO:- 09 Print the below pattern #LOGIC Take a variable and assign it with value n+2 and print this value using the inner loop whereas in the outer loop decrement the value by 1. Here n is the no. of entered rows whereas n+2 is the first element of the pattern. ex: if n=4 then n+2 result 6. #Program( In C): In    C language we use  ' // '  for Single line comment and /*---*/ for multi line comment #include<stdio.h> // header file for i/p and o/p int main() { int n,i,j,k,a; printf("\n enter the no.of rows:"); scanf("%d",&n); a=n+2; for(i=0;i<n;i++) {           for(k=0;k<n-i;k++)   { printf("%d ",a);           }           a--;// decrementing value by 1 printf("\n"); } return 0; } We got this output by using DEV C++ (IDE) Link: For ot...

Pattern Printing coding 6

Image
CODE NO:- 08 Print the below pattern #LOGIC Take a variable and assign it with value 0 then keep updating it in the inner loop and print the pattern. #Program( In C): In    C language we use  ' // ' for Single line comment and /*---*/ for multi line comment #include<stdio.h> //header file for i/p and o/p int main() { int n,i,k,a=0; printf("\n enter the no.of rows:"); scanf("%d",&n); for(i=0;i<n;i++) {          for(k=0;k<=i;k++)   { printf("%d ",++a);          } printf("\n"); } return 0; } We got this output by using DEV C++ (IDE) Link: For other pattern printing problems

Pattern Printing coding 5

Image
CODE NO:- 07 Print the below pattern #LOGIC #Program( In C): In    C language we use  ' // '  for Single line comment and /*---*/ for multi line comment #include<stdio.h> // header file for i/p and o/p int main() { int n,i,j,k; printf("\n enter the no.of rows:"); scanf("%d",&n); for(i=0;i<n;i++) {          for(j=0;j<n-i-1;j++) // loop for printing spaces   {              printf(" ");          }          for(k=0;k<=i;k++) // loop for printing pattern   { printf("%d ",i+1); /*Observe after %d there is a white space so that                  pattern  is printed correctly */          } printf("\n"); } return 0; } We got this output...

Series code 2

Image
CODE NO:- 06 Q)Find the no. for a user given position for this series  0,0,2,1,4,2,6,3,8,4,…     For example: (1) At position 4 you should get no. :1                             (2) At position 9 you should get no. :8 #LOGIC According to the given series, we can conclude that : 1) At even indexes, we are having a series which starts from 0 and gets incremented by (+2) . 2) At odd indexes, we are having a series which starts from 0 and gets incremented by (+1) . NOTE : Here we are considering 0 as starting index. #Program  (In C++): #include<iostream> using namespace std; int main() { int n,t; cout<<"\n enter the position :"; cin>>n; if(n%2==0) { n=n/2; t=(n-1); cout<<"The no. at given position is:"<<t; } else { n=n/2+1; t=(n-1)*2; cout<<"The...