0% found this document useful (0 votes)
6 views

Chapter 2 Programs

The document contains a list of programming tasks and C code examples for various basic operations, including comparisons, arithmetic operations, and string manipulations. It covers topics such as finding the largest number, checking for odd/even, calculating gross salary, and generating patterns like Fibonacci series and Floyd's triangle. Each task is accompanied by a code snippet demonstrating the implementation in C.

Uploaded by

patelifat48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter 2 Programs

The document contains a list of programming tasks and C code examples for various basic operations, including comparisons, arithmetic operations, and string manipulations. It covers topics such as finding the largest number, checking for odd/even, calculating gross salary, and generating patterns like Fibonacci series and Floyd's triangle. Each task is accompanied by a code snippet demonstrating the implementation in C.

Uploaded by

patelifat48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1. Program to accept two numbers and find whether they are equal or which is larger.

2. Program to find larger number using conditional operator.


3. Program to accept three numbers and find which is larger.
4. Program to accept a number and find whether it is odd or even.
5. Program for leap year
6. Program to accept a number and find whether it is positive or negative.
7. Program using switch case to perform arithmetic operations.
8. Program to accept a string from keyboard and count and display the number of vowels
present in the string.
9. C program to calculate gross salary of an employee
10. program using relational operator and nested if else for following criteria
Per > 75 - distinction
Per >=60 and per <= 74 - first class
Per >= 40 and per <= 49 – third class
Below 40 – fail
11. Program for multiplication table
12. Program using while loop to find the factorial of a number.
13. program to find entered number is prime or not
14. Program to display Fibonacci series upto n number.
Or
Program to print Fibonacci series.
1 1 2 3 5 8
15. Program to find sum of following series.
1+2+3+….10
16. Program using do-while loop
17. Program which will count number of digit in entered number.
18. Program to reverse entered number.
19. Program to read a string from keyboard and find whether it is palindrome or not.
20. Write a program to display Floyd‟s triangle as follow:
1
2 3
4 5 6
7 8 9 10
21. C program to print right triangle star pattern series
*
**
***
****
*****
22. program to print equilateral triangle or pyramid star pattern

*
***
*****
*******
*********
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);

/* Condition for vowel */


if(ch==‟a‟ || ch==‟e‟ || ch==‟i‟ || ch==‟o‟ || ch==‟u‟ ||
ch==‟A‟ || ch==‟E‟ || ch==‟I‟ || ch==‟O‟ || ch==‟U‟)
{
printf(“‟%c‟ is Vowel.”, ch);
}
else if((ch >= „a‟ && ch <= „z‟) || (ch >= „A‟ && ch <= „Z‟))
{
/* Condition for consonant */
printf(“‟%c‟ is Consonant.”, ch);
}
else
{
/*
* If it is neither vowel nor consonant
* then it is not an alphabet.
*/
printf(“‟%c‟ is not an alphabet.”, ch);
}

return 0;
}

9 /**
* C program to calculate gross salary of an employee
*/

#include <stdio.h>

int main()
{
float basic, gross, da, hra;

/* Input basic salary of employee */


printf(“Enter basic salary of an employee: “);
scanf(“%f”, &basic);

/* Calculate D.A and H.R.A according to specified conditions */


if(basic <= 10000)
{
da = basic * 0.8;
hra = basic * 0.2;
}
else if(basic <= 20000)
{
da = basic * 0.9;
hra = basic * 0.25;
}
else
{
da = basic * 0.95;
hra = basic * 0.3;
}

/* Calculate gross salary */


gross = basic + hra + da;

printf(“GROSS SALARY OF EMPLOYEE = %.2f”, gross);

return 0;
}

10 //display the marksheet


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
main()
{
char n[25],grad[25];
int roll,s1,s2,s3,total;
float per;
clrscr();

printf(“\n Enter the Name of the student “);


scanf(“%s”,n);
printf(“\n Enter the roll number of student “);
scanf(“%d”,&roll);
printf(“\nEnter the marks of first subject “);
scanf(“%d”,&s1);
printf(“\nEnter the marks of the second subject “);
scanf(“%d”,&s2);
printf(“\nEnter the marks of the third subject “);
scanf(“%d”,&s3);
total =s1+s2+s3;
per = total/3;
if(per>=85)
strcpy(grad,”Distinction “);
else
if(per>=65 && per<85)
strcpy(grad,”First class “);
else
if(per>=50 && per<=65)
strcpy(grad,” Second class “);
else
if(per>=35 && per<=50)
strcpy(grad,” Pass class “);
else
strcpy(grad,” Fail “);

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();
}

int fib(int f1,int f2,int n)


{
if(n==0)
return n;
else
f3=f1+f2;
printf("\t%d",f3);
fib(f2,f3,n-1);
}
15 //C program to find sum of natural numbers between 1 to n

#include <stdio.h>

int main()
{
int i, n, sum=0;

/* Input upper limit from user */


printf("Enter upper limit: ");
scanf("%d", &n);

/* Find sum of all numbers */


for(i=1; i<=n; i++)
{
sum =sum + i;
}

printf("Sum of first %d natural numbers = %d", n, sum);

return 0;
}

16 // to display 1-10 using do-while loop


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=1;
do
{
printf("%d\t",n);
n++;
}while(n<=10);
getch();
}
17 // to count number of digit in a number
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int num;
int count=0;
clrscr();
printf("Enter the number");
scanf("%d",&num);
while(num!=0)
{
count++;
num/=10;
}
printf(" Number of digits %d",count);
getch();
}
18 Write a program to reverse given integer number (input = 4567
reverse is 7654).
#include<stdio.h>
#include<conio.h>
void main()
{
int no,sum=0,rem;
printf(“Enter the number :”);
scanf(“%d”,&no);
while(no>0)
{
rem=no%10;
no=no/10;
sum=sum*10+rem;
}
printf(“\n sum=%d”,sum);
getch();
}
19 /**
* C program to check whether a number is palindrome or not
*/

#include <stdio.h>

int main()
{
int n, num, rev = 0;

/* Input a number from user */


printf("Enter any number to check palindrome: ");
scanf("%d", &n);

/* Copy original value to 'num' to 'n'*/


num = n;

/* Find reverse of n and store in rev */


while(n != 0)
{
rev = (rev * 10) + (n % 10);
n /= 10;
}

/* Check if reverse is equal to 'num' or not */


if(rev == num)
{
printf("%d is palindrome.", num);
}
else
{
printf("%d is not palindrome.", num);
}

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;

/* Input number of rows from user */


printf("Enter value of n: ");
scanf("%d", &n);

for(i=1; i<=n; i++)


{
/* Print i number of stars */
for(j=1; j<=i; j++)
{
printf("*");
}

/* Move to next line */


printf("\n");
}

return 0;
}

22 //C program to print equilateral triangle or pyramid star pattern

#include <stdio.h>

int main()
{
int i, j, rows;

/* Input number of rows to print */


printf("Enter number of rows : ");
scanf("%d", &rows);
/* Iterate through rows */
for(i=1; i<=rows; i++)
{
/* Print leading spaces */
for(j=i; j<rows; j++)
{
printf(" ");
}

/* Print star */
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}

/* Move to next line */


printf("\n");
}

return 0;
}
*
***
*****
*******
*********

You might also like