Pages

Thursday, 1 September 2011

if programs in c language



1- Write  a program to accept a number from the user then check the given number is even or odd?.
#include <stdio.h>
#include<conio.h>
void main()
{
      int n,r;
     clrscr();
printf(“\n Enter a number”);
scan(“%d”, &n);
r=n%2;
if (r==0)
 { 
  printf(“\n  %d” is even”, n);
}
else
printf(“\n  %d” is odd”, n);
getch();
}
2-Wap to accept two numbers from the user then print the greater number?
#include <stdio.h>
#include<conio.h>
void main()
{
      int a,b;
     clrscr();
printf(“\n Enter first number”);
scan(“%d”, &a);
printf(“\n Enter second number”);
scan(“%d”, &b);
if(a>b)
{
printf(“\n  %d” is greatest”, a);
}
else
printf(“\n  %d” is greatest”, b);
getch();
}
3- Wap to accept two numbers from the user then print the smaller number?
#include <stdio.h>
#include<conio.h>
void main()
{
      int a,b;
     clrscr();
printf(“\n Enter first number”);
scan(“%d”, &a);
printf(“\n Enter second number”);
scan(“%d”, &b);
if(a<b)
{
printf(“\n  %d” is smallest”, a);
}
else
printf(“\n  %d” is smallest”, b);
getch();
}
4- Wap to accept a roll number, three subject marks of a students and calculate total, average marks. If average marks is greater then 30 then print pass other wise print fail with roll no.l total, average marks of students.
#include <stdio.h>
#include<conio.h>
void main()
{
      int roll, m1,m2,m3;
     float total, avg;
     clrscr();
printf(“\n Enter Roll number”);
scan(“%d”, &roll);
printf(“\n Enter mark m1”);
scan(“%d”, &m1);
printf(“\n Enter mark m2”);
scan(“%d”, &m2);
printf(“\n Enter mark m3”);
scan(“%d”, &m3);
total=m1+m2+m3;
avg=total/3;
printf(“\n  roll number is %d”, roll);
printf(“\n  total marks is %f”, total);
printf(“\n  avarge marks is %f”, avg);
if(avg>30)
{
printf(“\n “pass”);
}
else
printf(“\n “fail”);
getch();
}
5-Wap to accept three number from user then print the greater number.
#include <stdio.h>
#include<conio.h>
void main()
{
      int a,b,c;
     clrscr();
printf(“\n Enter first number”);
scan(“%d”, &a);
printf(“\n Enter second number”);
scan(“%d”, &b);
printf(“\n Enter third number”);
scan(“%d”, &c);
if(a>b)
{
if(a>c)
{
printf(“\n  %d” is greatest”, a);
}
}
else
{
if(b>c)
{
printf(“\n  %d” is greatest”, b);
}
else
{
printf(“\n  %d” is greatest”, c);
}
}
getch();

}


6- Wap to accept code at basic salary tof employee if b is gretaer then 8000/- then calculate ta=40%, da=30%,of bs & HRA =1000 otherwise calculate ta=10% of bs, da=40% DA and HRA =500 then print employee code basic salary and gross salary of employee.
#include <stdio.h>
#include<conio.h>
void main()
{
      int  code, bs, ta, da, hra, gp;
     clrscr();
printf(“\n Enter code”);
scan(“%d”, &code);
printf(“\n Enter employee bs”);
scan(“%d”, &bs);
 if(bs>8000)
{
ta=bs* 4;
da= bs* 3;
hra=1000
}
else
{
ta=bs* 1;
da= bs* 4;
Hra=500
}
gp=bs+ta+da+hra;
printf(“code employee is  %a”, code);
printf(“basic salary employee is  %d”, bs);
printf(“gross pay %d”, gp);
getch();

}
7-  Wap to accept roll no and three subject marks of student then print roll no total marks, avg marks and grade of student if avg marks greater than equal to 75 then grade in A. if avg marks in greater than equal to 60 then print ‘B, if avg marks in greater than and equal to 45 and c 60 then print grade ‘c’ if avg marks is greater than equal to 30 then print grade D otherwise print grade E.
#include <stdio.h>
#include<conio.h>
void main()
{
      int roll, m1,m2,m3;
     float total, avg;
     clrscr();
printf(“\n Enter Roll number”);
scan(“%d”, &roll);
printf(“\n Enter mark m1”);
scan(“%d”, &m1);
printf(“\n Enter mark m2”);
scan(“%d”, &m2);
 printf(“\n Enter mark m3”);
scan(“%d”, &m3);
total=m1+m2+m3;
avg=total/30;
printf(“\n  roll number is %d”, roll);
printf(“\n  total marks is %f”, total);
printf(“\n  avarge marks is %f”, avg);
if(avg>=75)
{
printf(“grade ‘A’);
}
else
if(avg>=60)
{
printf(“grade ‘B’);
}
else
if(avg>=45)
{
printf(“grade ‘C’);
}
else
if(avg>=30)
{
printf(“grade ‘D’);
}
else
{
printf(“grade ‘E’);
}
getch();
}

 



Wednesday, 24 August 2011

For loop programs in c language



 1-
1
22
333
4444
55555

#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf(“%d”, i);
}
printf(“\n”);
}
getch();
}

2- 
1
12
123
1234
12345
#include <stdio.h>
#include<conio.h>
Void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf(“%d”, j);
}
printf(“\n”);
}
getch();
}

 3-
1
21
321
4321
54321
#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf(“%d”, (i+1)-j);
}
printf(“\n”);
}
getch();
}

4 - 
55555
4444
333
22
1

#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=(6-i); j++)
{
printf(“%d”, (6-i) );
}
printf(“\n”);
}
getch();
}

5 -
54321
4321
321
21
1

#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=(6-i); j>=1; j--)
{
printf(“%d”,j);
}
printf(“\n”);
}
getch();
}

6-
5
44
333
2222
11111
#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=5; i>=1; i--)
{
for (j=5; j>=i; j--)
{
printf(“%d”, i);
}
printf(“\n”);
}
getch();
}

7 -
5
54
543
5432
54321
#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=5; i>=1; i--)
{
for (j=5; j>=i; j--)
{
printf(“%d”, j);
}
printf(“\n”);
}
getch();
}

8 -

1
121
12321
1234321
123454321
#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j, k, l;
clrscr();

for (i=1; i<=5; i++)
{
for (k=1; k<=5-i; k++)
{
printf(“%d”, (“ “);
}
for (j=1; j<=i; j++)
{
printf(“%d”,  j);
}
for(i=i-1; i>=1; i--)
{
printf(“%d“, i);
}
printf(“\n”);
}
getch();
}

9 -
1
121
12321
1234321
123454321
1234321
12321
1

#include <stdio.h>
#include<conio.h>
Void main()
{
int  i, j, k, l;
clrscr();
for (i=1; i<=5; i++)
{
for (k=1; k<=5-i; k++)
{
printf(“%d”, (“ “);
}
for (j=1; j<=i; j++)
{
printf(“%d”, j);
}
for(i=i-1; i>=1; i--)
{
printf(“%d“, i);
}
printf(“\n”);
}
for (i=1; i<=4; i++)
{
for (j=1; j<=i; j++)
{
Printf(“%d”, (“ “);
}
for (j=1; j<=5-1; j++)
{
printf(“%d“, j);
}
j=j-2;
for ( ; j>=1; j++)
{
printf(“%d“, j);
}
printf(“\n”);
getch();
}     

10 -           
1234321
12321
121
1
121
12321
1234321


#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j, k, l;
clrscr();

for (i=1; i<=4; i++)
{
for (j=1; j<i; j++)
{
printf(“%d”, (“ “);
}
for (j=1; j<=5-i; j++)
{
printf(“%d”, j);
}
J=j-2;
for ( ; j<=1; j++)
{
printf(“%d“, j);
}
printf(“\n”);
}
for (i=1; i<=4; i++)
{
for (k=1; k<=4-i; k++)
{
printf(“%d”, (“ “);
}
for (j=1; j<=i; j++)
{
Printf(“%d“, j);
}
for (i=i-1 ; i>=1; i--)
{
printf(“%d“, i);
}
printf(“\n”);
getch();
}

11 -
*
**
***
****
*****

#include <stdio.h>
#include<conio.h>
Void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf(“*”);
}
}
getch();
}

12 -
*****
****
***
**
*

#include <stdio.h> 
#include<conio.h>
Void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=(6-i); j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}