Some basic programs based on loops are given below. And most of the problems will be solved like this.

If you are having some more programs please share with me. I am very much fascinated to solve them for you.

Program 19
WAP to 1 to 10 using loops

  1. clc;
  2. clear all;
  3. close all;
  4. for a=1:10
  5.    fprintf(' %d ',a);
  6. end

Program 20
WAP to display factors of number

  1. clc;
  2. clear all;
  3. close all;
  4. i=16;
  5. while(i<=36)
  6.     if(mod(i,4)==0)
  7.         continue;
  8.     end
  9.     i=i+2;
  10.     fprintf('%d',i);
  11. end

Program 21
WAP to print a multiplication table of a number

  1. clc;
  2. clear all;
  3. close all;
  4. n=input('enter an number = ');
  5. i=1;
  6. while(i<=10)
  7.     fprintf('%d*%d = %d\n',n,i,n*i);
  8.     i=i+1;
  9. end

Program 22
WAP to check  prime number

  1. clc;
  2. clear all;
  3. close all;
  4. n=input('enter a number = ');
  5.     i=2;
  6.     while(i<=n)
  7.         if(mod(n,i) == 0)
  8.             break;
  9.         end
  10.         i=i+1;
  11.     end
  12.     if(i==n)
  13.        fprintf('%d is prime no',n);
  14.     else
  15.         fprintf('%d is not prime no',n);
  16.     end

Program 23

WAP to check  prime numbersfrom 110 to 140

  1. clc;
  2. clear all;
  3. close all;
  4. for a= 110:140
  5.     i=2;
  6.     while(i<=a)
  7.         if(mod(a,i) == 0)
  8.             break;
  9.         end
  10.         i=i+1;
  11.     end
  12.     if(i==a)
  13.        fprintf('%d ',a);
  14.     end

Program 24
WAP to print the sum of digits of a number

  1. clc;
  2. clear all;
  3. close all;
  4.     i=0;
  5.     while(i<=10)
  6.         if(i == 5)
  7.             break;
  8.         end
  9.         i=i+1;
  10.        fprintf('%d ',i);
  11.     end
contact-form

Post a Comment

If you have any doubts regarding this topic...
feel free to ask...

Previous Post Next Post