Showing posts with label Dev. Show all posts

Write a program in C++ to find the Factorial by recursive function.

Write a program to find the Factorial by recursive function. This function will use recursion to find factorial of a given number. You can also calculate factorial a number with non recursive method for. Click Here to get code of non recursive method to find factorial of a given number. 



#include<iostream.h>

using namespace std;

int recurs_funct(int num){
    static int i=1;// to make one number initialize
    cout<<i<<" : numbers"<<endl; // count the function calls

    if(num==1){
      return 1;//base
       }
   else{
        i++;
        return  num=num*recurs_funct(num-1);
        }
}

int main(){

    cout<<"Enter to number to find Factorial: ";
    int number;
    cin>>number;
    number=recurs_funct(number); // function call
    cout<<"\t\t\tFactorial of number is: "<<number;
    cout<<"\n\n\n\n";
    system("pause");



}

Write a program in c language to compare two strings by using pointers.

#include <stdio.h>
#include <conio.h>

int compare_strings(char*, char*);

int main()
{
    char str1[100], str2[100], result;

    printf("Enter first string: ");
    gets(str1);

    printf("Enter second string: ");
    gets(str2);

    result = compare_strings(str1, str2);

    if ( result == 0 )
       printf("Strings are same.\n");
    else
       printf("Entered strings are not equal.\n");

    getch();
}

int compare_strings(char *str1, char *str2)
{
   while(*str1==*str2)
   {
      if ( *str1 == '\0' || *str2 == '\0' )
         break;

      str1++;
      str2++;
   }
   if( *str1 == '\0' && *str2 == '\0' )
      return 0;
   else
      return 1;
}

Write a program that will determine the prime factors of a specified factorial


#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{

int number,div, num1;
int arr[20], i=0; 
int result=0, large_no;
cout<<"Enter a number to know its prime factor: "; 
cin>>number;
num1= number;
cout<<"\nThe prime factors of "<<number<<" are: \n\n";

div = 2;

while(number!=0)
{
if(number%div!=0)
div = div + 1;

else {
number = number / div;
cout<<div<<" ";
arr[i]= div;
i++;

if(number==1)
break;
}
}
large_no= arr[i-1];


cout<<endl;
cout<<"In this prime factorss.. "<<endl;
for(int j=2;j<=large_no;j++)
{
for(int k=0;k<=large_no;k++)
{

if(j==arr[k])
{
result=result+1;
}


cout<<j<<" comes "<< result<<" time "<<endl;
result=0;
}


system("pause");
}

Write a program to check whether the given number is palindrome or not?

Note: Polindrome is a number that is same if we reverse it. e.g: 151 reverse is 151



#include <iostream>
using namespace std;

int main()
{
    int number, ans = 0, temp;
    cout << "Enter a number: ";
    cin >> number;
    temp = number;
    while( number != 0)
    {
           ans = ans*10;
           ans = ans + number%10;
           number = number/10;
    }
    if(ans == temp)
    {
           cout << "Number is polindrome.";
    }
    else
    {
           cout << "Number is not polindrome.";
    }
    system("pause");
}

Write a program to find GCD and LCM in C++ language

#include <iostream>
using namespace std;

int main()
{
    int a, b, temp, gcd, lcm, x, y;
    cout << "Enter two number: ";
    cin >> x>>y;
   
    a = x;
    b = y;
   
    while(b!= 0)
    {
           temp = b;
           b = a%b;
           a = temp;
    }
   
    gcd = a;
    lcm = x*y/gcd;
   
    cout << "GCD is: "<<gcd<<"\nLCM is: "<<lcm<<endl;
    system("pause");
}

Write a program to find first 1000 armstrong numbers in C++ language

Note: Armstrong numbers are those numbers if we take cube of their digits individually and add them then it will equal to the number.
e.g: 153
1^3 + 5^3 + 3^3 = 153



#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int remainder, temp, sum=0, num, i;
for(i=1; i<=1000; i++)
{
    num = temp = i;
    sum = 0;
    while(temp != 0)
    {
          remainder = temp%10;
          sum = sum + remainder*remainder*remainder;
          temp = temp/10;
    }
    if(num == sum)
           cout <<sum<<endl;
}
    getch();
}

Write a program in C++ to find angle between minutes and second hand, hour and second hand and hour and minute hand.

#include <iostream>
using namespace std;

int main()
{
    int hour, minut, second, angle1, angle2, angle;
    cout << "Enter hour: ";
    cin >> hour;
    cout << "Enter minut: ";
    cin >> minut;
    cout << "Enter second: ";
    cin >> second;
   
    //angle between hour and minut.
    if(angle1 == 12)
    angle1 = 0;
   
    angle1 = hour*30;
    angle2 = minut*6;
 
    angle = angle2 - angle1;
    if(angle < 0)
    {
         angle1 = 360 - angle1;
         angle = angle1 + angle2;
    }
    cout << "Angle between hour and minut is: ";
    cout << angle << endl;
   
    //angle between hour and second.
    if(angle1 == 12)
    angle1 = 0;
   
    angle1 = hour*30;
    angle2 = second*6;

    angle = angle2 - angle1;
    if(angle < 0)
    {
         angle1 = 360 - angle1;
         angle = angle1 + angle2;
    }
    cout << "Angle between hour and second is: ";
    cout << angle << endl;
   
    //angle between hour and second.
    angle1 = minut*6;
    angle2 = second*6;

    angle = angle2 - angle1;
    if(angle < 0)
    {
         angle1 = 360 - angle1;
         angle = angle1 + angle2;
    }
    cout << "Angle between hour and second is: ";
    cout << angle << endl;
   
    cout <<endl;
    system("pause");
}

Write a program to add two numbers by using pointers in C++ language.

#include <iostream>
using namespace std;

int main()
{
    int a, b, *p1, *p2, sum;
    cout << "Enter both numbers: ";
    cin >> a >> b;
    p1 = &a;
    p2 = &b;
    sum = *p1 + *p2;
    cout << "Sum is " << sum <<endl;
    system("pause");
}

Advantage And Disadvantage Of An Array

Array index start from zero.
Advantages of arrays:
 It has a ability of storing a many elements at a time
 It allows random accessing of elements i.e. any element of the array can be random access using indexes.
Disadvantages:
 Determine the size of the array is an essential.
 There is a chance of memory wastage or shortage.
 If we delete one element in an array then we traverse throughout the array..
Creation of an array consists of three steps.
1. First we can declare the array.
2. Then we create a memory
3. And then put a value into memory.

Common type of arrays is: single dimensional array, multidimensional array and etc.

Write a program to subtract general order matrixes in c language.

#include <iostream>
using namespace std;

int main()
{
    int a1[100][100], a2[100][100], sum[100][100], m, n, r, c;  
    cout<<"Enter number of rows: ";
    cin>>m;
    cout<<"Enter number of columns: ";
    cin>>n;
    cout<<"Enter elements of 1st metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a1[r][c];
          }
    }
    cout<<"\nEnter elements of 2nd metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a2[r][c];
          }
    }
    cout<<"\nElements of metrix 1: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a1[r][c]<<"\t";
          }
          cout<<endl;
    }
    cout<<"\nElements of matrix 2: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a2[r][c]<<"\t";
          }
          cout<<endl;
    }
    //Subtraction
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   sum[r][c]=a1[r][c]-a2[r][c];
          }
          cout<<endl;
    }
    cout<<"Subtraction of matrix is: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<sum[r][c]<<"\t";
          }
          cout<<endl;
    }
    system("pause");
}

Write a program to add general matrix in C language.

#include <iostream>
using namespace std;

int main()
{
    int a1[100][100], a2[100][100], sum[100][100], m, n, r, c;  
    cout<<"Enter number of rows: ";
    cin>>m;
    cout<<"Enter number of columns: ";
    cin>>n;
    cout<<"Enter elements of 1st metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a1[r][c];
          }
    }
    cout<<"\nEnter elements of 2nd metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a2[r][c];
          }
    }
    cout<<"\nElements of metrix 1: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a1[r][c]<<"\t";
          }
          cout<<endl;
    }
    cout<<"\nElements of matrix 2: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a2[r][c]<<"\t";
          }
          cout<<endl;
    }
    //addition
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   sum[r][c]=a1[r][c]+a2[r][c];
          }
          cout<<endl;
    }
    cout<<"Addition of matrix is: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<sum[r][c]<<"\t";
          }
          cout<<endl;
    }
    system("pause");
}

Write a c language to tell if a year is leap year or not.

Leap Year: A year which is divided on four with 0 remainder is called leap year.

#include <stdio.h>
#include <conio.h>
int main()
{
               int year;
               printf("Enter a year to check if it is a leap year: ");
               scanf("%d", &year);

                if (year%400 == 0)
                      printf ("%d is a leap year.\n", year);
               else if ( year%100 == 0)
                      printf ("%d is not a leap year.\n", year);
               else if ( year%4 == 0 )
                      printf ("%d is a leap year.\n", year);
               else
                      printf ("%d is not a leap year.\n", year);
                getch();
}

Write a program to print on screen without semicolon.

Program 1


#include <stdio.h>
#include <conio.h>

int main()
{
    if(printf("http://www.compprog.com"))
    {
           
    }
    getch();
}

Program 2


#include <stdio.h>
#include <conio.h>

int main()
{
    while(!printf("http://www.compprog.com"))
    {
            
    }
    getch();
}

Program 3


#include <stdio.h>
#include <conio.h>

int main()
{
    switch(printf("http://www.compprog.com"))
    {
            
    }
    getch();
}


Write a program to calculate factorial of given number in c language.

#include <stdio.h>
#include <conio.h>

int main()
{
   int num, i, sum=1;
   printf("Enter a number: ");
   scanf("%d", &num);
   for(i=1; i<=num; i++)
   {
         sum=sum*i;
   }
   printf("Factorial of %d is %d.", num, sum);
   getch();
}

Write a program in c language to tell that given number is palindromic number or numeral palindrome or not?

palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, …

#include <stdio.h>
#include <conio.h>

int main()
{
   int num, reverse = 0;
   int temp;

   printf("Enter a number to check if it is a palindrome or not: ");
   scanf("%d",&num);

   temp = num;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }
   if ( num == reverse )
      printf("Number %d is a palindrome.\n", num);
   else
      printf("Number %d is not a palindrome.\n", num);
   getch();
}

Write A code Of Fibonacci series without Recursion.

#include<stdio.h>
#include<conio.h>


int main(void)
{
    int n,l=0,m=1,c=0,counter=1;
    printf("How many number you want to Generate: ");
    scanf("%d",&n);
    printf("%d  %d",l,m);
    while(counter<=n-2)
    {
        c=l+m;
        printf("  %d",c);
        l=m;
        m=c;
      counter++;
    }
getch();

}

How We Used Array In Oop

#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

class Employee
{
private:
    char name[30];
    int age;
public:
    void setdata();
    void getdata();
};

void Employee::setdata()
{
    cout<<"\nEnter the name: ";
    cin>>name;

    cout<<"\nEnter the age: ";
    cin>>age;
}

void Employee::getdata()
{
    cout<<"\n\nThe name of employee: "<<name;
    cout<<"\n\nThe age of employee: "<<age;
}

const int size: 4;

int main()
{
    Employee c[size];

    for(int counter=1;counter<=size;counter++)
    {
        cout<<"\nData of manager "<<counter;
        c[counter].setdata();
    }
    cout<<"\n";
    for(int count=1;count<=size;count++)
    {
        cout<<"\n\nManager"<<count;
        c[count].getdata();
    }
    getchar();
    getch();
}

Write a program in c language to concatenate two strings.

#include<conio.h>
#include<stdio.h>

void stringconcatenation(char[],char[]);

 main()
{

    char mrr1[100],mrr2[100];
    printf("Enter the first string: ");
   gets(mrr1);
    printf("Enter the second string: ");
    gets(mrr2);
    stringconcatenation(mrr1,mrr2);
   printf("The String after concatenation: %s",mrr1);
    getch();
}
void stringconcatenation(char mrr1[],char mrr2[])
{
    int l=0,k=0;
    while(mrr1[l]!='\0')
    {
         l++;
    }
  while(mrr2[k]!='\0')
    {
         mrr1[l] = mrr2[k];
         l++;
         k++;
    }
 mrr1[l] ='\0';
}

Write a c language program to convert decimal to binary using recursion.

#include <stdio.h>
#include <conio.h>
long DecToBin(int); //declaration

int main(){

    long BinNo;
    int DecNo;

    printf("Enter any decimal number: ");
    scanf("%d",&DecNo);

    BinNo = DecToBin(DecNo);
    printf("Binary value is: %ld", BinNo);

    getch();
}
//definition
long DecToBin(int DecNo)
{

    static long BinNo,remainder,factor = 1;

    if(DecNo != 0)
    {

         remainder = DecNo % 2;
         BinNo = BinNo + remainder * factor;
         factor = factor * 10;
         DecToBin(DecNo / 2);
    }

    return BinNo;
}

Write a program in c language to reverse a string using recursion.

#include<stdio.h>
#include <conio.h>
char* reverse(char[]); //function declaration

int main()
{

    char number[100],*rev;

    printf("Enter any string/number: ");
    scanf("%s",number);
    rev = reverse(number);
    printf("Reversed of given string/number is: %s",rev);
    getch();
}
//function definition.
char* reverse(char number[])
{

    static int i=0;
    static char rev[100];

    if(*number){
         reverse(number+1);
         rev[i++] = *number;
    }

    return rev;
}