Program 11: Find average and rank of a student for given input marks using functions
Program:
#include<stdio.h>
#include<conio.h>
int total(int,int,int);
float average(int);
int main()
{
int s1,s2,s3,tot;
float avg;
clrscr();
printf("Enter the values for s1,s2,s3\n");
scanf("%d%d%d",&s1,&s2,&s3);
printf("The value of three subjects is %d%d%d\n",s1,s2,s3);
tot=total(s1,s2,s3);/*function declaration*/
avg=average(tot);
printf("The average of 3 subjects is\t %f\n",avg);
if(avg>75&&avg<100)
{
printf("First Rank");
}
else if(avg>50&&avg<75)
{
printf("Second Rank");
}
else if(avg>35&&avg<50)
{
printf("Third Rank");
}
else
{
printf("Fail");
}
getch();
return 0;
}
int total(int a,int b ,int c)/*function definition*/
{
int tot1;
tot1=a+b+c;
printf("the total is%d\n",tot1);
return tot1;
}
float average(int x)
{
float avg1;
avg1=x/3;
Return x;
Program 12: Find Factorial value between the range from 1 and 20
Program:
#include<stdio.h>
#include<conio.h>
int fact,y;
char answer;
int factorial(int a);
void main()
{
do
{
answer='y';
printf("Enter an integer value between 1 and 20: ");
scanf("%d",&y);
if(y > 20 || y<1)
{
printf("Only values between 1 and 20 are acceptable!\n");
}
else
{
fact = factorial(y);
printf("%d factorial equals %d\n",y,fact);
}
printf("\nDo you want to continue (Y/N)? ");
answer=getch();
}while (answer=='y' || answer=='Y');
}
int factorial(int a) /*function to calculate factorial*/
{
if (a == 1)
return 1;
else
{
a *=factorial(a-1);
return a; /* returns int value to program */
}
}
Program 13:Max element from given array using Pointers
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[100],*max,s,c,loc=1;
clrscr();
printf("Enter the number of elements in array\n");
scanf("%d",&s);
printf("Enter %dintegers\n",s);
for(c=0;c<s;c++)
scanf("%d",&a[c]);
max=a;
*max=*a;
for(c=1;c<s;c++)
{
if(*(a+c)>*max)
{
*max=*(a+c);
loc=c+1;
}
}
printf("Maximium element is persent at
location %d and it's value is%d\n",loc,*max);
getch();
return 0;
}
Program 14: Display frequency count vowels, consonants, digits and spaces
Program:
#include <stdio.h>
int main()
{
char line[150];
int vowels, consonant, digit, space;
vowels = consonant = digit = space = 0;
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
for (int i = 0; line[i] != '\0'; ++i)
{
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||
line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||
line[i] == 'U')
{
++vowels;
}
else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z'))
{
++consonant;
}
else if (line[i] >= '0' && line[i] <= '9')
{
++digit;
}
else if (line[i] == ' ')
{
++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
return 0;
}
//realloc function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
n = 10;
printf("\n\nEnter the new size of the array: %d\n", n);
ptr = realloc(ptr, n * sizeof(int));
printf("Memory successfully re-allocated using realloc.\n");
for (i = 5; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
free(ptr);
}
return 0;
}
No comments: