Showing posts with label for loop. Show all posts

Square Shape Code





#include<iostream.h>
using namespace std;
int main()
{
     for(int i=1;i<=10;i++)
        {
            cout<<"*";
        }
            cout<<endl;
for(int j=1;j<=5;j++)
    {
    for(int k=1;k<=1;k++)
        {
                cout<<"*";
        }
        for(int l=1;l<=8;l++)
              {
                cout<<" ";
              }
                cout<<"*"<<endl;
       }
for(int m=1;m<=10;m++)
 {
 cout<<"*";

 }
 cout<<endl<<endl;
// return 0;
 system("pause");
}

Code Of This Shape . 1***** 12**** 123*** 1234** 12345* 123456








#include<iostream.h>

#include<conio.h>

int main()

{


   int b,c,d;

   for(int b=1; b<=6; b++)

   {

      for(int c=1,a=1; c<=b; c++)

      {

       cout<<a; a++;

      }

      for(int d=5; d>=b; d--)

      {

cout<<"*";

      }

cout<<endl<<endl;

   }

   getch();

}

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


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

Write a Program To Print First 10 Even Number And Their Sum Through For Loop


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

{
int i, sum=0;

for(i=2; i<=10; i++)
{
sum=sum+i;
printf(" %d\n",i);// this shows output of 1st ten even numbers
}
printf("******************************\n\n");
printf("Sum of 1st ten even numbers is =%d\n", sum);

getch();
}

Write a program to get 9 numbers from user in two dimensional array and print it as a matrix.


#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3];
int i,j;
printf("value in a case\n");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
      printf("Enter Value of row %d and column %d: ", i, j);          
      scanf("%d", &a[i][j]);
}
}
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
getch();
}

Write a program to get row number from user and print diamond of given rows


#include <stdio.h>
#include <conio.h>
int main()
{
    int i, j, k, r;
    printf("Enter number of rows: ");
    scanf("%d", &r);
    for(i=1; i<=r; i+=2)
    {
          for(j=r-2; j>=i; j-=2)
          {
               printf(" ");  
          }
          for(k=1; k<=i; k++)
          {
               printf("*");  
          }
          printf("\n");
    }
    for(i=r-2; i>=1; i-=2)
    {
          for(j=r-1; j>=i; j-=2)
          {
               printf(" ");  
          }
          for(k=1; k<=i-1; k++)
          {
               printf("*");  
          }
          printf("\n");
    }
    getch();
}

Write a program to get numbers from user and sort is print on screen

#include <stdio.h>
#include <conio.h>
int main()
{
    int a[10], i, temp;
    printf("Enter Numbers to sort: ");
    for(i=0; i<=9; i++)
    {
         scanf("%d", &a[i]);   
    }
    for(i=0; i<=9; i++)
    {
             if(a[i+1]<a[i])
             {
                    temp=a[i];
                    a[i]=a[i+1];
                    a[i+1]=temp;
                    i=-1;
             }
    }
   

    for(i=0; i<=9; i++)
    {
        printf("%d ", a[i]);    
    }
    getch();
}

Write a program to print triangle using for loop



#include <stdio.h>
#include <conio.h>
int main()
{
          int i, j, k, l, m;
          for(i=1; i<=15; i+=2)
          {
                   for(j=15; j>i; j-=2)
                   {
                            printf(" ");
                   }
                   for(k=1; k<=2;  k++)
                   {
                            printf("*");
                            for(l=1; l<=i-2; l++)
                            {
                                printf(" ");
                            }
                   }
                   printf("\n");
          }
          for(m=1; m<=15; m++)
          {
              printf("*");
          }
       getch();
}

out put 
     *
    * *
  *    *
 *      *
******

Write a program print squire pyramid using for loop.


Square Pyramid through For Loop:

#include<stdio.h>
#include<conio.h>
int main()
{
 int colmn,rows,r,c,m;
 printf("Enter no. of columns: ");
 scanf("%d", &colmn);
 printf("Enter no. of rows: ");
 scanf("%d", &rows);
 for(r=1; r<=colmn; r++)
 {
  if(r==1 || r==colmn)
      printf(" ");
  else
      printf("*");
 }
 printf("\n");
 for(c=1; c<=rows-2; c++)
 {
   printf("*");
   for(m=1; m<=colmn-2; m++)
       printf(" ");
   printf("*\n");
 }
 for(r=1; r<=colmn; r++)
 {
   if(r==1 || r==colmn)
       printf(" ");
   else
       printf("*");
 }
 getch();
 }

Write a program to show the first ten even numbers and their sum



#include<stdio.h>
#include<conio.h>
int main ()
{
int i, sum=0;

for(i=2; i<=10; i=i+2)
{
sum=sum+i;
printf(" %d\n",i);// this shows output of 1st ten even numbers
}
printf("******************************\n\n");
printf("Sum of 1st ten even numbers is =%d\n", sum);
getch();
}

Write a program to print star triangle form.


#include<stdio.h>

#include<conio.h>
int main()
{
    int a,b;
    for(a=7;a>=0;a--)
    {
                    printf("\n");          
                    for(b=0;b<=a;b++)
                    {
                                     printf("*");
                    }
     }
getch();
}

Write a program to print star triangle form.


#include<stdio.h>

#include<conio.h>
int main()
{
    int a,b;
    for(a=7;a>=0;a--)
    {
                    printf("\n");          
                    for(b=0;b<=a;b++)
                    {
                                     printf("*");
                    }
     }
getch();
}