0% found this document useful (0 votes)
19 views2 pages

04 To 10 Programs - 111156

The document contains a series of C programming exercises covering various topics such as sum of elements, swapping values, structures, unions, file handling, string manipulation, and more. Each exercise includes code snippets demonstrating the implementation of specific functions and operations. Overall, it serves as a practical guide for learning different programming concepts in C.

Uploaded by

navinnavinraj88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

04 To 10 Programs - 111156

The document contains a series of C programming exercises covering various topics such as sum of elements, swapping values, structures, unions, file handling, string manipulation, and more. Each exercise includes code snippets demonstrating the implementation of specific functions and operations. Overall, it serves as a practical guide for learning different programming concepts in C.

Uploaded by

navinnavinraj88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EX NO 6a) SUM EX NO:7b) UNION

PROGRAM: #include<stdio.h>
#include <stdio.h> #include<stdlib.h>
int main() struct POINT1
{ int i, x[50], sum = 0,n; {int x,y;};
printf("Enter total number of Elements:"); union POINT2
scanf("%d",&n); {int x;
printf("\n Enter the Elements:"); int y;};
for(i = 0; i < n; ++i) int main()
{ scanf("%d", x+i); {struct POINT1 P1={2,3};
sum += *(x+i); } union POINT2 P2;
printf("Sum = %d", sum); printf("\n Coordinates of P1 are %d and %d",P1.x,P1.y);
return 0; } P2.x=4;
EX NO - 6b) SWAPPING printf("\n The x-Coordinates of P2 are %d",P2.x);
include <stdio.h> P2.y=5;
void swap(int *n1, int *n2); printf("\n The y-Coordinates of P2 are %d",P2.x);
int main() return 0;}
{ int num1,num2; EX NO:8PREPROCESSOR DIRECTIVE
printf("\n Enter two numbers:"); #include <stdio.h>
scanf("%d %d",&num1,&num2); #define INPUT printf("\n Enter Number:"); scanf("%d",&num)
printf("\n Before Swapping:"); #define EQUALS ==
printf("\n num1 = %d", num1); #define PRINT1 printf("\n GREAT")
printf("\n num2 = %d", num2); #define PRINT2 printf("\n TRY AGAIN")
swap( &num1, &num2); #define START int main() {
printf("\n After Swapping:"); #define END return 0; }
printf("\n num1 = %d", num1); START
printf("\n num2 = %d", num2); int num;
return 0; } INPUT;
void swap(int* n1, int* n2) if(num EQUALS 100)
{ int temp; PRINT1;
temp = *n1; else
*n1 = *n2; PRINT2;
*n2 = temp; } END
EX NO 6c) VOWELS AND CONSONANTS EX NO:9b)READ AND CLOSE
#include <stdio.h> # include <stdio.h>
void swap(int *n1, int *n2); int main( )
int main() { FILE *fp ;
{char str[100]; typedef struct student
char *ptr; {Int rollno;
int cntV,cntC; Char name[80];
printf("Enter a string: "); Float fees;
scanf("%s",str); char DOB[50] ;
ptr=str; }STUDENT;
cntV=cntC=0; STUDENT stud1;
while(*ptr!='\0') fp = fopen( "studentdetails.txt", "r" ) ;
{ if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U' ||*ptr=='a' ||*ptr=='e' if ( fp == NULL )
||*ptr=='i' ||*ptr=='o' ||*ptr=='u') { printf( "Could not open file " ) ;
cntV++; return 0; }
else Fscanf(fp,”%d %s %f %s”, &stud1.rollno,&stud1.name,&stud1.DOB);
cntC++; printf( “\n STUDENT DETAILS" ) ;
ptr++;} printf(“\n Roll Number:%d”,stud1.rollno);
printf("Total number of VOWELS: %d, CONSONANT: %d\n",cntV,cntC); printf(“\n Name:%s”,stud1.name);
return 0;} printf(“\n Fees:%f”,stud1.fees);
EX NO:7a)STRUCTURES printf(“\n DOB:%s”,stud1.DOB);
#include <stdio.h> fclose(fp) ;
struct student return 0; }
{char name[50]; EX NO:9a)OPEN, FILE WRITE AND FILE CLOSE
int roll; # include <stdio.h>
float marks;} s; # include <string.h>
int main() int main( )
{printf("Enter information:\n"); { FILE *fp ;
printf("Enter name: "); char filename[20],str[100];
scanf("%s",s.name); printf( "Enter the filename" ) ;
printf("Enter roll number: "); fp = fopen(filename, "w") ;
scanf("%d", &s.roll); if ( fp == NULL )
printf("Enter marks: "); { printf( "Could not open file test.c" ) ;
scanf("%f", &s.marks); exit(1); }
printf("Displaying Information:\n"); printf( "\n Enter some text from keyboard to write in the file " ) ;
printf("Name: "); gets(str);
printf("%s", s.name); fflush(stdin);fprintf(fp,”%s”,str);
printf("\n Roll number: %d", s.roll); fclose(fp) ;
printf("\n Marks: %.1f\n", s.marks); return 0; }
return 0;}
EX NO:10 ARGUMENTS EX NO 4b)STRING LENGTH
#include <stdio.h> #include<stdio.h>
#include <conio.h> int main() {
int main(int argc, char *argv[]) char str[100];
{ int i; int length;
if( argc >= 2 ) printf("\nEnter the String : ");
{ printf("The arguments supplied are:\n"); scanf(“%s”,str);
for(i = 1; i < argc; i++) length = 0; // Initial Length
{ printf("%s\t", argv[i]); } } while (str[length] != '\0')
else length++;
{ printf("argument list is empty.\n"); } printf("\nLength of the String is : %d", length);
return 0; } return(0); }
EX.NO:5aSWAPPING EX NO 4c)STRING REVERSE
#include <stdio.h> #include <stdio.h>
void swap(int, int); #include <string.h>
int main() int main()
{int x, y; { char Str[100], RevStr[100];
printf("Enter the value of x and y:"); int i, j, len;
scanf("%d%d",&x,&y); printf("\n Please Enter any String : ");
printf("\nBefore Swapping\n x = %d y = %d", x, y); scanf(“%s”,str);
swap(x, y); j = 0;
return 0;} len = strlen(Str);
void swap(int a, int b) for (i = len - 1; i >= 0; i--)
{int temp; { RevStr[j++] = Str[i]; }
temp = b; RevStr[i] = '\0';
b = a; printf("\n String after Reversing = %s", RevStr);
a = temp; return 0; }
printf("\nAfter Swapping\nx = %d y = %d\n", x, y);} EX NO 4d)STRING COMPARE
EX.NO:5b FACTORIAL #include<stdio.h>
#include <stdio.h> #include<string.h>
int fact(int n); int main()
int main() {char str1[50],str2[50];
{ int num,factorial; int i=0,len1=0,len2=0,same=0;
printf(Enter the Number:”); printf("\n Enter the First String:");
scanf(“%d”,&num); scanf("%s",str1);
factorial=fact(num); printf("\n Enter the Second String:");
printf("Factorial of %d is %d", num, factorial); scanf("%s",str2);
return 0; } len1=strlen(str1);
int factl(int n) len2=strlen(str2);
{ if (n==1) if(len1==len2)
return 1; {while(i<len1)
else {if(str1[i]==str2[i])
return (n*fact(n – 1)); } i++;
EX.NO:5cFIBONACCI else
#include<stdio.h> break;}
int fibonnaci(int); if(i==len1)
int main() { same=1;
{ int n, i = 0, res; printf("\n The two Strings are Equal"); } }
printf(“\n Enter Number of Terms:); if(len1!=len2)
scanf("%d", &n); printf("\n The two strings are not equal");
printf("Fibonacci series terms are:\n"); if(same==0)
for (i = 0; i <n; i++) { if(len1>len2)
{Res=fibonacci(i); printf("\n String1 is greater than String2");
printf("%d\t", res);} else
return 0;} printf("\n String2 is greater than String1");}
int fibonnaci(int n) return 0;}
{ if (n == 0 || n == 1) EX NO 4e)STRING CONCATENATE
return n; #include<stdio.h>
else #include<string.h>
return (fibonnaci(n-1) + fibonnaci(n-2)); } void concat(char[], char[]);
EX NO 4a)STRING FUNCTIONS int main() {
#include <stdio.h> char s1[50], s2[30];
#include <string.h> printf("\nEnter String 1 :");
int main() scanf(“%s”,s1);
{ char s1[20] = "Hello"; printf("\nEnter String 2 :");
char s2[20] = "World"; scanf(“%s”,s2);
printf(“\nString Comparison:”); concat(s1, s2);
if(strcmp(s1, s2) ==0) printf("\n Concated string is :%s", s1);
{ printf(" string 1 and string 2 are equal"); } return (0); }
else void concat(char s1[], char s2[]) {
{ printf(" string 1 and 2 are different"); } int i, j;
printf(“\n String Concatenation “); i = strlen(s1);
strcat(s1,s2); for (j = 0; s2[j] != '\0'; i++, j++) {
printf("\n Output string after concatenation: %s", s1); s1[i] = s2[j]; }
printf(“ \n String Copy“); s1[i] = '\0'; }
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0; }

You might also like