C Programs Example 2
Program to calculate the number of vowels in a given string
#include<stdio.h>int main( )
{
char ch,c[50];
int k=0,a=0,e=0,i=0,o=0,u=0;
printf("\nEnter the string to find the number of vowels : \n");
scanf("%s",c);
ch=c[k];
while(ch!='\0')
{
switch(ch)
{
case 'a' : a++;
break;
case 'e' : e++;
break;
case 'i' : i++;
break;
case 'o' : o++;
break;
case 'u' : u++;
break;
default: printf(“\nNo vowels in string”);
}
ch=c[++k];
}
printf("\nNumber of vowel a is %d\n",a);
printf("\nNumber of vowel e is %d\n",e);
printf("\nNumber of vowel i is %d\n",i);
printf("\nNumber of vowel o is %d\n",o);
printf("\nNumber of vowel u is %d\n",u);
return 0;
}
Output:
Enter the string to find the number of vowels :
dictionary
Number of vowel a is 1
Number of vowel e is 0
Number of vowel i is 2
Number of vowel o is 1
Number of vowel u is 0
Enter the string to find the number of vowels :
beautiful
Number of vowel a is 1
Number of vowel e is 1
Number of vowel i is 1
Number of vowel o is 0
Number of vowel u is 2
Program to print the given format of n number of rows * * * * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q,r,x,k;
clrscr();
q=0;
printf("Number of rows :");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q<r)
{
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;x++)
{
k=0;
printf("%c",'*');
while(k<5)
{
printf(" ");
k++;
}
}
printf("\n");
q++;
}
getch();
}
Output
Number of rows : 4
Pascal’s Triangle:
*
* *
* * *
* * * *
C Program to perform concatenation of two strings without using built in functions.
#include<stdio.h>
int main()
{
char str1[30],str2[30];
int i=0,j=0,c=0,c1=0;
printf("Enter two strings \n");
scanf("%s%s",str1,str2);
while(str1[i]!='\0')
{
c++;
i++;
}
while(str2[j]!='\0')
{
c1++;
j++;
}
for(i=0;i<c1;i++)
{
str1[c]=str2[i];
c++;
}
str1[c]='\0';
printf("%s",str1);
return 0;
}
Output
Enter two strings
Hi
Hello
HiHello
Write a program to accept the employee details and calculate each of the employee commission. Given commission is 20% of the salary.
employee.c
Program
#include<stdio.h>
#include<conio.h>
void main()
{
struct employee
{
int empno,salary;
char name[15],sex[10];
float comm;
}emp[10];
int lim,i=0;
printf("Enter the number of employees :\n");
scanf("%d",&lim);
while(i<lim)
{
printf("Enter the employee details: ");
printf("\nEmployee number :");
scanf("%d",&emp[i].empno);
printf("\nEmployee name : ");
scanf("%s",emp[i].name);
printf("\nSex : ");
scanf("%s",emp[i].sex);
printf("\nSalary : ");
scanf("%d",&emp[i].salary);
i++;
}
for(i=0;i<lim;i++)
{
emp[i].comm=emp[i].salary*.2;
printf("\nCommission of employee %d is %f",i+1,emp[i].comm);
}
getch();
}
Output
Enter the number of employees :2
Enter the employee details:
Employee number :1
Employee name :anu
Sex :female
Salary :30000
Enter the employee details:
Employee number :2
Employee name :manu
Sex :male
Salary :25000
Commission of employee 1 is 6000.000000
Commission of employee 2 is 5000.000000
Write a program to print the student record. Accept name, register nos. and marks in three subjects for each student and compute the class average of each subject.
student.c
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,lim,p=0,c=0,m=0;
float avg1,avg2,avg3;
struct student
{
char name[10];
int regno,phy,chem,maths;
}s[10];
clrscr();
printf("\nEnter the number of students whos details to be entered : ");
scanf("%d",&lim);
while(i<lim)
{
printf("Enter student name : ");
scanf("%s",s[i].name);
printf("\nEnter regno : ");
scanf("%d",&s[i].regno);
printf("\nEnter the marks for physics : ");
scanf("%d",&s[i].phy);
printf("\nEnter the marks for chemistry : ");
scanf("%d",&s[i].chem);
printf("\nEnter the marks for maths : ");
scanf("%d",&s[i].maths);
i++;
}
for(i=0;i<lim;i++)
{
p=p+s[i].phy;
c=c+s[i].chem;
m=m+s[i].maths;
}
avg1=p/lim;
avg2=c/lim;
avg3=m/lim;
printf("\nClass average of physics : %.3f",avg1);
printf("\nClass average of chemistry : %.3f",avg2);
printf("\nClass average of maths : %.3f",avg3);
getch();
}
Output
Enter the number of students whos details to be entered :5
Enter student name : Mini
Enter regno : 25
Enter the marks for physics : 47
Enter the marks for chemistry : 39
Enter the marks for maths : 42
Enter student name : Akhil
Enter regno : 33
Enter the marks for physics : 40
Enter the marks for chemistry : 35
Enter the marks for maths : 41
Enter student name : Mathew
Enter regno : 20
Enter the marks for physics : 33
Enter the marks for chemistry : 39
Enter the marks for maths : 41
Enter student name : Manu
Enter regno : 29
Enter the marks for physics : 47
Enter the marks for chemistry : 42
Enter the marks for maths : 42
Enter student name : Smith
Enter regno : 19
Enter the marks for physics : 48
Enter the marks for chemistry : 32
Enter the marks for maths : 42
Class average of physics :43.000
Class average of chemistry :37.400
Class average of maths :41.600
C program to simulate a simple calculator to perform arithmetic operations like addition, subtraction, multiplication and division
#include <stdio.h>
void main()
{
char operator;
float num1, num2, result;
printf("Simulation of a Simple Calculator\n");
printf("*********************************\n");
printf("Enter two numbers \n");
scanf("%f %f", &num1, &num2);
fflush(stdin);
printf("Enter the operator [+,-,*,/] \n");
scanf("%s", &operator);
switch(operator)
{
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
case '/': result = num1 / num2;
break;
default : printf("Error in operationn");
break;
}
printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result);
}
C program to sort N numbers in ascending order and print both the given and the sorted array.
#include <stdio.h>#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
}
Comments
Post a Comment