Pages

Showing posts with label Loops (For While Do-While). Show all posts
Showing posts with label Loops (For While Do-While). Show all posts

Write a program in c language to reverse a string in reverse order without using strrev function or any other function

Sunday, 6 July 2014

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

int main()
{
char str[100], ch, str2[100];
int count = 1, i, count2 = 0;

printf("Enter String: ");
while(ch != 13)
{
ch = getche();
str[count] = ch;
count++;
}
 
    for(i=0; i<=99; i++)
    {
str2[i] = str[i];
}

    for(i=count; i>=0; i--)
    {
str[count2] = str2[i];
count2++;
}

printf("\nRevered string is\n");

for(i = 0; i < count; i++)
{
printf("%c", str[i]);
}

   getch();
}

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 in c language to compare two strings without strcmp function or any other function

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

int main()
{
   char str1[100], str2[100], ch;
   int i, check = 0, count = 0;

for(i=0; i<=100; i++)
{
str1[i] = str2[i] = 0;
}

   printf("Enter the first string: ");
   while(ch != 13)
   {
ch = getche();
str1[count] = ch;
count++;
   }
 
 
count = 0;
ch = NULL;
   printf("\nEnter the second string: ");
   while(ch != 13)
   {
ch = getche();
str2[count] = ch;
count++;
   }

for(i=0; i<=99; i++)
{
if(str1[i] != str2[i])
check = 1;
}


   if( check == 0 )
   {
      printf("\nEntered strings are equal.\n");
   }
   else
   {
      printf("\nEntered strings are not equal.\n");
   }
 
getch();
}

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 in c language to print Floyd's triangle


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

int main()
{
  int n, i,  c, a = 1;

  printf("Enter the number of rows of Floyd's triangle to print: ");
  scanf("%d", &n);

  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ", a);
      a++;
    }
    printf("\n");
  }

  getch();
}


Write a program to find given given number is armstrong or not?

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;


cout << "Enter a number: ";
cin >> i;


num = temp = i;
while(temp != 0)
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if(num == sum)
cout << "Number is Armstrong." <<endl;
else
cout << "Number is not Armstrong." <<endl;

getch();
}

Write a program in c language to find minimum number from array

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

int main()
{
    int array[100], min, size, c, location = 1;

    printf("How many elements you want to enter in array: ");
    scanf("%d",&size);

    printf("Enter %d integers: ", size);

    for ( c = 0 ; c < size ; c++ )
        scanf("%d", &array[c]);

    min = array[0];

    for ( c = 1 ; c < size ; c++ )
    {
        if ( array[c] < min )
        {
           min = array[c];
           location = c+1;
        }
    }

    printf("Minimum element is located at location %d and value is %d: ", location, min);
    getch();
}

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 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 program to calculate factorial of given number in c language.

Tuesday, 16 July 2013

#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 to print counting from 1 to 10 using for loop

Tuesday, 25 June 2013

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    // The loop goes while x < 10, and x increases by one every loop
    for ( x = 0; x < 10; x++ ) {
        /* Keep in mind that the loop condition checks 
           the conditional statement before it loops again.
           consequently, when x equals 10 the loop breaks.
           x is updated before the condition is checked. */   
        printf( "%d\n", x );
    }
    getch();
}

Write a program in c language to find first and second largest number from array.

Monday, 27 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
    int i, a[10], loc1=0, loc2=0, max1=0, max2=0;
    for(i=1; i<=5; i++)
    {
             printf("Enter number: ");
             scanf("%d", &a[i]);
    }
    for(i=1; i<=5; i++)
    {
             if(max1<a[i])
             {
                    max2=max1;
                    max1=a[i];
                    loc2=loc1;
                    loc1=i;
             }
             if(max2<a[i] && a[i]<max1)
             {
                     max2=a[i];
                     loc2=i;
             }
    }
    printf("First largest is %d  at %d and Second largest is %d at %d",max1, loc1, max2, loc2);
    getch();
}


Write a C program to implement the following: 1. Take an int array of 100 elements. Populate it using random number generator built-in function. 2. User will be given the following choices: a) To insert a new element in the array. b) To delete an element in the array. c) To search an item in the array. d) To update an item in the array. e) To show the elements of the array. f) To quit from the program.

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int main()
{
    int i, a[100], loc, item, size=99;
    char option;
    for(i=0; i<=size; i++)
    {
             a[i]= rand() % 20;
    }
    printf("Press a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
    option=getche();
    while(option!='f')
    {
    switch(option)
    {
           case 'a':
                if(size==100)
                {
                             printf("you can never add more elements because array in completlty filled.");
                }
                else
                {
                    printf("\nEnter location where you want to enter number: ");
                    scanf("%d", &loc);
                    printf("\nEnter number: ");
                    scanf("%d", &item);
                    for(i=size+1; i>loc; i--)
                                {
                                         a[i]=a[i-1];
                                }
                   a[loc]=item;
                   size++;
                   break;
                }
    case 'b':
         printf("\nEnter location where you want to delete number: ");
    scanf("%d", &loc);
    for(i=loc; i<=size; i++)
    {
             a[i]=a[i+1];
    }
    size--;
    break;
    case 'c':
         printf("\nEnter number which you want to search from array: ");
    scanf("%d", &item);
    for(i=0; i<=size; i++)
    {
             if(item==a[i])
             {
                         loc=i;
                         break;
             }
    }
    if(i>size)
    {
           printf("\nNumber is not found in array.");
    }
    else
    {
        printf("\n%d is found at location %d", item, loc);
    }
    break;
    case 'd':
         printf("\nEnter location where you want to update number: ");
    scanf("%d", &loc);
    printf("Enter number: ");
    scanf("%d", &item);
    a[loc]=item;
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
    case 'e':
    printf("\n");
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
         default:
         printf("\nYou have entered invalid entry.");
}
printf("\nPress a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
option=getche();
}
}

Looping Tutorial in C++ language.

Saturday, 18 May 2013

What is Loop?
Ans: Loop is a control structure which is used to repeat one task severel time.
For example we want to print our name 5 time it is easy with cin>> statement but if we want ti print name 1000 time it is very difficult to print with cin>> statement. So for this purpose we use loops.
Types of loops on C++
There are three loops in C++
  1. For Loop.
  2. While Loop.
  3. Do-While Loop.
For Loop: Used when we know starting point and ending point of loop.
Syntax of For Loop.
for(Initialization; Condition; Increment/Decrement)
{
           Body of Loop.
}

Initialization: From where we want to start our loop. for example we want to print counting from 1 to 100, here we start from 1, so our initialization is 1.
Condition: We want to print counting from 1 to 100 then our condition will be that loop process his work until reach to 100.
Increment/Decrement: We want to print counting from 1 to 100. So we make increment of 1 every time we next number will be greater than previous number by 1.
Now we write a program to print counting from 1 to 100.

#include <iostream.h> //header file
int main()  // main function from where program will start.

               int i;  // declare a variable i.
               for(i=1; i<=1; i=i+1)   // loop
               { //start loop body
                              cout<<i; //loop body
                } // end loop body
}

in this program i=1 is initialization and i<= 100 means loop run until i will be greater than 100. when i will be greater than 100 means 101 than loop will be terminated. And i=i+1 mean every time i will increased by 1. We can write i=i+1 like this i++.
cout<<i means print value of i as we learn in previous lectures.

While Loop: Used when don't know starting and ending point we know only condition.

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 Find A Maximum Number In Array

Wednesday, 8 May 2013


#include <stdio.h>
#include <conio.h>
int main()
{
  int array[100], maximum, s, d, l = 1;

  printf("Enter the number of elements in array\n");
  scanf("%d", &s);

  printf("Enter %d integers\n", s);

  for (d = 0; d < s; d++)
    scanf("%d", &array[d]);

  maximum = array[0];

  for (d = 1; d < s; d++)
  {
    if (array[d] > maximum)
    {
       maximum  = array[d];
       l = d+1;
    }
  }

  printf("Maximum element is present at location %d and it's value is %d.\n", l, maximum);
 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();
    }
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *