Sunday 26 June 2016

SIMPLE LOOPING programs

1. WAP in c to print hello ten times.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("hello\n");
}
getch();
}

Output:










2. WAP in c to print natural numbers up to 10.

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

getch();
}

Output:














3. WAP in c to print sum of natural numbers up to 10.

S=1+2+3+4+5+6+7+8+9+10

#include<stdio.h>
#include<conio.h>
void main()
{
int i,s=0;
clrscr();
for(i=1;i<=10;i++)
{
s=s+i;
}
printf("sum=%d",s);
getch();
}

Output:







4. WAP in c to print sum of square of natural numbers from 1 to 10.

S=12 +22+3 2+4 2 +5 2 +6 2 +7 2 +8 2 +9 2 +10 2


#include<stdio.h>
#include<conio.h>
void main()
{
int i,s=0;
clrscr();
for(i=1;i<=10;i++)
{
s=s+i*i;
}
printf("sum=%d",s);
getch();
}

Output:






5. WAP in c to print sum of cube of natural numbers up to 10.

S=13 +23+3 3+4 3 +5 3 +6 3 +7 3 +8 3 +9 3+10 3

#include<stdio.h>
#include<conio.h>
void main()
{
int i,s=0;
clrscr();
for(i=1;i<=10;i++)
{
s=s+i*i*i;
}
printf("sum=%d",s);
getch();
}

Output:









6. WAP in c to print all even numbers up to 100.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=100;i++)
{
if(i%2==0)
{
printf("%d\t",i);
}
}
getch();
}

0utput:




7. WAP in c to print all odd numbers up to 100.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=100;i++)
{
if(i%2==1)
{
printf("%d\t",i);
}
}
getch();
}

Output:







8. WAP in c to print sum of all even numbers up to 10.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,s=0;
clrscr();
for(i=1;i<=10;i++)
{
if(i%2==0)
{
s=s+i;
}
}
printf("sum=%d",s);
getch();
}

Output:






9. WAP in c to print sum of all odd numbers up to 10.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,s=0;
clrscr();
for(i=1;i<=10;i++)
{
if(i%2==1)
{
s=s+i;
}
}
printf("sum=%d",s);
getch();
}

Output:







10. WAP in c to print multiplication table of given number.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,s;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
printf("Multiplication table of %d is:\n",n);
for(i=1;i<=10;i++)
{
s=n*i;
printf("%d*%d=%d\n",s,n,i);
}
getch();
}

Output:














11. WAP in c to print product of natural numbers upto 10.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
long int s=1;
clrscr();
for(i=1;i<=10;i++)
{
s=s*i;
}
printf("product = %ld",s);
getch();
}

Output:






12. WAP in c to print product of square of natural numbers upto 5.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
long int s=1;
clrscr();
for(i=1;i<=5;i++)
{
s=s*i*i;
}
printf("product = %ld",s);
getch();
}

Output:






13. WAP in c to print product of cube of first five natural number.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
long int s=1;
clrscr();
for(i=1;i<=5;i++)
{
s=s*i*i*i;
}
printf("product = %lu",s);
getch();
}

Output:






14. WAP in c to print odd and even numbers in two columns upto 10.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
printf("ODD\tEVEN");
for(i=1;i<=10;i++)
{
if(i%2==0)
{
printf("\t %d",i);
}
else
{
printf("\n %d",i);
}
}
getch();
}


Output:



SWITCH CASE programming

1. WAP in c to find addition,subtraction, multiplication and division of two numbers using switch.

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
float a,b,c;
clrscr();
printf("\n1. Add");
printf("\n2. Subtract");
printf("\n3. Multiply");
printf("\n4. Divide");
printf("\nEnter your choice:");
scanf("%d",&x);
printf("\nEnter two numbers:\n");
scanf("%f%f",&a,&b);
switch(x)
{
case 1:c=a+b;
break;
case 2:c=a-b;
break;
case 3:c=a*b;
break;
case 4:c=a/b;
break;
default:
printf("\nwrong choice");
}
printf("result=%f",c);
getch();
}

Output:







2. WAP in c to find month name if month number is given.

#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
float a,b,c;
clrscr();
printf("\nEnter any month number");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nJanuary");
break;
case 2:printf("\nFebruary");
break;
case 3:printf("\nMarch");
break;
case 4:printf("\nApril");
break;
case 5:printf("\nMay");
break;
case 6:printf("\nJune");
break;
case 7:printf("\nJuly");
break;
case 8:printf("\nAugust");
break;
case 9:printf("\nSeptember");
break;
case 10:printf("\nOctober");
break;
case 11:printf("\nNovember");
break;
case 12:printf("\nDecember");
break;
default:printf("\nwrong choice");
}
getch();
}

Output:






3. WAP in c to find number of days in a month if month number is given using switch (assume feb has 28 days).

#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
clrscr();
printf("\nEnter any month number");
scanf("%d",&ch);
switch(ch)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("\n31 days");
break;
case 4:
case 6:
case 9:
case 11:printf("\n30 days");
break;
case 2:printf("\n28 days");
break;
default:printf("\nwrong choice");
}
getch();
}

Output:





4. WAP in c to check whether a given character is vowel or consonant.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\nEnter any character:");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':printf("\nvowel");
break;
default:printf("\nconsonent");
}
getch();
}

Output:

Friday 24 June 2016

IF ELSE programming


1. WAP in c to print "hello" if given number is positive otherwise print "bye"

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("enter any number:");
scanf("%d",&x);
if(x>0)
{
printf("\nhello");
}
else
{
printf("\nbye");
}
getch();
}

output:












2. WAP in c to accept 5 subject marks from user calculate and print sum and percentage obtained also check for pass or fail.

#include<stdio.h>
#include<conio.h>
void main()
{
float s1,s2,s3,s4,s5,t,p;
clrscr();
printf("enter five subject marks:\n");
scanf("%f%f%f%f%f",&s1,&s2,&s3,&s4,&s5);
t=s1+s2+s3+s4+s5;
printf("marks obtained=%f\n",t);
p=t*100/500;
printf("percentage=%f\n",p);
if(p>=33)
{
printf("pass");
}
else
{
printf("fail");
}
getch();
}

output:










3. WAP in c to check whether a given number is even or odd.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter any number:");
scanf("%d",&num);
if(num%2==0)
{
printf("even");
}
else
{
printf("odd");
}
getch();
}

output:








4. WAP in c to calculate total salary according to given condition
/*
if basic salary >=10000
then TA =40% of basic salary
DA =60% of basic salary
HRA=50% of basic salary
if basic salary <10000
then TA =70% of basic salary
DA =80% of basic salary
HRA=5000
total salary=basic salary+TA+DA+HRA
*/

#include<stdio.h>
#include<conio.h>
void main()
{
float bs,ta,da,hra,ts;
clrscr();
printf("Enter basic salary:");
scanf("%f",&bs);
if(bs>=10000)
{
ta=bs*40/100;
da=bs*60/100;
hra=bs*50/100;
}
else
{
ta=bs*70/100;
da=bs*80/100;
hra=5000;
}
ts=bs+ta+da+hra;
printf("\nTA=%f",ta);
printf("\nDA=%f",da);
printf("\nHRA=%f",hra);
printf("\ntotal salary=%f",ts);
getch();
}

output:







5. WAP in c to print greater number between two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter two numbers:\n");
scanf("%d%d",&x,&y);
if(x>y)
{
printf("%d is greater",x);
}
else
{
printf("%d is greater",y);
}
getch();
}

output:







6.WAP in c to find greatest number between three numbers:

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("Enter three numbers:\n");
scanf("%d%d%d",&x,&y,&z);
if(x>y)
{
if(x>z)
{
printf("\n\t%d is greater",x);
}
else
{
printf("\n\t%d is greater",z);
}
}
else
{
if(y>z)
{
printf("\n\t%d is greater",y);
}
else
{
printf("\n\t%d is greater",z);
}
}
getch();
}


output:








7. WAP in c to check eligbility criteria for voting in college using nested if else. The condition is that you must be student of college and your age should be between 18 to 25 (both including).

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char ch;
clrscr();
printf("Enter your age:");
scanf("%d",&age);
printf("\nAre you student of college(Y or N):");
ch=getche();
if(age>=18)
{
if(age<=25)
{
if(ch=='Y')
{
printf("\nEligible to vote");
}
else
{
if(ch=='y')
{
printf("\nEligible to vote");
}
else
{
printf("\nnot Eligible to vote");
}
}
}
else
{
printf("\nnot Eligible to vote");
}
}
else
{
printf("\nnot Eligible to vote");
}
getch();
}

output:







8. WAP in c to check eligibility criteria for voting in college using logical operators. The condition is that you must be student of college and your age should be between 18 to 22.

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char ch;
clrscr();
printf("Enter your age:\n");
scanf("%d",&age);
printf("\nAre you student of college(Y or N):\n");
ch=getche();
if(age>=18 && age<=25 && (ch=='Y'|| ch=='y'))
{
printf("\n Eligible to vote");
}
else
{
printf("\n not Eligible to vote");
}
getch();
}

output:








9. WAP in c to accept a character from user and print whether it is vowel or not.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter any character:");
ch=getche();
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
    ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' )
{
printf("\nVowel");
}
else
{
printf("\nnot a Vowel");
}
getch();
}

output:






10. WAP in c to accept 5 subject marks from user calculate and print sum and percentage obtained also print grade according to condition -
00<=p<33 print "F grade"
33<=p<50 print "D grade"
50<=p<60 print "C grade"
60<=p<75 print "B grade"
75<=p<90 print "A grade"
90<=p<=100 print "A+ grade"
also print "PASS" or "FAIL". 


#include<stdio.h>
#include<conio.h>
void main()
{
float s1,s2,s3,s4,s5,t,p;
clrscr();
printf("enter five subject marks:\n");
scanf("%f%f%f%f%f",&s1,&s2,&s3,&s4,&s5);
t=s1+s2+s3+s4+s5;
printf("marks obtained=%f\n",t);
p=t*100/500;
printf("percentage=%f\n",p);
if(p>=33)
{
printf("pass");
}

if(p>=90)
{
printf("\nA+ grade");
}
else
{
if(p>=75)
{
printf("\nA grade");
}
else
{
if(p>=60)
{
printf("B grade");
}
else
{
if(p>=50)
{
printf("\nC grade");
}
else
{
if(p>=40)
{
printf("\nD grade");
}
else
{
if(p>=33)
{
printf("\nE grade");
}
else
{
printf("\nFail \nF grade");
}
}
}
}
}
}
getch();
}

output:









11. WAP in c to accept distance from user and calculate total fare according to the condition-
for 0<d<=3 - rs 50
for 3<d<=10 - rs 15/km
for 10<d<=25 - rs 13/km
for 25<d<=30 - rs 10/km
above 30 km - service not available


#include<stdio.h>
#include<conio.h>
void main()
{
int dis=0,fare=0;
clrscr();
printf("Enter distace to be travelled:");
scanf("%d",&dis);
if(dis<=30)
{
if(dis>25)
{
fare=50+7*15+15*13+(dis-25)*10;
}
else
{
if(dis>10)
{
fare=50+7*15+(dis-10)*13;
}
else
{
if(dis>3)
{
fare=50+(dis-3)*15;
}
else
{
if(dis>0)
{
fare=50;
}
else
{
printf("\nService not available");
}
}
}
}
}
else
{
printf("\nService not available");
}
printf("\nTotal fare= Rs %d",fare);
getch();
}


output:





12. WAP in c to accept number of units consumed by user and calculate total charge accoding to the condition-
for 0<u<=100 - rs 500
for 100<u<=300 - rs 8/unit
for 300<u<=500 - rs 10/km
for above 500 - rs 15/km

#include<stdio.h>
#include<conio.h>
void main()
{
long unsigned int u,bill;
clrscr();
printf("Enter units consumed:");
scanf("%lu",&u);
if(u>500)
{
bill=500+200*8+200*10+(u-500)*15;
}
else
{
if(u>300)
{
bill=500+200*8+(u-300)*10;
}
else
{
if(u>100)
{
bill=500+(u-100)*8;
}
else
{
bill=500;
}
}
}
printf("\n Bill payable= Rs%lu",bill);
getch();
}


output:







13. A company promotes its employees in the following cases:
- If the employee is married.
- If the employee is unmarried, male & above 3 years of experience.
- If the employee is unmarried, female & above 2 years of experience.
In all other cases the employee is not promoted. If the marital status, sex and experience of the employee are the inputs, write a program to determine whether the employee is to be promoted or not (without using logical operators).

#include<stdio.h>
#include<conio.h>
void main()
{
int exp;
char gen,ms;
clrscr();
printf("\nEnter marital status(married-m or unmarried-u):");
scanf("%c",&ms);
if(ms=='m')
{
printf("\nPromoted");
}
else
{
printf("\nEnter your gender (m or f):");
gen=getche();
printf("\nEnter your experience:");
scanf("%d",&exp);
if(gen=='m')
{
if(exp>=3)
{
printf("\nPromoted");
}
else
{
printf("\nNot Promoted");
}
}
else
{
if(exp>=2)
{
printf("\nPromoted");
}
else
{
printf("\nNot Promoted");
}
}
}
getch();
}

Output :









14. A company promotes its employees in the following cases:
  • If the employee is married.
  • If the employee is unmarried, male & above 3 years of experience.
  • If the employee is unmarried, female & above 2 years of experience.

In all other cases the employee is not promoted. If the marital status, sex and experience of the employee are the inputs, write a program to determine whether the employee is to be promoted or not (using logical operators).


#include<stdio.h>
#include<conio.h>
void main()
{
int exp;
char gen,ms;
clrscr();
printf("\nEnter marital status:(married-m or unmarried-u):");
scanf("%c",&ms);
printf("\nEnter your gender (m or f):");
gen=getche();
printf("\nEnter your experience:");
scanf("%d",&exp);
if(ms=='m'||(ms=='u'&&gen=='m'&&exp>=3)|| (ms=='u'&&gen=='f'&&exp>=2) )
{
printf("\nPromoted");
}
else
{
printf("\nNot Promoted");
}

getch();
}


Output :









15. WAP in c to check whether a given character is in lower case or not 

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\nEnter any character:");
scanf("%c",&ch);
if(ch>='a' && ch<='z')
{
printf("\ngiven character is in lower case");
}
else
{
printf("\ngiven character is not in lower case");
}
getch()
}


output:










16. Program to calculate salary of an employee according to given condition

Gender        Service years      qualification        Salary
----------      ----------------       ---------------        --------
Male            >= 10                   Post-Graduate      15000
                     >= 10                  Graduate              10000
                     <=10                   Post-Graduate      10000
                     < 10                     Graduate                7000
Female          >= 10                  Post-Graduate      12000
                      >= 10                  Graduate                9000
                      < 10                     Post-Graduate     10000
                      < 10                     Graduate               6000




#include<stdio.h>
#include<conio.h>
void main( )
{
char g ;
int y, q, s ;
clrscr();
printf("Enter Gender,Years of Service and qualifications(0=G,1=PG ):\n") ;
scanf("%c%d%d", &g, &y, &q ) ;

if ( (g == 'm'|| g == 'M') && y >= 10 && q == 1 )
s = 15000 ;
else if ( ( (g == 'm'|| g == 'M') && y >= 10 && q == 0 )
||( (g == 'm'|| g == 'M') && y < 10 && q == 1 ) )
s = 10000 ;
else if ( (g == 'm'|| g == 'M') && y < 10 && q == 0 )
s = 7000 ;
else if ( (g == 'f'|| g == 'F') && y >= 10 && q == 1 )
s = 12000 ;
else if ( (g == 'f'|| g == 'F') && y >= 10 && q == 0 )
s = 9000 ;
else if ( (g == 'f'|| g == 'F') && y < 10 && q == 1 )
s = 10000 ;
else if ( (g == 'f'|| g == 'F')&& y < 10 && q == 0 )
s = 6000 ;

printf("\nSalary of Employee = %d", s) ;
getch();
}

Output:








17. A certain grade of steel is graded according to the following conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600

The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met

Write a program, which will require the user to give values of hardness,carbon content and tensile strength of the steel under consideration and output the grade of the steel. 


#include<stdio.h>
#include<conio.h>
void main()
{
int h,t;
float c;
clrscr();
printf("Instructions:\n(i) Hardness must be greater than 50\n(ii) Carbon content must be less than 0.7\n(iii) Tensile strength must be greater than 5600\n");
printf("Enter Hardness, cabon content and tensile strength of steel\n");
scanf("%d%f%d",&h,&c,&t);
if(h>50 && c<0.7 && t>5600)
{
printf("Grade 10");
}
else
{
if(h>50 && c<0.7)
{
printf("Grade 9");
}
else
{
if(c<0.7 && t>5600)
{
printf("Grade 8");
}
else
{
if(h>50 && t>5600)
{
printf("Grade 7");
}
else
{
if(h>50 || c<0.7 || t>5600)
{
printf("Grade 6");
}
else
{
printf("Grade 5");
}
}
}
}
}
getch();
}

Output:
















index

MENU Sample Papers  (new sample paper added for computers) C Language Mathematics IX (NCERT solution)   Computer IX (CBSE) ...