C Programming: Mastering Arithmetic Operations, Grades, and Array Manipulation, Lecture notes of Computer science

A comprehensive collection of c programming exercises and solutions, covering a wide range of topics such as arithmetic operations, grade calculation, number classification, function usage, array manipulation, and string handling. The programs demonstrate the application of control structures, functions, and various built-in string functions in c. The document serves as a valuable resource for students and developers looking to enhance their c programming skills, as it covers fundamental concepts and practical implementations. The programs are well-structured, with clear explanations and sample outputs, making it an excellent learning tool for both beginners and experienced programmers. By studying this document, users can gain a deeper understanding of c programming principles, problem-solving techniques, and the versatility of the language in handling diverse programming tasks.

Typology: Lecture notes

2023/2024

Uploaded on 06/12/2024

baaaaaa-aaaaaa
baaaaaa-aaaaaa ๐Ÿ‡ฎ๐Ÿ‡ณ

1 document

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Write a C program to obtain the percentage and convert them into grades
using else-if ladder statement.
/* C Program to classify the grades based on percentage obtained and print the
same */
#include<stdio.h>
main()
{
float percentage;
printf("Enter the score of a student:\n");
scanf("%f",&percentage);
if(percentage>100)
printf("Invalid Score");
else if(percentage>90)
printf("S Grade");
else if(percentage>80)
printf("A Grade");
else if(percentage>70)
printf("B Grade");
else if(percentage>60)
printf("C Grade");
else if(percentage>50)
printf("D Grade");
else if(percentage>=40)
printf("E Grade");
else
printf("Fail");
}
Output:
Case 1: Enter the score of a student:
95.5
S Grade
Case 2: Enter the score of a student:
102.7
Invalid Score
Case 3: Enter the score of a student:
34.9
Fail
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download C Programming: Mastering Arithmetic Operations, Grades, and Array Manipulation and more Lecture notes Computer science in PDF only on Docsity!

  1. Write a C program to obtain the percentage and convert them into grades using else-if ladder statement.

/* C Program to classify the grades based on percentage obtained and print the same */ #include<stdio.h> main() { float percentage; printf("Enter the score of a student:\n"); scanf("%f",&percentage); if(percentage>100) printf("Invalid Score"); else if(percentage>90) printf("S Grade"); else if(percentage>80) printf("A Grade"); else if(percentage>70) printf("B Grade"); else if(percentage>60) printf("C Grade"); else if(percentage>50) printf("D Grade"); else if(percentage>=40) printf("E Grade"); else printf("Fail"); } Output:

Case 1: Enter the score of a student:

S Grade Case 2: Enter the score of a student:

Invalid Score Case 3: Enter the score of a student:

Fail

  1. Write a Menu based C program to implement all the arithmetic operations.

/Program to perform all the arithmetic operations using switch statement where the case values are characters/ #include<stdio.h> main() { char ch; int num1,num2; printf(โ€œ+. Addition\n-:Subtraction\n:Multiplication\n/:Division\nโ€); printf("Enter the two numbers to perform any arithmetic operation\n"); scanf("%d%d",&num1,&num2); printf(โ€œ+. Addition\n-:Subtraction\n:Multiplication\n/:Division\nโ€); printf("Enter a character value\n"); scanf(" %c",&ch); switch(ch) { case '+': printf("The addition result is %d \n",num1+num2); break; case '-': printf("The subtraction result is %d\n",num1-num2); break; case '': printf("The multiplication result is %d\n", num1 num2); break; case '/': if(num2==0) printf("Cannot divide a number by zero"); else { printf("The division result is %f\n",(float)num1/num2); } break; default: printf("Invalid choice"); } }

  1. Write a C program to swap two numbers using a function.

/C program to swap two numbers using functions/ #include<stdio.h> void swap(int,int); main() { int n1, n2; printf("Enter first number:\n"); scanf("%d",&n1); printf("Enter second number:\n"); scanf("%d",&n2); printf("The numbers before exchanging are:\n"); printf("First number : %d\nSecond number : %d\n",n1,n2); swap(n1,n2); }

void swap(int num1,int num2) { num1=num1+num2; num2=num1-num2; num1=num1-num2; printf("The numbers after exchanging are:\n"); printf("First number : %d\nSecond number : %d\n",num1,num2); }

  1. Write a program in C to print the first N Fibonacci sequence.

/*C program to find first N fibonacci sequence */ #include<stdio.h> void fib(int,int,int); main() { int f1=0,f2=1,n; printf("Enter the limit:\n"); scanf("%d",&n); fib(f1,f2,n); }

void fib(int f1,int f2,int n) { int f3,i; printf("The first %d Fibonacci Sequence is:\n",n); printf("%d\n%d\n",f1,f2); i=3; while(i<=n) { f3=f1+f2; f1=f2; f2=f3; printf("%d\n",f3); i++; } }

  1. Write a C program to print the square of array elements.

/Write a C program to print the square of array elements/ #include<stdio.h> void square(int a[],int n) { int square,i; for(i=0;i<n;i++) { square=a[i]*a[i]; printf("The square of %d = %d\n",a[i],square); } } main() { int a[20],n,i; printf("Enter the array limit:\n"); scanf("%d",&n); printf("Enter array elements:\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } square(a,n);

}

  1. Write a menu-based program to calculate the areas of different Geometrical figures implementing functions.

/Write a menu-based program to calculate the areas of different geometrical figures implementing functions/ #include<stdio.h> #define pi 3. float circle(float radius) { float area=piradiusradius; return area; } float rectangle(float length,float breadth) { float area=lengthbreadth; return area; } float triangle(float base,float heigth) { float area=0.5baseheigth; return area; } float square(float side) { float area=sideside; return area; } main() { int ch; float radius,length,breadth,base,height,side; printf("******MENU******\n"); printf("1.Area of Circle\n2.Area of Rectangle\n3.Area of triagnle\n4.Area of Square\n"); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1:printf("Enter radius of a circle\n"); scanf("%f",&radius); float res1=circle(radius); printf("The area of circle is : %.2f",res1); break;

  1. Program to find the greater of two numbers using functions in C.

/Program to find the number is positive or negative using functions in C/ #include<stdio.h> int greatest(int,int); main() { int n1,n2,res; printf("Enter first number\n"); scanf("%d",&n1); printf("Enter second number\n"); scanf("%d",&n2); res=greatest(n1,n2); if(res==1) printf("%d is greater than %d",n1,n2); else printf("%d is greater than %d",n2,n1); } int greatest(int n1,int n2) { if(n1>n2) return 1; else return 0; }

10.Write a Menu based C Program to find the biggest and smallest element in an array /Write a Menu based C Program to find the biggest and smallest element in an array/ #include<stdio.h> main() { int a[20],i,n,ch,min,max; printf("Enter the limit\n"); scanf("%d",&n); printf("Enter the elements of an array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf(โ€œMENU\nโ€); printf(โ€œ1.Miniimum in an array\n2.Maximum in an array\nโ€); printf("Enter the choice\n"); scanf("%d",&ch); switch(ch) { case 1:min=a[0]; for(i=0;i<n;i++) { if(a[i]<min) { min=a[i]; } } printf("The smallest element in an array is %d",min); break; case 2:max=a[0]; for(i=0;i<n;i++) { if(a[i]>max) { max=a[i]; } } printf("The largest element in an array is %d",max); break; default:printf("Invalid Choice"); } }

  1. Write a C Program to demonstrate string built in functions in C.

/Write C Program to demonstrate String built in functions in C/ #include<stdio.h> #include<string.h> main() { char str1[20],str2[20]; int count; printf("Enter the first string\n"); scanf("%s",str1); count=strlen(str1); printf("The length of a string %s is %d\n",str1,count); strupr(str1); printf("The string in uppercase is %s\n",str1); strlwr(str1); printf("The string in lowercase is %s\n",str1); strcpy(str2,str1); printf("The copied string is %s\n",str2); }

13.Write C Program to demonstrate String built in functions in C.

/Write C Program to demonstrate String built in functions in C/ #include<stdio.h> #include<string.h> main() { char str1[20],str2[20]; int res; printf("Enter the first string\n"); scanf("%s",str1); printf("Enter the first string\n"); scanf("%s",str2); res=strcmp(str1,str2); printf("The result is %d\n",res); if(res==0) printf("The strings are equal\n"); else if(res==1) printf("%s is bigger than %s\n",str1,str2); else printf("%s is smaller than %s\n",str1,str2);

strcat(str1,str2); printf("The concatenated string is %s\n",str1); strrev(str1); printf("The reversed string is %s\n",str1); }