Thursday, 8 June 2023

C Programming - Chapter 3 Loops

 

C - Programming

Chapter 3

LOOPS

FOR

Q3.1 Write a program to print 1 to n.

#include<stdio.h>

#include<conio.h>

 void main()

  {

  int i,n;

  clrscr();

  printf("Enter the limit: ");

  scanf("%d",&n);

  for(i=1;i<=n;i++)

            {

            printf("\t%d",i);

            }

  getch();

  }

 

Q3.2 Write a program to show your name five times

#include<stdio.h>

#include<stdio.h>

 void main()

  {

  Char n[10];

  int i;

  clrscr();

  printf("Enter The Name Please: ");

  scanf("%s",n);

  for(i=1;i<=5;i++)

            {

            printf("\t%d My name is %s",i,n);

            }

  getch();

  }

 

 

 

Q3.3 Write a program to show the number series in reverse order.

#include<stdio.h>

#include<stdio.h>

 void main()

  {

  int i,n;

  clrscr();

  printf("Enter the limit: ");

  scanf("%d",&n);

  for(i=10;i>=0;i--)

            {

            printf("\t%d",i);

            }

  getch();

  }

 

Q3.4 Write a C program to generate multiplication table.

#include<stdio.h>

#include<conio.h>

void main()

{

int x,n,c,i;

printf(“\n enter the number for multiplication table: ”);

scanf(“%d”,&x);

printf(“\n Enter the range: ”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

            {

            c=x*n;

            printf(“\n %d x %d = %d”,x,n,i);

            }

getch();

}

Q3.5 Write a program to show odd numbers only in a range.

#include<stdio.h>

#include<stdio.h>

 void main()

  {

  int i,n;

  clrscr();

  printf("Enter the limit: ");

  scanf("%d",&n);

  for(i=1;i<=n;i+=2)

            {

            printf("\t%d",i);

            }

  getch();

  }

 

Q3.6 Write a program to show even numbers only in a range.

#include<stdio.h>

#include<stdio.h>

 void main()

  {

  int i,n;

  clrscr();

  printf("Enter the limit: ");

  scanf("%d",&n);

  for(i=0;i<=n;i+=2)

            {

            printf("\t%d",i);

            }

  getch();

  }

 

Q3.7 Write a program to show prime numbers only in a range.

 
#include<stdio.h>
 int main()
{
   int n, i = 3, count, c;
   printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);
   if ( n >= 1 )
               {
               printf("First %d prime numbers are :\n",n);
               printf("2\n");
               }
 
   for ( count = 2 ; count <= n ;  )
               {
               for ( c = 2 ; c <= i - 1 ; c++ )
                               {
                               if ( i%c == 0 )
                               break;
                               }
      if ( c == i )
               {
               printf("%d\n",i);
               count++;
               }
      i++;
   }
   return 0;
}

 

Q3.8 Write a program to show Armstrong numbers only in a range.

 
#include <stdio.h>
 int main()
{
int r;
long number = 0, c, sum = 0, temp;
printf("Enter an integer upto which you want to find armstrong numbers\n");
scanf("%ld",&number);
printf("Following armstrong numbers are found from 1 to %ld\n",number);
 
   for( c = 1 ; c <= number ; c++ )
               {
               temp = c;
               while( temp != 0 )
                               {
                                r = temp%10;
                               sum = sum + r*r*r;
                                temp = temp/10;
                                }
      if ( c == sum )
                printf("%ld\n", c);
      sum = 0;
               }
    return 0;
}

 

Q3.9 Write a program to show magic numbers only in a range.

#include<stdio.h>
int main()
{
int low=0,high=0;
printf("Enter the range: low,high");
scanf("%d,%d",&low,&high);

int i=0;
for(i = low; i<= high ;i++)
{
int n = i,sum = n;

while(sum > 9)
{
n = sum;
sum = 0;
while(n > 0)
{
int digit = n%10;
n = n/10;
sum = sum + digit;
}

}
if(sum == 1)
{
printf(" %d is a magic number \n",i);
}
}

return 0;
}

Q3.10 Write a C program to print Fibonacci Series of given range.

#include<stdio.h>

#include<conio.h>

void main()

  {

intn,a=0,b=1,c,i;

clrscr();

printf("Enter the lmit=");

scanf("%d",&n);

printf("\n%d\n%d",a,b);

for(i=2;i<=n;i++)

            {

            c=a+b;

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

            a=b;

            b=c;

            }

getch();

  }

 

Q3.11 Write a C program to get Factorial of given number.

 

#include<stdio.h>

#include<conio.h>

void main()

 {

long int a=1;

int n,i;

clrscr();

printf("Enter the limit of factorial=");

scanf("%d",&n);

for(i=1;i<=n;i++)

            {

            a=a*i;

            }

printf("\nThe result of %d!=%ld",n,a);

getch();

 }

 

Q3.12 Addition this series  1+1/(1*2)+1/(1*2*3)+1/(1*2*3*4)

 

#include<stdio.h>

#include<conio.h>

void main()

 {

 int i,n;

 float  t,s;

 clrscr();

 printf("Enter the no of term=");

 scanf("%d",&n);

 for(i=1;i<=n;i++)

            {

            t=(float)1/(i*i);

            s+=t;

            }

 printf("\nSum of series=%.2f",s);

 getch();

 }

 

Q3.13 Addition of odd number

 

#include<stdio.h>

#include<conio.h>

 void main()

  {

  int n,i,sum=0;

  clrscr();

  printf("Enter the number=");

  scanf("%d",&n);

  for(i=1;i<=(2*n)-1;i+=2)

            {

            sum=sum+i;

            }

  printf("%d",sum);

  getch();

  }

 

Q3.14 Addition this series 1+1*2+1*2*3+1*2*3*4+.......

 

#include<stdio.h>

#include<conio.h>

void main()

 {

 long int a=1,m=0;

 int n,i;

 clrscr();

 printf("Enter the limit=");

 scanf("%d",&n);

 for(i=1;i<=n;i++)

            {

            a=a*i;

            m=m+a;

            }

 printf("\nThe result is above series=%ld",m);

 getch();

 }

 

Q3.15 WAP to find this series 1+1/5+1/9+1/13+……

 

#include<stdio.h>

#include<conio.h>

 void main()

  {

  float sum=0;

  int n,i;

  clrscr();

  printf("Enter the limit=");

  scanf("%d",&n);

  for(i=1;i<=(2*n)-1;i+=2)

            {

            sum=sum+(float)1/i;

            }

  printf("\nResult of above series=%.2f",sum);

  getch();

  }

 

 

Q3.16. Write a program to show ASCII characters with for loop.

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main()

{

int a,i,n,p;

clrscr();

for(i=0;i<=255;i++)

            {

            printf("%d = %c\n",i,i);

            getch();

            }

getch();

}

 

Q3.17. Write a program to check the vowels in a word.

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main()

{

char n[10];

int x,i,a,v=0,c=0;

clrscr();

printf("Enter the name");

scanf("%s",n);

x=strlen(n);

for(i=0;i<x;i++)

            {

            a=n[i];

      

       if((a==65)||(a==69)||(a==73)||(a==79)||(a==85)||(a==97)||(a==101)||

(a==105)||(a==111)||(a==117))

                        {

                        v++;

                        }

            }

       printf(" So do you know ? you have %d vowels  in your good name\n",v);

            getch();

}

 

 

Q3.18. Write a program to check the vowels and consonants in a word.

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main()

{

char n[10];

int x,i,a,v=0,c=0;

clrscr();

printf("Enter the name");

scanf("%s",n);

x=strlen(n);

for(i=0;i<x;i++)

            {

            a=n[i];

      

       if((a==65)||(a==69)||(a==73)||(a==79)||(a==85)||(a==97)||(a==101)||(a==105)||(a==111)||(a==117))

                        {

                        v++;

                        }

       else

                        {

                        c++;

                        }

 

            }

       printf(" So do you know you have %d vowels and %d consonants in your good name\n",v,c);

            getch();

}                                                       

 

 

Q3.19. Write a program to check the vowels and consonants with capitals and semi vowels in a word.

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main()

{

char n[10];

int x,i,a,cvs=0,sv=0,csv=0,sc=0,ssv=0,cc=0,cv=0;

clrscr();

printf("Enter the name");

scanf("%s",n);

x=strlen(n);

for(i=0;i<x;i++)

            {

            a=n[i];

       if((a>=65)&&(a<=92))

                 {

                        if((a==65)||(a==69)||(a==73)||(a==79)||(a==85))

                                    {

                                    cv++;

                                    }

 

                        else if((a==87)||(a==89))

                                    {

                                    csv++;

                                    }

                        else

                                    {

                                    cc++;

                                    }

                  }

       else

 

                        {

                        if((a==97)||(a==101)||(a==105)||(a==111)||(a==117))

                                    {

                                    sv++;

                                    }

                        else  if((a==119)||(a==121))

                                    {

                                    ssv++;

                                    }

                        else

                                    {

                                    sc++;

                                    }

 

                        }

       }

printf("Capital Vowels %d\n",cv);

printf("Capital semi vowel %d\n",cvs);

printf("Capital consonants %d\n",cc++);

printf("Small Vowels %d\n",sv);

printf("Small semi Vowel %d\n",ssv);

printf("Small Consonants %d\n",sc);

 

getch();

}

 

Q3.20. Write a program to show the series of leap years within a range specified.

#include<stdio.h>

#include<conio.h>

void main()

{

int s,n,e,i;

clrscr();

printf("Enter start year");

scanf("%d",&s);

printf("Enter end year");

scanf("%d",&e);

printf("\n\nleap years are following\n\n");

for(i=s;i<=e;i++)

            {

            if(i%4==0)

                        {

                        printf("%d\t",i);

                        }

            }

getch();

}

                                                                                                                    

 

Q3.21. Write a program to make a marquee text with for loop.

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main()

{

char n[20];

int i;

clrscr();

printf("enter your name");

scanf("%s",n);

for(i=0;i<=79;i++)

            {

            clrscr();

            gotoxy(i,20);

            printf("%s",n);

            delay(100);

            }

getch();

}

 

 

 

 

 

 

 

NESTED LOOP

Q. Write a program to fill the entire screen with a smiling face.

#include<stdio.h>

#include<conio.h>

void main()

{

int r,c;

clrscr();

for (r=0;r<=64;r++)

            for(c=0;c<=79;c++)

            printf("%c",2);

printf("\n press any key to exit..");

getch();

}

 

Q. Write a program to generate all combinations of 1, 2 and 3 using for loop.

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1,j=1,k=1;

clrscr();

for (i=1;i<=3;i++)

{

            for (j=1;j<=3;j++)

            {

                        for (k=1;k<=3;k++)

                        printf("%d%d%d\t",i,j,k);

            }

}

printf("\n press any key to exit......");

getch();

}

 

 

 

 

Q. Write a program to show  this shape.

*

**

***

*****

 

#include<stdio.h>

#include<conio.h>

void main()

{

int r,c,a;

clrscr();

printf("enter the no");

scanf("%d",&a);

for(r=0;r<=10;r++)

            {

            for(c=0;c<=r;c++)

                        {

                        printf("*");

                        }

            printf("\n");

            }

getch();

}

 

Q. Write a program to show this shape.

*****

****

***

**

*

 

#include<stdio.h>

#include<conio.h>

void main()

{

int r,c,a;

clrscr();

printf("enter the no");

scanf("%d",&a);

for(r=10;r>=0;r--)

            {

            for(c=0;c<=r;c++)

                        {

                        printf("*");

                        }

            printf("\n");

            }

getch();

}

 

Q. Write a program to produce the following output

                                                1

                                    2                      3

                        4                      5                      6

            7                      8                      9                      10

 

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,k,l,sp;

sp=20;

clrscr();

for (i=1,k=1;i<5;i++)

{

            for (l=1;l<=sp;l++)

                        printf(" ");

            sp-=2;

            for(j=1;j<=i;j++,k++)

                        printf(" %d ",k);

            printf("\n");

}

printf("\n press any key to exit......");

getch();

}

Q. Write a program to produce the following output

                                                1

                                    1                      1

                        1                      2                      1

            1                      3                      3                      1

1                      4                      6                      4                      1

 

 

 

WHILE

Q. Write a C program to reverse any number.

 

 

#include<stdio.h>

#include<conio.h>

void main()

 {

longinta,b=0;

clrscr();

printf("Enter a group of number: ");

scanf("%ld",&a);

while(a!=0)

            {

            b=b*10+a%10;

            a=a/10;

            }

printf("\nThe reverse number is: %ld",b);

getch();

 }

Q. Write a C program to find out sum of digit of given number.

 

#include<stdio.h>

#include<conio.h>

void main()

 {

longinta,b=0,sum=0;

clrscr();

printf("Enter a group of number: ");

scanf("%ld",&a);

while(a!=0)

            {

            b=a%10;

            sum+=b;

            a=a/10;

            }

printf("\nThe sum of digit is: %d",sum);

getch();

 }

Q. Write a C program to find out value of a power number without using pow().

#include<stdio.h>

#include<conio.h>

void main()

{

int p,b,r,i;

b=p=0,r=1,i=1;

printf("\n enter number: ");

scanf("%d",&b);

printf("\n enter power: ");

scanf("%d",&p);

while(i<=p)

            {

            r=r*b;

            i++;

            }

printf("\n %d raised to %d=%d", b,p,r);

getch();

 }

Q. Write a C program to count number of digits in a number.

#include<stdio.h>

#include<conio.h>

void main()

 {

longinta,b=0,count=0;

clrscr();

printf("Enter a group of number: ");

scanf("%ld",&a);

while(a!=0)

            {

            count+=1;

            a=a/10;

            }

printf("\nThe number of digit is: %d",count);

getch();

 }

 

Write a program to check a number is Armstrong number or not

#include <stdio.h>

 int main()

{

   int number, sum = 0, temp, remainder;

 

   printf("Enter an integer\n");

   scanf("%d",&number);

 

   temp = number;

 

   while( temp != 0 )

   {

      remainder = temp%10;

      sum = sum + remainder*remainder*remainder;

      temp = temp/10;

   }

 

   if ( number == sum )

      printf("Entered number is an armstrong number.\n");

   else

      printf("Entered number is not an armstrong number.\n");

 

   return 0;

}

 

Q. Write a C program to check whether the given integer is Magic number or not.

/*Magic number:- A number which is divisible by 9 and also after reverse if it is divisible by 9, then it is said to Magic number.

Example:- Suppose take 18
We know '18' is divisible by 9 (ie., 9 * 2 = 18)
Now After reverse '18' becomes 81.
Here '81' is also is divisible by 9 (ie., 9 * 9 =81 )
So we say '18' is a Magic number.*/

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

int rem, rev=0, n;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
while(n>0)
       {
       rem=n%10;
      rev=(10*rev)+ rem;
      n=n/10;
      }
if(rev%9== 0)
printf("\nMagic Number.");
else
printf("\nNot a Magic number.");
getch( );
}


 

DO..WHILE

Addition two numbers using dowhile

#include<stdio.h>

#include<conio.h>

 void main()

  {

  int a,b,c,x;

  clrscr();

  do

            {

            printf("\nEnter two no=");

            scanf("%d%d",&a,&b);

            c=a+b;

            printf("\nResult of addition=%d",c);

            printf("\nAnother calculation press 1");

            scanf("%d",&x);

            }while(x==1);

  getch();

  }

 

No comments:

Post a Comment

JAVA PROGRAMMING - OOPS CHAPTER 1

Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...