Chapter 2 Programs
Chapter 2 Programs
*
***
*****
*******
*********
1 //to display 2 number is greater
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf(“Enter two numbers “);
scanf(“%d%d”,&a,&b);
if(a==b)
printf(“\n both are equal “);
else
if(a>b)
printf(“%d is larger number,a”);
else
printf(“%d is smaller number,b”);
getch();
}
2 //to display 2 number is greater using conditional operator ?:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf(“Enter two numbers “);
scanf(“%d%d”,&a,&b);
a>b?printf(“%d is larger number,a”): printf(“%d is smaller number,b”);
getch();
}
3 // a program using if-else ladder to find largest no. among three no.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter the value of three numbers”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b&&a>c)
{
printf(“A is greater no.”);
}
else
{
if(b>a&&b>c)
{
printf(“B is greater no.”);
}
else
{
printf(“C is greater no.”);
}
}
getch();
}
4 //to display number is odd or Even
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int x;
clrscr();
printf(“Enter the Number”);
scanf(“%d”,&x);
if(x%2 == 0)
print(“%d is even number”,x);
else
printf(“%d is odd number”,x);
getch();
}
5 // leap year
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int year;
clrscr();
printf(“Enter the year “);
scanf(“%d”,&year);
if(year%400==0&&year%100!=0||year%4==0)
printf(“ This year is Leap Year “);
else
printf(“ This is not leap year “);
getch();
return 0;
}
6 // Program to accept a number and find whether it is positive or negative.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“enter a number”);
scanf(“%d”,&n);
if(n==0)
{
printf(“the enter number of zero”);
}
else if(n>0)
{
printf(“the enter number is positive”);
}
else
{
printf(“the enter number is egative.”);
}
getch();
}
7 // program of arithmetic operation using switch case
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,choice;
printf(“ Enter first and second number”);
scanf(“%d%d”,&a,&b);
printf(“1 addition”);
printf(“2 subtraction”);
printf(“3 multiplication”);
printf(“4 division”);
printf(“\n Enter your choice “);
scanf(“%d”,&choice);
swtich(choice)
{
case 1:
c=a+b;
printf(“answer = %d”,c);
break;
case 2:
c=a-b;
printf(“answer = %d”,c);
break;
case 3:
c=a*b;
printf(“answer = %d”,c);
break;
case 4:
c=a/b;
printf(“answer = %d”,c);
break;
default:
printf(“\n invalid case”)
}
getch();
}
8 /**
* C program to check whether a character is vowel or consonant
*/
#include <stdio.h>
int main()
{
char ch;
/* Input character from user */
printf(“Enter any character: “);
scanf(“%c”, &ch);
return 0;
}
9 /**
* C program to calculate gross salary of an employee
*/
#include <stdio.h>
int main()
{
float basic, gross, da, hra;
return 0;
}
clrscr();
printf(“\n***************************************************************”);
printf(“\n S.H.H.J.B.Polytechnic “);
printf(“\n Neminath,Neminagar,Chandwad “);
printf(“\n****************************************************************
“);
printf(“\n Name of the student = %s”,n);
printf(“\n Roll number of student = %d”,roll);
printf(“\n PHYSICS = %d”,s1);
printf(“\n MATHS = %d”,s2);
printf(“\n C PROGRAM = %d”,s3);
printf(“\n\n TOTAL = %d”,total);
printf(“\n percentage = %f”,per);
printf(“\n grade= %s”,grad);
printf(“\n\n\n\t\t\t\t\t\t\n\n class teacher signature”);
getch();
return 0;
}
11 //multiplication table
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{ clrscr();
int i;
int x;
printf(“Enter the number “);
scanf(“%d”,&x);
for(i=1;i<=10;i++)
{
printf(“\n %d “,x*i);
}
getch();
}
12 //Write a program to calculate factorial of number.
#include<stdio.h>
#include<conio.h>
void main() {
int fact = 1,n,i;
clrscr();
printf("Enter a number");
scanf("%d",&n);
for(i = 1; i <= n; i++) {
fact = fact*i;
}
printf("%d",fact);
getch();
}
13 Write a program to find entered number is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,j,flag=0;
clrscr();
printf("Enter number:");
scanf(“%d”,&num);
for (j = 2; j <num; j++)
{
if (num % j == 0)
{
flag=1
break;
}
}
if (flag==1)
{
printf("%d is not prime number",num);
else
printf("%d is prime number",num);
}
}
getch();
}
14 // program of Fibonacci series
#include"stdio.h"
#include<conio.h>
#include<string.h>
int f3;
main()
{
clrscr();
int fib(int,int,int);
int f1=0,f2=1,f3;
printf("\t%d\t%d",f1,f2);
fib(f1,f2,5);
getch();
}
#include <stdio.h>
int main()
{
int i, n, sum=0;
return 0;
}
#include <stdio.h>
int main()
{
int n, num, rev = 0;
return 0;
}
20 Write a program to display Floyd‟s triangle as follow:
1
23
456
7 8 9 10
#include<stdio.h>
void main()
{
inti,j,k=1;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k);
k++;
}
printf("\n");
}
}
21 *
**
***
****
*****
/*
* C program to print right triangle star pattern series
*/
#include <stdio.h>
int main()
{
int i, j, n;
return 0;
}
#include <stdio.h>
int main()
{
int i, j, rows;
/* Print star */
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
return 0;
}
*
***
*****
*******
*********