Pages

Showing posts with label Dev. Show all posts
Showing posts with label Dev. Show all posts

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

Sunday, 6 July 2014

#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

Tuesday, 1 July 2014


#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?

Friday, 21 February 2014

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");
}

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

Wednesday, 24 July 2013

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

Tuesday, 16 July 2013

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?

A 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 program in c language to concatenate two strings.

Monday, 15 July 2013

#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;
}

Write a program to print time on screen.

Wednesday, 10 July 2013

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string>
int main(void)
{
    time_t current_time;
    char* c_time_string, finel_time;

    current_time = time(NULL);
    c_time_string = tm(&current_time);
    (void) printf("%s", c_time_string+4);
    getch();
}

Write a program in c language to get numbers from user and print average.

# include<stdio.h>
#include<conio.h>
#define LIMIT 130
int main(){
float marks[LIMIT], average, sum = 0.0;
int i=0;
do {
printf("\nEnter the mark of the student(-1 to finish)");
scanf("%f", &marks[i]);
if (marks[i] >= 0)
sum = sum + marks[i];
}
while(marks[i++] >= 0);
average = sum/(i-1);
printf ("\nThe average is %.2f\n", average);
getch();
}

Write a program in c language to get three numbers from user and print maximum number.

#include<stdio.h>
#include<conio.h>
int maximum(int,int,int);
int main()
{
    int num1,num2,num3;
    printf("enter hte three integer: ");
    scanf("%d %d %d",&num1,&num2,&num3);
    printf("Max is %d",maximum(num1,num2,num3));
    getch();
}
int maximum(int x,int y,int z)
{
   int max;
       if(x>y && x>z)
       {
              max=x;
       }
       if(y>z && y>x)
       {
              max=z;
       }
       if(z>x && z>y)
       {
              max=z;          
       }
       return max;
}
         
                               
                     
                   


Write a program in c language to print counting using function.

#include<stdio.h>
#include<conio.h>
int mul(int);
int main()
{
    int a=10;
    printf("%d", mul(a));
    getch();
}
int mul(int a)
{
       int x;
       for(x=1;x<=a;x++)
       {
              printf("%d\n",x);
       }

}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *