/* B10. C Program to demonstrate the difference between structure and union */
#include<stdio.h>
struct mystruct
{
int i;
float f;
};
union myunion
{
int i;
float f;
};
void main()
{
struct mystruct s;
union myunion u;
clrscr();
printf("Size of the struct mystruct=%d\n", sizeof(s));
printf("Size of the union myunion=%d\n", sizeof(u));
s.i=10;
s.f=3.45;
printf("st.i=%d\n",s.i);
printf("st.f=%f\n",s.f);
u.i=20;
printf("ut.i=%d\n",u.i);
u.f=8.25;
printf("ut.f=%f\n",u.f);
getch();
}
Search This Blog
Wednesday, 2 March 2022
B10. C Program to demonstrate the difference between structure and union.
B9. C Program to demonstrate student structure to read and display records of n students.
// B9. C Program to demonstrate student structure to read and display records of n students.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int age;
char city[20];
};
void main()
{
struct student s[10];
int i, n;
clrscr();
printf("\nEnter the number of students \n");
scanf("%d",&n);
printf("\nEnter details of %d student(s):\n",n);
for(i=0;i<n;i++)
{
printf("\nEnter Name, age and city of %d student: \n",i+1);
scanf("%s%d%s",&s[i].name,&s[i].age,&s[i].city);
}
printf("\nThe details of students: \n");
printf("Name \t Age \t City \n");
printf("==== \t === \t ==== \n");
for(i=0;i<n;i++)
printf("%s \t %d \t %s \n",s[i].name,s[i].age,s[i].city);
getch();
}
B8. Write a C Program to Reverse a String using Pointer.
//B8. Write a C Program to Reverse a String using Pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("\nEnter a String:\n");
gets(s);
len=strlen(s);
printf("\nThe reverse of String is:\n");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}
B7. C Program to read a string and to find the number of alphabets, digits, vowels, consonants, spaces and special characters.
/* B7. C Program to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters. */
#include <stdio.h>
void main()
{
char str[100];
int i, digit, vowel, consonant, splchar, spaces;
clrscr();
digit = vowel = consonant = splchar = spaces = 0;
printf("Enter a string: ");
gets(str);
for(i=0;str[i]!=NULL;i++)
{
if(str[i]>='0' && str[i]<='9')
digit++;
else if(str[i]==' ')
spaces++;
else if(str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]== 'O' || str[i]=='U' ||
str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')
vowel++;
else if((str[i]>='A' && str[i]<='Z')||(str[i]>='a' && str[i]<='z'))
consonant++;
else
splchar++;
}
printf("\nDigits: %d \nVowels: %d \nSpaces: %d \nConsonants: %d \nSpecial Characters: %d",digit,vowel,spaces, consonant,splchar);
getch();
}
B5. Write a C Program to find the trace of a square matrix using function
//B5. Write a C Program to find the trace of a square matrix using function
#include<stdio.h>
#include<conio.h>
int trace(int[10][10],int); // Function prototype declaration.
void main()
{
int a[10][10],m,n,i,j,sum;
clrscr();
printf("Enter the order of the square matrix\n");
scanf("%d",&m);
printf("Enter the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]); // Read matrix elements.
sum=trace(a,m); // Call trace function.
printf("Trace of the matrix is %d\n",sum);
getch();
}
int trace(int a[10][10], int m) //Function to find trace of matrix.
{
int i,j,sum;
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==j) // Condition to check for diagonal elements.
sum=sum+a[i][j];
}
}
return sum;
}
B4. C Program to generate n prime numbers by defining isprime function.
//B4. C Program to generate n prime numbers by defining isprime function.
#include<stdio.h>
#include<conio.h>
void isprime(int); //Function prototype declaration.
void main()
{
int n;
clrscr();
printf("How many prime numbers are required? \n");
scanf("%d", &n);
printf("Prime numbers are:\n");
isprime(n);
}
void isprime(int n)
{
int count=1,flag, i=2,j;
/* Generating prime numbers */
while(count <= n)
{
flag = 0;
for(j=2; j <= i/2; j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d\n",i);
count++;
}
i++;
}
getch();
}
B3. C Program to demonstrate pointers in C
/* B3. C Program to demonstrate pointers in C */
#include<stdio.h>
void main()
{
int i=10, *ip;
float f=3.4, *fp;
char c='a', *cp;
clrscr();
printf("i=%d\n",i);
printf("f=%f\n",f);
printf("c=%c\n",c);
ip=&i;
printf("\nAddress of i=%u\n", ip);
printf("Value stored in i=%d\n", *ip);
fp=&f;
printf("\nAddress of f=%u\n", fp);
printf("Value stored in f=%f\n", *fp);
cp=&c;
printf("\nAddress of c=%u\n", cp);
printf("Value stored in c=%c\n", *cp);
getch();
}
B2. C Program to demonstrate string functions(Atleast 3)
/* B2. C Program to demonstrate string functions(Atleast 3)*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
int n,i,length1,length2;
clrscr();
printf("Enter first string\n");
scanf("%s",&str1);
printf("Enter second string\n");
scanf("%s",&str2);
printf("\nString1 Entered=%s\n",str1);
printf("String2 Entered=%s\n",str2);
length1=strlen(str1); // Find the length of str1
length2= strlen(str2); // Find the length of str2
printf("\nString Length of %s is %d\n",str1,length1);
printf("String Length of %s is %d\n",str2,length2);
strcat(str1,str2); // Concatenate str1 & str2
printf("\nString after concatenation:%s\n",str1);
strcpy(str1,str2); // Copy str2 in str1
printf("\nString after strcpy:%s", str1);
getch();
}
B1. C Program to find the length of a string without using built in function.
/* B1. C Program to find the length of a string without using built in function */
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int i,length=0;
clrscr();
printf("Enter a string:\n");
gets(str);
for(i=0;str[i]!='\0';i++)
length++;
printf("Length of %s is %d\n",str,length);
getch();
}