C Program Examples 3

Program Code in C Language

1. Describe steps for algorithm, draw a flow chart and write a program to print first 10 numbers.

For Loop

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

void main()
{
int i;
clrscr();

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

While Loop

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

void main()
{
int i;
clrscr();
i=1;

while(i<=10)
{
printf("%d \n",i);
i++;
}
getch();
}


Do While Loop

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

void main()
{
int i;
clrscr();
i=1;

do
{
printf("%d \n",i);
i++;
}while(i<=10);

getch();
}


2. Describe steps for algorithm, draw a flow chart and write a program to print first 10 numbers in reverse.

For Loop

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

void main()
{
int i;
clrscr();

for(i=10;i>=1;i--)
{
printf("%d \n",i);
}
getch();
}

While Loop

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

void main()
{

int i;
clrscr();
i=10;

while(i>=1)
{
printf("%d \n",i);
i - -;
}
getch();
}


Do While Loop

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

void main()
{

int i;
clrscr();
i=10;

do
{
printf("%d \n",i);
i - -;
}while(i>=1);

getch();
}


3. Describe steps for algorithm, draw a flow chart and write a program to print sum of first 10 numbers.

For Loop

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

void main()
{

int i,sum=0;
clrscr();

for(i=1;i<=10;i++)
{
Sum=sum+i;
}

printf("%d \n",sum);

getch();
}

While Loop

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

void main()
{

int I,sum=0;
clrscr();
i=1;

while(i<=10)
{
Sum=sum+i;
i++;
}

printf("%d ",sum);
getch();
}


Do While Loop

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

void main()
{

int i;
clrscr();
i=1;
sum=0;

do
{
printf("%d \n",i);
sum=sum+1;
i++;
}while(i<=10);

printf("%d",sum);
getch();
}


4. Write a program to print even numbers from first 10 numbers.

For Loop

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

void main()
{

int i;
clrscr();

for(i=1;i<=10;i++)
{
if(i%2==0)
{
printf("%d \n",i);
}
}
getch();
}

While Loop

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

void main()
{

int i;
clrscr();
i=1;

while(i<=10)
{
if(i%2==0)
{
printf("%d \n",i);
}
i++;
}
getch();
}


Do While Loop

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

void main()
{

int i;
clrscr();
i=1;

do
{
if(i%2==0)
{
printf("%d \n",i);
}
i++;
}while(i<=10);

getch();
}


5. Describe steps for algorithm, draw its flow chart and write a program to print the factorial of given number.

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

void main()
{
    int fact,i,n;
    clrscr();
    printf("Enter any number");
    scanf("%d",&n);

    fact=1;
    for(i=1;i<=n;i++)
    {
fact=fact*i;
    }

printf("Factorial value = %d",fact);
getch();
}

6. Describe steps for algorithm, draw its flow chart and write a program to print sum of 1! + 2! + 3! + 4! + 5! + --------------------- + n! 
(if you entered number 5 then o/p is sum=153)

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

void main()
{
int i,fact=1,sum=0,n;
clrscr();
printf("Enter the number");
scanf("%d",&n);

i=1;

fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
sum=sum+fact;
}
printf("sum= %d",sum);
getch();
}

7. Write a program to make sum of  1 + 1/2 + 1/3 + 1/4 + 1/5 + -------------------- + 1/n 
(if you entered number 5 then o/p is sum=2.283334)

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

void main()
{
float i,n,sum=0.00,div=0.00;
clrscr();
printf("Enter the value ");
scanf("%f",&n);

for(i=1;i<=n;i++)
{
div=1/i;
printf("%f",div);
sum=sum+div;
}
printf("=%f",sum);
getch();
}

8. Print output like this:

1
22
333
4444
55555

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

void main()
{
int i,j;
clrscr();

for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

9. Print output like this:
*
**
***
****
*****


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

void main()
{
int i,j;
clrscr();

for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“*");
}
printf("\n");
}
getch();
}


10. Describe steps for algorithm, draw its flow chart and write a program to generate a Fibonacci series like this:    1 1 2 3 5 8 13 ……

#include<stdio.h>
#include<conio.h>
void main()
{
int f,s,t,n,i;
clrscr();
f=1;
s=1;
printf("Enter any number");
scanf("%d",&n);

printf(" %d",f);
printf(" %d",s);

for(i=1;i<=n;i++)
{
t=f+s;
printf(" %d",t);
f=s;
s=t;
}
getch();
}


11.  1 - 2 + 3 - 4 + 5 - 6 + 7 ---------------------- = sum. Print this series and also  display its sum.
(if you entered number 5 then o/p is 1 – 2 + 3 – 4 + 5 = 3)

#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0,n;
clrscr();
printf("Enter value of n");
scanf("%d",&n);
i=1;
for(i=1;i<=n;i++)
{
if(i%2==0)
{
printf("  - %d",i);
sum=sum-i;
}
else
{
if(i==1)
printf("%d",i);
else
printf("  +  %d ",i);

sum=sum+i;
}
}
printf("Sum = %d",sum);
getch();
}

12. Print output like this:

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

void main()
{
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
{
for(k=i;k<=5;k++)
{
printf(" ");
}

for(j=1;j<=i;j++)
{
printf(" *");
}

printf("\n");
}
getch();
}


13. Print output like this:

     1
    12
   123
    12
     1

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

void main()
{
int i,k,j,sp=40,n;
clrscr();

printf("enter number");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(k=0;k<=sp;k++)
{
printf(" ");
}

for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
sp=sp-1;
}
sp=sp+2;


for(i=n-1;i>=1;i--)
{
for(k=0;k<=sp;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",j);
}

printf("\n");
sp=sp+1;
}
getch();
}


14. Print output like this:

     A
    AB
   ABC
    AB
     A

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

void main()
{

int i,k,j,sp=40,n;
clrscr();

printf("enter number");
scanf("%d",&n);

for(i=65;i<=n;i++)
{
for(k=0;k<=sp;k++)
{
printf(" ");
}

for(j=65;j<=i;j++)
{
printf("%c ",j);
}
printf("\n");
sp=sp-1;

}
sp=sp+2;


for(i=n-1;i>=65;i--)
{
for(k=0;k<=sp;k++)
{
printf(" ");
}
for(j=65;j<=i;j++)
{
printf("%c ",j);
}

printf("\n");
sp=sp+1;
}
getch();
}

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes