A combination lock does not have any key. Setting digits on dials open it. Suppose in our combination lock we have three dials. We will write a program to take the three numbers from the user. The numbers will be compared with the preset digits of the dials. If match is found, the lock will be opened. Otherwise the user will be given chances .If the user fails to open the lock after four chances, the program will display the combination digits.
#include < stdio.h>
void main ()
{
int first, second, third, i;
clrscr ();
for (i=0;i<4;i++)
{
printf ("enter the three numbers: -");
scanf ("%d%d%d", &first, &second, &third);
if ((first==4)&&(second==2)&&(third==0))
{
printf ("Lock open\n");
break;
}
else
{
if (i!=3)
printf ("Try again\n");
}
}
if (i==4)
printf ("The combination is:%d%d%d", 4,2,0);
getch ();
}
The following exercise will take the weight and height in Kg. and ft. respectively of ten boys as input. Then the program will calculate the total number of boys with weight greater than 80 Kg. and height is less than 5 feet 4 inches. Program on for loop.
#include < stdio.h>
void main ()
{
int i, counter=0;
float wt, ht;
clrscr ();
printf ("Enter the weight in Kg.and height in ft. of '10' boys:-\n");
for (i=0;i<10;i++)
{
scanf ("%f%f", &wt, &ht);
if ((wt>80)&&(ht<5.4))
{
counter++;
}
}
printf ("The number of boys with weight greater than 60 Kgs\n");
printf ("and height less than 5 feet 6 inches are: %d", counter);
getch ();
}
Next is an example with for loop.If the entered value is less than 0, then the value will not be displayed.
#include
void main ()
{
int x, i=0;
clrscr ();
for (i=0;i<10;i++)
{
printf ("Enter a value:-\n");
scanf ("%d", &x);
if (x<0)
continue;
printf ("The value is: -%d\n", x);
}
getch ();
}
C program on magic number
Write program using for loop to display all the magic numbers less than 500.An integer is said to be a magic number when all the digits of the number are added till a single digit is obtained is 1.
#include < stdio.h>
void main()
{
int i,j=0;
clrscr ();
for (i=1;i<=500;i=i+9)
{
printf ("%d ", i);
j++;
if (j%10==0)
printf ("\n");
}
getch ();
}
program on prime number.
#include < stdio.h>
void main ()
{
int i, j;
clrscr ();
printf ("Enter the number:\n");
scanf ("%d", &i);
for (j=2;j<=i-1; j++)
{
if (i%j==0)
break;
}
if (j==i)
printf ("The number is prime number.\n");
else
printf ("The number is not a prime number.\n");
getch ();
}
Evaluate 12+32+52=....+(2n-1)2 using ‘for’ loop
#include < stdio.h>
#include < stdio.h>
void main ()
{
int i,n,sum=1,val=3,x;
clrscr ();
printf ("Enter the value of 'n': -");
scanf ("%d", &n);
for(i=1;i<=n-1;i++)
{
x=val*val;
sum=sum+x;
val=val+2;
}
printf ("The sum is:-%d",sum);
getch ();
}
Evaluate the following expression using for loop 22+42+62+....+2n2
#include < stdio.h>
void main ()
{
int i,n,sum=0,val=2,x;
clrscr ();
printf ("Enter the value of 'n': -");
scanf ("%d", &n);
for(i=0;i<=n-1;i++)
{
x=val*val;
sum=sum+x;
val=val+2;
}
printf ("The sum is:-%d",sum);
getch ();
}
Need Your Comments.
#include < stdio.h>
void main ()
{
int first, second, third, i;
clrscr ();
for (i=0;i<4;i++)
{
printf ("enter the three numbers: -");
scanf ("%d%d%d", &first, &second, &third);
if ((first==4)&&(second==2)&&(third==0))
{
printf ("Lock open\n");
break;
}
else
{
if (i!=3)
printf ("Try again\n");
}
}
if (i==4)
printf ("The combination is:%d%d%d", 4,2,0);
getch ();
}
The following exercise will take the weight and height in Kg. and ft. respectively of ten boys as input. Then the program will calculate the total number of boys with weight greater than 80 Kg. and height is less than 5 feet 4 inches. Program on for loop.
#include < stdio.h>
void main ()
{
int i, counter=0;
float wt, ht;
clrscr ();
printf ("Enter the weight in Kg.and height in ft. of '10' boys:-\n");
for (i=0;i<10;i++)
{
scanf ("%f%f", &wt, &ht);
if ((wt>80)&&(ht<5.4))
{
counter++;
}
}
printf ("The number of boys with weight greater than 60 Kgs\n");
printf ("and height less than 5 feet 6 inches are: %d", counter);
getch ();
}
Next is an example with for loop.If the entered value is less than 0, then the value will not be displayed.
#include
void main ()
{
int x, i=0;
clrscr ();
for (i=0;i<10;i++)
{
printf ("Enter a value:-\n");
scanf ("%d", &x);
if (x<0)
continue;
printf ("The value is: -%d\n", x);
}
getch ();
}
Write program using for loop to display all the magic numbers less than 500.An integer is said to be a magic number when all the digits of the number are added till a single digit is obtained is 1.
#include < stdio.h>
void main()
{
int i,j=0;
clrscr ();
for (i=1;i<=500;i=i+9)
{
printf ("%d ", i);
j++;
if (j%10==0)
printf ("\n");
}
getch ();
}
program on prime number.
#include < stdio.h>
void main ()
{
int i, j;
clrscr ();
printf ("Enter the number:\n");
scanf ("%d", &i);
for (j=2;j<=i-1; j++)
{
if (i%j==0)
break;
}
if (j==i)
printf ("The number is prime number.\n");
else
printf ("The number is not a prime number.\n");
getch ();
}
Evaluate 12+32+52=....+(2n-1)2 using ‘for’ loop
#include < stdio.h>
void main ()
{
int i,n,sum=1,val=3,x;
clrscr ();
printf ("Enter the value of 'n': -");
scanf ("%d", &n);
for(i=1;i<=n-1;i++)
{
x=val*val;
sum=sum+x;
val=val+2;
}
printf ("The sum is:-%d",sum);
getch ();
}
Evaluate the following expression using for loop 22+42+62+....+2n2
#include < stdio.h>
void main ()
{
int i,n,sum=0,val=2,x;
clrscr ();
printf ("Enter the value of 'n': -");
scanf ("%d", &n);
for(i=0;i<=n-1;i++)
{
x=val*val;
sum=sum+x;
val=val+2;
}
printf ("The sum is:-%d",sum);
getch ();
}
Need Your Comments.
No comments:
Post a Comment