Translate

Labels

Showing posts with label LOOP PROGRAMS. Show all posts
Showing posts with label LOOP PROGRAMS. Show all posts

Saturday, 27 April 2013

ALL PERFECT SQUARE BELOW 1000

/*PROGRAM TO PRINT ALL PERFECT SQUARES BELOW 1000*/
#include<stdio.h>
main()
{
int i=1;
printf("The perfect squares below 1000 are\n");
while(i*i<1000)
{
printf("%5d",i*i);
i++;
}
getch();
}

SUM THE FIRST 50 EVEN NATURAL NUMBERS

/*PROGRAM TO SUM THE FIRST 50 EVEN NATURAL NUMBERS*/
#include<stdio.h>
main()
{
int sum=0,i=2;
while(i<=100)
{
sum+i;
i+=2;
}
printf("Sum of first 50 even natural numbers=%d\n",sum);
getch();
}

OUTPUT
Sum of first 50 natural numbers=2550


Thursday, 21 March 2013

WHETHER A NUMBER IS PRIME NUMBER OR NOT USING FUNCTION

#include<stdio.h>
#include<conio.h>
int prime(int number)
{
int k,rem,flag;
flag=1;
k=2
while(k<=number-1)
{
rem=number%k;
if(rem==0)
{
flag=0;
break;
}
k++;
}
return(flag);
}
void main()
{
int number;
clrscr();
printf("Enter a number \n");
scanf("%d",&number);
if(prime(number))
printf("%d is prime",number);
else
printf("%d is not prime",number);
getch();
}

OUTPUT
Enter a number
6
6 is not a prime number

Enter a number
7
7 is not a prime number

EXPLANATION
Function prime() is defined with one formal argument of int type and is made to return either 1 or 0.The purpose of the function is to take an integer number and find out whether the number is prime or not. If the number is prime, it returns 1.Otherwise it returns 0.
In the main(),an integer number is accepted into the variable n. A call is made to the function prime() by passing the value of n.Since the function the function returns either 1 or 0, the function call has been used as a conditional expression as if (prime(n)) in main().If prime(n) returns 1 then the number is displayed as prime. Otherwise, the number is displayed as not prime.    

Wednesday, 20 March 2013

MULTIPLICATION TABLES OF A RANGE OF NUMBERS

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,j,i,p;
clrscr();
printf("Enter lower limit \n");
scanf("%d",&m);
printf("Enter upper limit \n");
scanf("%d",&n);
for(i=m;i<n;i++)
{
for(j=1;j<=10;j++)
{
p=i*j;
printf("%4d",p);
}
printf("\n");
}
getch();
}
OUTPUT
Enter Lower limit
5
Enter upper limit
10
5     10     15     20     25     30     35     40     45     50
6     12     18     24     30     36     42     48     54     60
7     14     21     28     35     42     49     56     63     70
8     16     24     32     40     48     56     64     72     80
9     18     27     36     45     54     63     72     81     90
10   20     30     40     50     60     70     80     90     100      

WHETHER A NUMBER IS A PRIME NUMBER OR NOT

#include <stdio.h>
#include<conio.h>
void main()
{
int number ,k,flag,rem;
clrscr();
printf("Enter a number \n");
scanf("%d",number);
k=2;
flag=1;
while((k<=number-1)&&(flag==1))
{
rem=number%k;
if(rem==0)
flag=0;
k++;
}
if(flag==1)
printf("%d is prime \n",number);
else
printf("%d is not prime \n",number);
getch();
}
OUTPUT
Enter a number
7
7 is prime
Enter a number
8
8 is not prime

SUM OF FIRST n NATURAL NUMBERS USING WHILE LOOP

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum;
clrscr();
printf("Enter a number \n");
scanf("%d",&n);
i=1;
sum=0;
while(i<=n)
{
sum+=i;
i++;
}
printf("sum =%d\n",sum);
getch();
}

OUTPUT:
First run
Enter a number
10
sum=55

EXPLANATION:
n,i and sum are declared to be variables of int type.n is to collect a natural number;i is to select each natural number from 1 t on; sum is to collect the sum of natural numbers up to n.
First we accept a number into the variables n and initialize sum with zero.The following segment finds the sum of the first n Natural numbers.
while (i<=n)
{
sum+=i;
i++;
}
The sum is collected by the variable sum.The value of the variable sum is then displayed

Saturday, 5 January 2013

PRINTING THE NUMBER WITH RESPECT TO THAT NUMBER

Write a program to print 1 once,2 twice in the next line,3 thrice in the next line and continues up to 10, that is 10 written ten times.This problem requires two loops;the outer loop controls the number of the times the inner loop has to be repeated.Each time the inner loop is executed, the number of times it gets repeated depends on the outer loop control variable,s value.The program is as follows:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
i=1;
while(i<=10)
{
j=1;
while(j<=1)
{
printf("%d",i);
j++;
}
printf("\n");
i++;
}

 The output is as follows:
1
22
333
4444
55555
666666
7777777
88888888
999999999
10101010101010101010

The inner loop of the above program is executed for i number of times where i is the control variable of the outer loop.

ADDING CURRENT NUMBER WITH BEFORE NUMBER UP TO n TERMS


Write a program to generate the following sequence:

1,2,4,7,11,16...........................................20 terms
The second number in the sequence is obtained by adding 1 to previous number, the third number is obtained by adding 2 to the previous number, the fourth number is obtained by adding 3 to the previous number and so on. The loop is to be executed for 20 times.The program is as follows:
#include<stdio.h>
#include<conio.h>
void main()
{
int term;
int i;
i=1;
term=1;        /*control variable*/
while(term<=20)
{
printf("%d\n",i);
i=i+term;
term=term+1;
}

In the above, the variable term is used to control the execution of the loop and the variable i is used as a processing variable.The logic of the program depends on the statement i=i+term; where the next number in the sequence is obtained by adding the value of term to the previous number and the term is incremented by 1 every time the loop executes.The success of the program that uses a while loop depends on the proper use of control and process variables.The control variable is to be initialized, tested in the condition and then updated in the loop.The process variable is used in the logic

Tuesday, 1 January 2013

SUM OF DIAGONAL ELEMENTS USING C PROGRAM

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10][10],i,j,m,n,s;
clrscr();
printf("Enter the order the order of matrix:");
scanf("%d,%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n The given matrix:\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",a[i][j]);
}
printf("\n");
}
s=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i= =j)
s=s+a[i][j];
}
}
printf("\n sum of the main diagonal element:%d",s);
getch();
}

Friday, 28 December 2012

COUNTING NUMBER OF VOWELS IN A STRING

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i, len, v;
char s[20];
clrscr();
printf("Enter a string: ");
scan f("%s",s);
len=strlen(s);
v=0;
for(i=0;i<len;i++)
{
if(s[i]= ='a'||s[i]= ='e'||s[i]= ='i'||s[i]= ='o'||s[i]= ='u')
v=v+1;
}
printf("Number of vowels in a string:%d",v);
getch();
}

FIBONACCI SERIES

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
int a=-1;
int b=1;
int c=0;
clrscr();
printf("Enter the number of terms to generate fibonacci series:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d\n",c);
}
getch();
}

Saturday, 22 December 2012

A C PROGRAM TO PRINT MULTIPLICATION TABLE


main()
{
int x,n,z;
for(x=1;x<=3;x++)
{
printf("\n Multiplication table for%d:",x);
for(n=1;n<=10;n++)
{
z=n*x;
printf("\n%d*%d=%d",x,n,z);
}
getch();
}
}

  • By above program you can print so many tables as your need.
  • In this program, x is multiplier
  •                            n is multiplicand
  •                            z is product.
  • It is the nested for loop program, the first for loop is used to determine the multipliers which the user requires.second for loop is used to determine the multiplicand.
  • z=n*z    in this statement the multiplicand and the multiplier are multiplied and then the value is assigned to the product(Z).

SQUARING AN INTEGER


main()
{
int i,square,;
clrscr();
printf("Number \t square(x)\n\n");
for(i=1;i<=8;i++)
printf("%d\t%d\n",i,i*i);
getch();
}