Showing posts with label BASIC C. Show all posts
Showing posts with label BASIC C. Show all posts

Thursday, 21 July 2016

PASCALS'S TRIANGLE IS A GEOMETRIC ARRANGEMENT OF THE BINOMIAL COEFFICIENT'S IN A TRIANGLE. IT IS NAMED AFTER BLAISE PASCAL. WAP TO DISPAY N LINES OF THE TRIANGLE

              1
            1  1
          1  2  1
        1  3  3  1
      1  4  6  4  1
    1 5 10 10  5 1
  1 6 15 20 15 6 1

#include<stdio.h>
int fact(int);
main()
{
int k, c,n1,n2,n3,n,j;
printf("Enter the value of n");
scanf("%d",&n);
for(k=1;k<=n;k++)
{
for(j=1;j<=k;j++)
{
int i=0;
n1=fact(j);
n2=fact(j-1);
n3=fact(i);
c=n1/(n2*n3);
printf("%d",c);
i++;
}
printf("\n");
}
}
int fact(int a)
{
int fact=1,j;
if(a>0)
{
for(j=1;j<=a;j++)
{
fact=fact*j;
}
}
else
fact=1;
return fact;
}


WAP TO ADD AND MULTIPLY TWO MATRICES. WRITE A SEPARATE FUNCTIONS TO ACCEPT, DISPLAY AND MULTIPLY THE MATRICES. PERFORM NECESSARY CHECKS BEFORE ADDING AND MULTIPLYING THE MATRICES.

#include<stdio.h>
void accept(int [10][10],int,int);
void add(int [10][10],int [10][10],int,int);
void mul(int [10][10],int [10][10],int,int,int);
void display(int [10][10],int,int);
main()
{
int a[10][10],b[10][10];
int r1,c1,r2,c2;
printf("For 1st Matrix:");
printf("Enter no of rows:");
scanf("%d",&r1);
printf("Enter no of columns:");
scanf("%d",&c1);
printf("For 2nd Matrix:");
printf("Enter no of rows:");
scanf("%d",&r2);
printf("Enter no of columns:");
scanf("%d",&c2);
printf("Enter elements of 1st matrix");
accept(a,r1,c1);
printf("Enter elements of 2nd matrix");
accept(b,r2,c2);
if((r1==r2)&&(c1==c2))
add(a,b,r1,c1);
else
printf("Addition not possible");
if(c1==r2)
mul(a,b,r1,c1,c2);
else
printf("Multiplication not possible");
}
void accept(int a[10][10],int r,int c)
{int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void add(int a[10][10],int b[10][10],int r1,int c1)
{
int i,j;
int d[10][10];
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
d[i][j]=a[i][j]+b[i][j];
}
}
printf("Addition:\n");
display(d,r1,c1);
}
void mul(int a[10][10],int b[10][10],int r1,int c1,int c2)
{
int i,j,v[10][10],k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
v[i][j]=0;
for(k=0;k<c1;k++)
{
v[i][j]=v[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("Multiplication:\n");
display(v,r1,c2);
}
void display(int a[10][10],int r,int c)
{int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}

WAP TO ACCEPT A MATRIX A OF SIZE m*n AND STORE ITS TRANSPOSE IN MATRIX B. DISPLAY MATRIX B. WRITE SEPARATE FUNCTIONS.

#include<stdio.h>
main()
{
int num,j,i,prime[50],n,p=0,c=0,d=0;
printf("Enter value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&num);
for(j=1;j<=num;j++)
{
if(num%j==0)
p=p+1;
}
if(p==2)
{
prime[c]=num;
c++;
d++;
}
p=0;
}
printf("prime numbers from the numbers are\n");
for(i=0;i<d;i++)
{
printf("%d ",prime[i]);
}
}

WAP TO ACCEPT N NUMBERS AND STORE ALL PRIME NUMBERS IN AN ARRAY CALLED PRIME. DISPLAY THIS ARRAY.

#include<stdio.h>
main()
{
int num,j,i,prime[50],n,p=0,c=0,d=0;
printf("Enter value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&num);
for(j=1;j<=num;j++)
{
if(num%j==0)
p=p+1;
}
if(p==2)
{
prime[c]=num;
c++;
d++;
}
p=0;
}
printf("prime numbers from the numbers are\n");
for(i=0;i<d;i++)
{
printf("%d ",prime[i]);
}
}

Wednesday, 13 July 2016

WRITE A FUNCTION FOR LINEAR SEARCH, WHICH ACCEPTS AN ARRAY OF N ELEMENTS AND A KEY AS PARAMETER AND RETURNS THE POSITION OF KEY IN THE ARRAY AND -1 IF KEY IS NOT FOUND , ACCEPT THE KEY TO BE SEARCHED AND SEARCH IT USING THIS FUNCTION. DISPLAY APPROPRIATE MESSAGES.

#include<stdio.h>
int search(int [],int,int);
main()
{
int i,a[50],s,n,p;
printf("Enter no of elements");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter element to be searched");
scanf("%d",&s);
p=search(a,n,s);
if(p==-1)
{
printf("Element not found");
}
else
{
printf("Position=%d",p+1);
}
}
int search(int a[],int n,int s)
{
int i,p=-1;
for(i=0;i<n;i++)
{
if(a[i]==s)
{
p=i;
break;
}
}
return p;
}





ASSIGNMENT 9)

  TO DEMONSTRATE USE OF 1-D ARRAY AND FUNCTIONS.

WAP TO ACCEPT N NUMBERS AND DISPLAY THE ARRAY IN THE REVERSE ORDER. WRITE A SEPARATE FUNCTIONS TO ACCEPT AND DISPLAY

#include<stdio.h>
void accept(int [],int);
void display(int [],int);
main()
{
int a[50],n;
printf("Enter no. of elements:");
scanf("%d",&n);
accept(a,n);
display(a,n);
}
void accept(int a[],int n)
{
int i;
printf("Enter elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display(int a[],int n)
{
int i;
printf("Reverse:");
for(i=n-1;i>=0;i--)
{
printf("\n%d",a[i]);
}

}



ASSIGNMENT 9)

  TO DEMONSTRATE USE OF 1-D ARRAY AND FUNCTIONS.


WRITE A RECURSIVE C FUNCTION TO CALCULATE THE SUM OF DIGITS OF A NUMBER. USE THIS FUNCTION IN MAIN TO ACCEPT A NUMBER AND PRINT SUM OF ITS DIGIT

#include<stdio.h>
int sum(int);
main()
{
int n,c;
printf("Enter the no");
scanf("%d",&n);
c=sum(n);
printf("%d",c);
}
int sum(int n)
{
int r,d;
if(n==0)
return 0;
else
{
r=n%10;
d=r+sum(n/10);
return d;
}

}



ASSIGNMENT 8)

  TO DEMONSTRATE RECURSION

WRITE A RECURSIVE C FUNCTION TO CALCULATE x^y(x raised to y)(DO NOT USE STANDARD LIBRARY FUNCTION)

#include<stdio.h>
int power(int,int);
main()
{
int x,y,a;
printf("Enter the no");
scanf("%d",&x);
printf("Enter the power");
scanf("%d",&y);
a=power(x,y);
printf("%d",a);
}
int power(int x,int y)
{
int a=1;
if(y==0)
return 1;
else
{
a=x*power(x,y-1);
return a;
}
}


ASSIGNMENT 8)

  TO DEMONSTRATE RECURSION

WRITE A RECURSIVE C FUNCTION TO CALCULATE THE GCD OF TWO NUMBERS. USE THIS FUNCTION IN MAIN

GCD CACULATED AS
  gcd(a,b) = a                   if b=0
                  =gcd(b, a mod b) otherwise

#include<stdio.h>
int gcd(int a,int b);
main()
{
int a,b,c;
printf("Enter two nos ");
scanf("%d%d",&a,&b);
c=gcd(a,b);
printf("%d",c);
}
int gcd(int a,int b)
{
int c;
if(b==0)
return a;
else
{
c=gcd(b,a%b);
return c;
}

}



ASSIGNMENT 8)

  TO DEMONSTRATE RECURSION

WRITE A FUNCTION, WHICH ACCEPTS A CHARACTER ANDINTEGER N AS PARAMETER AND DISPLAYS THE NEXT N CHARACTERS

#include<stdio.h>
void display(int,char);
main()
{
int n;
char ch;
printf("Enter a no and character");
scanf("%d %c",&n,&ch);
display(n,ch);
}
void display(int x,char c)
{
int i;
for(i=1;i<=x;i++)
{
printf("%c\n",c+i);
}

}


ASSIGNMENT 7)

  TO DEMONSTRATE WRITING C PROGRAMS IN MODULAR WAY (USE OF USER DEFINED FUNCTIONS)

WRITE A FUNCTIONO isEven, WHICH ACCEPTS AN INTEGER AS PARAMETER AND RETURNS 1 IF THE NUMBER IS EVEN AND 0 OTHERWISE. USE THIS FUNCTION IN MAIN TO ACCEPT N NUMBERS AND CHECK IF EVEN / ODD

#include<stdio.h>
int isEven(int);
main()
{
int n,no,c;
int i;
printf("Enter a no");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&no);
c=isEven(no);
if(c==1)
printf("EVEN NO");
else 
printf("ODD NO");
}}
int isEven(int x)
{
if(x%2==0)
return 1;
else 
return 0;

 }


ASSIGNMENT 7)

  TO DEMONSTRATE WRITING C PROGRAMS IN MODULAR WAY (USE OF USER DEFINED FUNCTIONS)

Tuesday, 21 June 2016

WAP WHICH ACCEPTS A CHARACTER FROM THE USER AND CHECKS IF IT IS A AN ALPHABET, DIGIT OR PUNCTUATION MARK

IF IT IS ALPHABET , CHECK IF IT IS UPPERCASE AND THEN CHANGE THE CASE


#include<stdio.h>
#include<math.h>
main()
{
char ch;
printf("Enter a character");
scanf("%c",&ch);
if(isalpha(ch))
{
printf("%c is an alphabet",ch);
if(isupper(ch))
printf("%c",tolower(ch));
else
printf("%c",toupper(ch));
}
else if(isdigit(ch))
{
printf("%c is a digit",ch);
}
else if(ispunct(ch))
{
printf("%c is a punctuation mark");
}
}



ASSIGNMENT 6)

  TO DEMONSTRATE MENU DRIVEN PROGRAMS AND USE OF STANDARD LIBRARY FUNCTIONS

You should read following topics before starting this exercise
   1) Use of Switch statement to create menus as in Assignment 3.
   2) Use of while and do- while as in Assignment 4.

ctype.h : contains function prototypes for performing various operations on characters. Some commenly used functions are listed below.

FUNCTION                                                               PURPOSE
isalpha()                                  check whether a character is alphabet
isalnum()                                 check whether a character is alphanumeric
isdigit()                                    check whether a character is a digit
isspace()                                   check whether a character is space
ispunct()                                   check whether a character is a punctuation mark
isupper()                                   check whether a character is in uppercase
islower()                                   check whether a character is in lowercase
toupper()                                   converts a character to uppercase
tolower()                                   converts a character to lowercase

math.h() : this function is used to perform various mathematical operations

ceil                                           smallest integer not less than parameter
cos
cosh
exp(double x)                           computes e^x
fabs                                           absolute value
floor                                          largest integer not less than parameter      
fmod                                          floating point reamainder
log
log10
pow(x,y)                                   computes x^y
sin
sinh
sqrt
tan
tanh


Menu Driven program

   A program that obtains input from a user by displaying a list of options – the menu – from which the user indicates his/her choice. Systems running menu-driven programs are commonplace, ranging from microprocessor controlled washing machines to bank cash dispensers. In the case of the cash dispenser, single keys are pressed to indicate the type of transaction (whether a receipt is wanted with the cash, or if a statement of the bank balance is required) and with many, a single key is pressed to indicate the amount of money required.

Menu-driven systems are advantageous in two ways: firstly, because input is via single key strokes, the system is less prone to user error; secondly, because only a limited range of characters are “allowed”, the way in which the input is to be entered is unambiguous. This contributes toward making the system more user-friendly. Compare command-line interface.



WRITE A PROGRAM, TO PERFORM THE FOLLOWING OPERATIONS TILL THE USER SELECT EXIT.

USE STANDARD LIBRARY FUNCTIONS FROM math.h
i. Power          ii. Sqr. Root        iii.Floor        iv. Ceiling        v. Exit

#include<stdio.h>
#include<math.h>
main()
{
int n;
double pr,n1,p,a,f,sqr,n3,n4;
do
{
printf("Enter your choice");
printf("1.Power\n2.Square Root\n3.Floor\n4.Ceiling\n5.Exit");
scanf("%d",&n);
if(n==1)
{
printf("Enter a no and power");
scanf("%lf %lf",&n1,&p);
printf("%lf",pow(n1,p));
}
if(n==2)
{
printf("Enter a no");
scanf("%lf",&a);
sqr=sqrt(a);
printf("%lf",sqr);
}
if(n==3)
{
printf("Enter a no");
scanf("%lf",&n3);
printf("%lf",floor(n3));
}
if(n==4)
{
printf("Enter a no");
scanf("%lf",&n4);
printf("%lf",ceil(n4));
}
}while(n!=5);
}




ASSIGNMENT 6)

  TO DEMONSTRATE MENU DRIVEN PROGRAMS AND USE OF STANDARD LIBRARY FUNCTIONS

You should read following topics before starting this exercise
   1) Use of Switch statement to create menus as in Assignment 3.
   2) Use of while and do- while as in Assignment 4.

ctype.h : contains function prototypes for performing various operations on characters. Some commenly used functions are listed below.

FUNCTION                                                               PURPOSE
isalpha()                                  check whether a character is alphabet
isalnum()                                 check whether a character is alphanumeric
isdigit()                                    check whether a character is a digit
isspace()                                   check whether a character is space
ispunct()                                   check whether a character is a punctuation mark
isupper()                                   check whether a character is in uppercase
islower()                                   check whether a character is in lowercase
toupper()                                   converts a character to uppercase
tolower()                                   converts a character to lowercase

math.h() : this function is used to perform various mathematical operations

ceil                                           smallest integer not less than parameter
cos
cosh
exp(double x)                           computes e^x
fabs                                           absolute value
floor                                          largest integer not less than parameter      
fmod                                          floating point reamainder
log
log10
pow(x,y)                                   computes x^y
sin
sinh
sqrt
tan
tanh



Menu Driven programs

    A program that obtains input from a user by displaying a list of options – the menu – from which the user indicates his/her choice. Systems running menu-driven programs are commonplace, ranging from microprocessor controlled washing machines to bank cash dispensers. In the case of the cash dispenser, single keys are pressed to indicate the type of transaction (whether a receipt is wanted with the cash, or if a statement of the bank balance is required) and with many, a single key is pressed to indicate the amount of money required.

Menu-driven systems are advantageous in two ways: firstly, because input is via single key strokes, the system is less prone to user error; secondly, because only a limited range of characters are “allowed”, the way in which the input is to be entered is unambiguous. This contributes toward making the system more user-friendly. Compare command-line interface.

The Standard Library Functions


Some of the "commands" in C are not really "commands" at all but are functions. For example, we have been using printf and scanf to do input and output, and we have used rand to generate random numbers - all three are functions.
There are a great many standard functions that are included with C compilers and while these are not really part of the language, in the sense that you can re-write them if you really want to, most C programmers think of them as fixtures and fittings. Later in the course we will look into the mysteries of how C gains access to these standard functions and how we can extend the range of the standard library. But for now a list of the most common libraries and a brief description of the most useful functions they contain follows:
  
  1. stdio.h: I/O functions:
    1. getchar() returns the next character typed on the keyboard.
    2. putchar() outputs a single character to the screen.
    3. printf() as previously described
    4. scanf() as previously described
  2. string.h: String functions
    1. strcat() concatenates a copy of str2 to str1
    2. strcmp() compares two strings
    3. strcpy() copys contents of str2 to str1
  3. ctype.h: Character functions
    1. isdigit() returns non-0 if arg is digit 0 to 9
    2. isalpha() returns non-0 if arg is a letter of the alphabet
    3. isalnum() returns non-0 if arg is a letter or digit
    4. islower() returns non-0 if arg is lowercase letter
    5. isupper() returns non-0 if arg is uppercase letter
  4. math.h: Mathematics functions
    1. acos() returns arc cosine of arg
    2. asin() returns arc sine of arg
    3. atan() returns arc tangent of arg
    4. cos() returns cosine of arg
    5. exp() returns natural logarithim e
    6. fabs() returns absolute value of num
    7. sqrt() returns square root of num
  5. time.h: Time and Date functions
    1. time() returns current calender time of system
    2. difftime() returns difference in secs between two times
    3. clock() returns number of system clock cycles since program execution
  6. stdlib.h:Miscellaneous functions
    1. malloc() provides dynamic memory allocation, covered in future sections
    2. rand() as already described previously
    3. srand() used to set the starting point for rand()

WAP TO DISPLAY ALL PRIME NUMBERS BETWEEN __ TO__

#include<stdio.h>
main()
{
int i,j,x,y;
int div=0;
printf("Enter two nos\n");
scanf("%d%d",&x,&y);
for(i=x;i<=y;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
div=div+1;
}
if(div==2)
printf("%d ",i);
div=0;
}

}



ASSIGNMENT NO 5)

  TO DEMONSTRATE USE OF NESTED LOOPS

In the previous exercise, you used while, do-while and for loops. You should read following topics before starting this exercise.

1) Different types of loop structures in C.
2) Syntax for these statements.
3) Usage of each loop structure.

Nested loop means a loop that is contained within another loop. Nesting can be done upto any levels. However the inner loop has to be completely enclosed  in the outer loop. No overlapping of loops is allowed.

i) Nested for loop
   Syntax=        for(inilz;condn;incr/dcr)
                        {
                                     for(inilz;condn;incr/dcr)
                                            {
                                                      statements;
                                             }                  
                        }

ii) Nested while loop/ do- while loop
    Syntax=      while(conditions1)
                         {
                                 while(condition 2)
                                {
                                     stmts;
                                 }
                          stmts;
                          }

         do
             {
                while(condition1)
                 {
                    stmts;
                  }
                 stmts;
            }  while(condition2)



EXAMPLES OF DO-WHILE & FOR LOOPS


*
**
***
****
*****
#include <stdio.h>
int main()
{
    int i=1,j;
    do
    {
        j=1;
        do
        {
            printf("*");
            j++;
        }while(j <= i);
        i++;
        printf("\n");
    }while(i <= 5);
    return 0;
}
In this program, nested do-while loop is used to print the star pattern. The outermost loop runs 5 times and for every loop, the innermost loop runs i times which is 1 at first, meaning only one "*" is printed, then on the next loop it's 2 printing two stars and so on till 5 iterations of the loop executes, printing five stars. This way, the given star pattern is printed.

Nested for loop

A for loop inside another for loop is called nested for loop.

Syntax of Nested for loop

for (initialization; condition; increment/decrement)
{
    statement(s);
    for (initialization; condition; increment/decrement)
    {
        statement(s);
        ... ... ...
    }
    ... ... ...
}

Flowchart of Nested for loop

flowchart of nested for loop in c programming

Example of Nested for loop

 C program to print all the composite numbers from 2 to a certain number entered by user.
#include<stdio.h>
#include<math.h>
int main()
{
    int i,j,n;
    printf("Enter a number:");
    scanf("%d",&n);
    for(i=2;i<=n;i++)
    {
        for(j=2;j<=(int)pow(i,0.5);j++)
        {
            if(i%j==0)
            {
                printf("%d is composite\n",i);
                break;
            }    
        }
    }
    return 0;
}
Output
Enter a number:15
4 is composite
6 is composite
8 is composite
9 is composite
10 is composite
12 is composite
14 is composite
15 is composite
A number is said to be composite if it has at least one factor other than 1 and itself. This program prints all the composite numbers starting from 2 to a certain number n, entered by user. We need to use a nested loop to solve this problem. The outer for loop runs from 2 to n and the inner loop is used to determine whether a number is composite or not. We need to check for that factor starting from 2 to integer part of square root of that number.
Consider 15, its square root is nearly 3.873. Here, the integer part is 3. Now, if there is a factor of 15 from 2 to 3 then it is composite. Here, 3 is a factor of 15. Hence, 15 is a composite number.


           

WAP TO DISPLAY MULTIPLICATION TABLES FROM __TO__HAVING N MULTIPLES EACH. THE OUTPUT SHOULD BE DISPLAYED IN A TABULAR FORMAT

Eg:
2 x 1= 2              3 x 1=3       ............   9 x 1=9
2 x 2= 4              3 x 2=6       ............   9 x 2=18
............                ...........
2 x 10 = 20         3 x 10=30   ............   9 x 10 =90

#include<stdio.h>
main()
{
int i,j,x,y,n;
printf("Enter two nos");
scanf("%d%d",&x,&y);
printf("Enter the multiple");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=x;j<=y;j++)
{
printf("%d*%d=%d",j,i,i*j);
printf("\t");
}
printf("\n");
}

scanf();
}

ASSIGNMENT NO 5)

  TO DEMONSTRATE USE OF NESTED LOOPS

In the previous exercise, you used while, do-while and for loops. You should read following topics before starting this exercise.

1) Different types of loop structures in C.
2) Syntax for these statements.
3) Usage of each loop structure.

Nested loop means a loop that is contained within another loop. Nesting can be done upto any levels. However the inner loop has to be completely enclosed  in the outer loop. No overlapping of loops is allowed.

i) Nested for loop
   Syntax=        for(inilz;condn;incr/dcr)
                        {
                                     for(inilz;condn;incr/dcr)
                                            {
                                                      statements;
                                             }                  
                        }

ii) Nested while loop/ do- while loop
    Syntax=      while(conditions1)
                         {
                                 while(condition 2)
                                {
                                     stmts;
                                 }
                          stmts;
                          }

         do
             {
                while(condition1)
                 {
                    stmts;
                  }
                 stmts;
            }  while(condition2)
         

EXAMPLES OF DO-WHILE & FOR LOOPS


*
**
***
****
*****
#include <stdio.h>
int main()
{
    int i=1,j;
    do
    {
        j=1;
        do
        {
            printf("*");
            j++;
        }while(j <= i);
        i++;
        printf("\n");
    }while(i <= 5);
    return 0;
}
In this program, nested do-while loop is used to print the star pattern. The outermost loop runs 5 times and for every loop, the innermost loop runs i times which is 1 at first, meaning only one "*" is printed, then on the next loop it's 2 printing two stars and so on till 5 iterations of the loop executes, printing five stars. This way, the given star pattern is printed.

Nested for loop

A for loop inside another for loop is called nested for loop.

Syntax of Nested for loop

for (initialization; condition; increment/decrement)
{
    statement(s);
    for (initialization; condition; increment/decrement)
    {
        statement(s);
        ... ... ...
    }
    ... ... ...
}

Flowchart of Nested for loop

flowchart of nested for loop in c programming

Example of Nested for loop

 C program to print all the composite numbers from 2 to a certain number entered by user.
#include<stdio.h>
#include<math.h>
int main()
{
    int i,j,n;
    printf("Enter a number:");
    scanf("%d",&n);
    for(i=2;i<=n;i++)
    {
        for(j=2;j<=(int)pow(i,0.5);j++)
        {
            if(i%j==0)
            {
                printf("%d is composite\n",i);
                break;
            }    
        }
    }
    return 0;
}
Output
Enter a number:15
4 is composite
6 is composite
8 is composite
9 is composite
10 is composite
12 is composite
14 is composite
15 is composite
A number is said to be composite if it has at least one factor other than 1 and itself. This program prints all the composite numbers starting from 2 to a certain number n, entered by user. We need to use a nested loop to solve this problem. The outer for loop runs from 2 to n and the inner loop is used to determine whether a number is composite or not. We need to check for that factor starting from 2 to integer part of square root of that number.
Consider 15, its square root is nearly 3.873. Here, the integer part is 3. Now, if there is a factor of 15 from 2 to 3 then it is composite. Here, 3 is a factor of 15. Hence, 15 is a composite number.

WAP TO DISPLAY N LINES AS FOLLOWS (here n = 4)

A     B      C      D
E     F      G
H     I
J

#include<stdio.h>
main()
{
int i,j,n;
printf("Enter no of lines");
scanf("%d",&n);
int t=65;
for(i=n;i>=0;i--)
{
for(j=1;j<=i;j++)
{
printf("%c",t);
printf("\t");
t=t+1;
}
printf("\n");

}}


ASSIGNMENT NO 5)

  TO DEMONSTRATE USE OF NESTED LOOPS

In the previous exercise, you used while, do-while and for loops. You should read following topics before starting this exercise.

1) Different types of loop structures in C.
2) Syntax for these statements.
3) Usage of each loop structure.

Nested loop means a loop that is contained within another loop. Nesting can be done upto any levels. However the inner loop has to be completely enclosed  in the outer loop. No overlapping of loops is allowed.

i) Nested for loop
   Syntax=        for(inilz;condn;incr/dcr)
                        {
                                     for(inilz;condn;incr/dcr)
                                            {
                                                      statements;
                                             }                  
                        }

ii) Nested while loop/ do- while loop
    Syntax=      while(conditions1)
                         {
                                 while(condition 2)
                                {
                                     stmts;
                                 }
                          stmts;
                          }

         do
             {
                while(condition1)
                 {
                    stmts;
                  }
                 stmts;
            }  while(condition2)
         

EXAMPLES OF DO-WHILE & FOR LOOPS


*
**
***
****
*****
#include <stdio.h>
int main()
{
    int i=1,j;
    do
    {
        j=1;
        do
        {
            printf("*");
            j++;
        }while(j <= i);
        i++;
        printf("\n");
    }while(i <= 5);
    return 0;
}
In this program, nested do-while loop is used to print the star pattern. The outermost loop runs 5 times and for every loop, the innermost loop runs i times which is 1 at first, meaning only one "*" is printed, then on the next loop it's 2 printing two stars and so on till 5 iterations of the loop executes, printing five stars. This way, the given star pattern is printed.

Nested for loop

A for loop inside another for loop is called nested for loop.

Syntax of Nested for loop

for (initialization; condition; increment/decrement)
{
    statement(s);
    for (initialization; condition; increment/decrement)
    {
        statement(s);
        ... ... ...
    }
    ... ... ...
}

Flowchart of Nested for loop

flowchart of nested for loop in c programming

Example of Nested for loop

 C program to print all the composite numbers from 2 to a certain number entered by user.
#include<stdio.h>
#include<math.h>
int main()
{
    int i,j,n;
    printf("Enter a number:");
    scanf("%d",&n);
    for(i=2;i<=n;i++)
    {
        for(j=2;j<=(int)pow(i,0.5);j++)
        {
            if(i%j==0)
            {
                printf("%d is composite\n",i);
                break;
            }    
        }
    }
    return 0;
}
Output
Enter a number:15
4 is composite
6 is composite
8 is composite
9 is composite
10 is composite
12 is composite
14 is composite
15 is composite
A number is said to be composite if it has at least one factor other than 1 and itself. This program prints all the composite numbers starting from 2 to a certain number n, entered by user. We need to use a nested loop to solve this problem. The outer for loop runs from 2 to n and the inner loop is used to determine whether a number is composite or not. We need to check for that factor starting from 2 to integer part of square root of that number.
Consider 15, its square root is nearly 3.873. Here, the integer part is 3. Now, if there is a factor of 15 from 2 to 3 then it is composite. Here, 3 is a factor of 15. Hence, 15 is a composite number.