Posts

General code 3

Image
CODE NO:- 18 Q) C program to convert a binary number to octal number. Input: 101 Output: 5 Input: 11010 Output: 32 Test Case: ------------- (1) Valid Input:     (a) Only number consisting of 0s and 1s will be given as input (2) Invalid inputs:     (a) Decimal ( b) Fraction  (c) String  (d) Two or more command-line arguments  (e) Negative number (3)You should generate output as follows:      (a) For right output print just the actual Octal Value to STDOUT without any other text.         (b) If any error: print 'ERROR' to the STDOUT without any other text. #LOGIC First, we need to convert the Binary no. into in Decimal no. then convert the decimal no. to octal no. . #Program( In C): In    C language we use  ' // ' for Single line comment and /*---*/ for multi line comment                             ...

Series Printing coding 5

Image
CODE NO:- 17 Q)Write a program to display the sequence 1,4,9,16,25,36,49...... #LOGIC Here we have to raise ith index to power 2. For example: 1^1=1, 2^2=4, 3^2=9 and so on. #Program  (In C): In    C language we use  ' // '  for Single line comment and /*---*/ for multi line comment #include<stdio.h> // for i/p and o/p #include<math.h> // for pow() function int main() { int n,i,a; printf("\nenter no.of terms:"); scanf("%d",&n); for(i=1;i<=n;i++) {             a=pow(i,2); printf("%d, ",a); } return 0; } We got this output by using DEV C++ (IDE) Link: For other series printing problems

Series Printing coding 4

Image
CODE NO:- 16 Q)Write a program to display the Fibonnaci series 0,1,1,2,3,5,8,13,21..... #LOGIC Here we need to add the two previous term to get next term. NOTE: Considering starting index as 0 (zero). For example: we got 1 by adding 1 and 0 i.e we got 2nd term by  adding   1st term and 0th term. #Program  (In C): In    C language we use  ' // '  for Single line comment and /*---*/ for multi line comment APPROACH 1:  Using RECURSION #include<stdio.h> int fibo(int); // function prototype void main() { int n,i; printf("\n enter no.of terms:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("%d, ",fibo(i)); // function call } return 0; } int fibo(int a) // function body { if(a==0) return 0; else if(a==1) return 1; else return fibo(a-1)+fibo(a-2); // recursive call } We got this output by using DEV C++ (IDE) AP...

Series Printing coding 3

Image
CODE NO:- 15 Q)Write a program to display the sequence 1,2,2,4,8,32,256..... #LOGIC If you observe this series carefully you will find this is somewhat same as the Fibonacci series with minute change instead of "+" between the two  previous  terms we are having "*".  NOTE: Considering starting index as 0 (zero). For example: we got 2 by multiplying 1 and 2 i.e we got 2nd term by multiplying 1st term and 0th term. #Program  (In C++): In    C++ language we use  ' // '  for Single line comment and /*---*/ for multi line comment #include<iostream> // header file for i/p and o/p using namespace std; int main() { int n,re,a=1,b=2,i; cout<<"\n enter the value of n (no. of terms):"; cin>>n; cout<< a <<" "<< b ; for(i=2;i<n;i++) // here i=2 because first two terms we had already printed (a,b) { re=a*b; cout<<" "<...

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...