Pages

Showing posts with label Programs for Beginners. Show all posts
Showing posts with label Programs for Beginners. Show all posts

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

Friday, 21 February 2014

#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 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 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 get numbers from user and print average.

Wednesday, 10 July 2013

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

}

Write a program in C language to find area of rectangle

Saturday, 18 May 2013

#include<stdio.h>
#include <conio.h>
  int main()
{

    float length, width;
    float area;

    printf("Enter size of each sides of the rectangle: ");
    scanf("%f %f",&length, &width);
 
    area = length * width;  //calculation to find area of rectangle
    printf("Area of rectangle is: %.3f",area);
 
    getch();
}

Write a program in C language to find area of circle.

#include <stdio.h>
#include <conio.h>
int main()
{
         float radius, area; // declare vaviables
         printf("Enter radius of circle: ");
         scanf("%f", &radius);
         area = 3.1415 * radius * radius; // Formula to find area of circle
         printf("%f\n", area);
         getch();
}

Write a program to create a class name employee and create three attributes and create a setter and a getter function for each attribute and call a default constructor in C++

Monday, 13 May 2013

#include <iostream>
#include <string>
using namespace::std;
class employee
{
      string first_name;
      string last_name;
      int salary;
      public:
             employee()
             {
                       first_name='NULL';
                       last_name='NULL';
                       salary=0;
             }
             void setFirstName()
             {
                  cout<<"Enter Your First Name: ";
                  cin>>first_name;
             }
             void setlastName()
             {
                  cout<<"Enter Your Last Name: ";
                  cin>>last_name;
             }
             void setSalary()
             {
                  cout<<"Enter Your Salary: ";
                  cin>>salary;
                  if(salary<0)
                  {
                              salary=0;
                  }
             }
             void dsplayFirstName()
             {
                  cout<<"Your First Name is: "<<first_name<<endl;
             }
             void displayLastName()
             {
                  cout<<"Your Last Name is: "<<last_name<<endl;
             }
             void displaySalary()
             {
                  cout<<"Your Salary is: "<<salary<<endl;
             }
};
int main()
{
    employee a1;
    a1.setFirstName();
    a1.setlastName();
    a1.setSalary();
    a1.dsplayFirstName();
    a1.displayLastName();
    a1.displaySalary();
    system("pause");
}

Scope resolution operator in C++

#include <iostream>
using namespace::std;
class date
{
      int day;
      int month;
      int year;
      public:
             int setdate(int, int, int);
             void display()
             {
                  cout<<day<<"/"<<month<<"/"<<year<<endl;
             }
};
int date::setdate(int d, int m, int y)
{
    day=d;
    month=m;
    year=y;
}
int main()
{
    int d, m, y;
    date a1;
    cout<<"Enter Day: "; cin>>d;
    cout<<"Enter Month: "; cin>>m;
    cout<<"Enter Year: "; cin>>y;
    a1.setdate(d, m, y);
    a1.display();
    system("pause");
}

Write a program to swap numbers using two variables.

Wednesday, 8 May 2013

#include <stdio.h>
#include <conio.h>
main()
{
      int a, b;
      printf("Enter To Numbers: ");
      scanf("%d%d", &a, &b);
      //swaping
      a=a+b;
      b=a-b;
      a=a-b;
      printf("%d %d", a, b);
      getch();
}

Write a program to print ASCCI code for given character.

#include<stdio.h>
#include<conio.h>
int main()
{
   char ch;
   printf("Enter a chararcter: ");
   ch=getche();

   printf("\nThe ASCII Code for \%c is %d",ch,ch);
   getch();
}

Write A Program To Swap A Two Number With Each Other


#include <stdio.h>
#include <conio.h> 
int main()
{
   int x, y, temporay;
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
   temporay = x;
   x    = y;
   y    = temporay;
   printf("After Swapping\nx = %d\ny = %d\n",x,y);

 getch();
}

Write a program to calculate volum and area of sphere.

#include<stdio.h>
#include<conio.h>
int main()
{
    float r,v,area;
    printf("Enter the radius of the sphere\n ");
    scanf("%f", &r);
    area=4*3.14*r*r*r;
    v=(4/3.14)*3.14*r*r*r;
    printf("volume of the sphere=%f\n", v);
    printf("area of sphere=%f\n", area);
    getch();
    }

Write a program to print hello world on screen in c language

Monday, 22 April 2013

#include <stdio.h> // header file whicj contain printf(); funcion.

#include <conio.h> // header file which contain getch() funcion
int main() // start body of program.
{
       printf("Hello World! This is my first program in C.");  //prompt
       getch(); //function which is mostly use to hold screen while you typing a charachtar on screen
}

Write a program to get a number from user and print on screen in c language

#include <stdio.h> // header file which contain printf(); funcion.

#include <conio.h> // header file which contain getch() funcion
int main() // start body of program.
{
    char num; // declaration of variable which is use to store data during the execution of program
       printf("Enter a number: ");  //prompt
       scanf("%d", &num); // scanf is used to get input from user.
       printf("your have entered %d", num); // %d is formate specifier use to tell about data that is character or number and num is variable name which store our number
       getch(); //function which is mostly use to hold screen while you typing a charachtar on screen
}

An easy code to get reverse of given number in c Language

Saturday, 20 April 2013


#include <stdio.h>
#include <conio.h>
int main()
{
          int num,mod,rem;
          printf("Enter the Number to Find it's Reverse\n");
          scanf("%d",&num);
          while(num!=0)
          {
                     mod=num%10;
                     rem=num/10;
                     printf("%d",mod);
                     num=rem;
          }
          getch();
}

 

Search Box

Most Reading

Contact Form

Name

Email *

Message *