Important programs for cps……..

                                                                                           

STRUCTURES
Program to maintain student details using structure
#include<stdio.h>
#include<conio.h>
struct stud
{
int rollno, s1, s2, tot ;
char name[10] ;
float avg ;
} s[10] ;
void main()
{
int i, n ;
clrscr() ;
printf(“Enter the number of students : “) ;
scanf(“%d”, &n) ;
for(i = 0 ; i < n ; i++)
{
printf(“\nEnter the roll number : “) ;
scanf(“%d”, &s[i].rollno) ;
printf(“\nEnter the name : “) ;
scanf(“%s”, s[i].name) ;
printf(“\nEnter the marks in 2 subjects : “) ;
scanf(“%d %d”, &s[i].s1, &s[i].s2) ;
s[i].tot = s[i].s1 + s[i].s2 ;
s[i].avg = s[i].tot / 2.0 ;
}
printf(“\nRoll No. Name \t\tSub1\t Sub2\t Total\t Average\n\n”) ;
for(i = 0 ; i < n ; i++)
{
printf(“%d \t %s \t\t %d \t %d \t %d \t %.2f \n”,
s[i].rollno,s[i].name,s[i].s1,s[i].s2,s[i].tot,s[i].avg);
}
getch() ;
}
Output:
Enter the number of students : 2
Enter the roll number : 101
Enter the name : Arun
Enter the marks in 2 subjects : 75 85
Enter the roll number : 102
Enter the name : Babu
Enter the marks in 2 subjects : 65 75
Roll No. Name Sub1 Sub2 Total Average
101 Arun 75 85 160 80.00
102 Babu 65 75 140 70.00


                                                                                    STRINGS

Example #1: Calculate Length of String without Using strlen() Function
#include <stdio.h>
int main()
{
char s[1000];
int i;
printf(“Enter a string: “);
scanf(“%s”, s);
for(i = 0; s[i] != ‘\0’; ++i);
printf(“Length of string: %d”, i);
return 0;
}

Output
Enter a string: Programiz
Length of string: 9

Example #2: Find the Frequency of Characters
#include <stdio.h>
int main()
{
char str[1000], ch;
int i, frequency = 0;
printf(“Enter a string: “);
gets(str);
printf(“Enter a character to find the frequency: “);
scanf(“%c”,&ch);

for(i = 0; str[i] != ‘\0’; ++i)
{
if(ch == str[i])
++frequency;
}
printf(“Frequency of %c = %d”, ch, frequency);
return 0;
}

 

Output
Enter a string: This website is awesome.
Enter a character to find the frequency: e
Frequency of e = 4

Example #3: Program to count vowels, consonants.
#include <stdio.h>
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
printf(“Enter a line of string: “);
scanf(“%[^\n]”, line);
for(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’))
{
++consonants;
}
else if(line[i]>=’0′ && line[i]<=’9′)
{
++digits;
}
else if (line[i]==’ ‘)
{
++spaces;
}
}
printf(“Vowels: %d”,vowels);
printf(“\nConsonants: %d”,consonants);
printf(“\nDigits: %d”,digits);
printf(“\nWhite spaces: %d”, spaces);
return 0;
}

 

Output
Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2

Example #4: Concatenate Two Strings Without Using strcat()
#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf(“Enter first string: “);
scanf(“%s”, s1);
printf(“Enter second string: “);
scanf(“%s”, s2);
/* calculate the length of string s1 and store it in i */
for(i = 0; s1[i] != ‘\0’; ++i);
for(j = 0; s2[j] != ‘\0’; ++j, ++i)
{
s1[i] = s2[j];
}
s1[i] = ‘\0’;
printf(“After concatenation: %s”, s1);
return 0;
}
Output:
Enter first string: new
Enter second string: delhi
After concatenation: new delhi

Example #5: Copy String Manually Without Using strcpy()
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf(“Enter string s1: “);
scanf(“%s”,s1);
for(i = 0; s1[i] != ‘\0’; ++i)
{
s2[i] = s1[i];
}
s2[i] = ‘\0’;
printf(“String s2: %s”, s2);
return 0;
}
Output
Enter String s1: programming
String s2: programming

 


            POINTER

1) Write a C program to swap two numbers using pointer concept.
Ans:- #include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
printf(“Enter the value of x and y\n”);
scanf(“%d%d”, &x, &y);
printf(“Before Swapping\nx = %d\ny = %d\n”, x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf(“After Swapping\nx = %d\ny = %d\n”, x, y);

return 0;

}

2) Write a C program for sum of elements of an integer array using Pointer.
Ans:-
#include<stdio.h>
int main()
{
int array[5];
int i,sum=0;
int *ptr;
printf(“\nEnter array elements (5 integer values):”);
for(i=0;i<5;i++)
scanf(“%d”,&array[i]);
ptr = array;
for(i=0;i<5;i++)
{
//*ptr refers to the value at address
sum = sum + *ptr;
ptr++;
}
printf(“\nThe sum is: %d”,sum);
}


 PREPROCESSOR DIRECTIVE

1) Program on useof #define and #include 

#include <stdio.h>
#define height 100
#define number 3.14
#define letter ‘A’
#define letter_sequence “ABC”
#define backslash_char ‘\?’
void main()
{
printf(“value of height : %d \n”, height );
printf(“value of number : %f \n”, number );
printf(“value of letter : %c \n”, letter );
printf(“value of letter_sequence : %s \n”, letter_sequence);
printf(“value of backslash_char : %c \n”, backslash_char);
}
OUTPUT:
value of height : 100
value of number : 3.140000
value of letter : A
value of letter_sequence : ABC
value of backslash_char : ?

2) Program on use of #IFDEF, #ELSE AND #ENDIF
#include <stdio.h>
#define RAJU 100
int main()
{
#ifdef RAJU
printf(“RAJU is defined. So, this line will be added in this C file\n”);
#else
printf(“RAJU is not defined\n”);
#endif
return 0;
}

OUTPUT:
RAJU is defined. So, this line will be added in this C file

3) Program on use of #IFNDEF AND #ENDIF
#include <stdio.h>
#define RAJU 100
int main()
{
#ifndef SELVA
{
printf(“SELVA is not defined. So, now we are going to define here\n”);
#define SELVA 300
}
#else
printf(“SELVA is already defined in the program”);
#endif
return 0;
}
OUTPUT:
SELVA is not defined. So, now we are going to define here.

4) Program on use of #IF, #ELSE AND #ENDIF
#include <stdio.h>
#define a 100
int main()
{
#if (a==100)
printf(“This line will be added in this C file since a = 100\n”);
#else
printf(“This line will be added in this C file since a is not equal to 100\n”);
#endif
return 0;
}
OUTPUT:

This line will be added in this C file since a = 100


Some of the other programs which will definitely come from lab mannual.

Leave a Reply

Your email address will not be published. Required fields are marked *