0% found this document useful (0 votes)
108 views11 pages

C Programs (1-13)

The document contains 11 C programming questions and their solutions. The questions cover topics such as generating Fibonacci numbers, finding GCD and LCM, checking if a number is palindrome, checking if a number is prime, calculating factorial using functions, finding sum of even and odd numbers using functions, finding string length without built-in functions, adding and subtracting matrices, converting case of characters in a string, counting vowels, consonants, digits and special characters in a string, and accepting product details using structures.

Uploaded by

Pallavi Kumari
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)
108 views11 pages

C Programs (1-13)

The document contains 11 C programming questions and their solutions. The questions cover topics such as generating Fibonacci numbers, finding GCD and LCM, checking if a number is palindrome, checking if a number is prime, calculating factorial using functions, finding sum of even and odd numbers using functions, finding string length without built-in functions, adding and subtracting matrices, converting case of characters in a string, counting vowels, consonants, digits and special characters in a string, and accepting product details using structures.

Uploaded by

Pallavi Kumari
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/ 11

1.Write a C program to generate and print first N Fibonacci numbers.

//program to generate a Fibonacci series


#include<stdio.h>
void main()
{
int f1,f2,f3,i,n;
clrscr();
f1=0;
f2=1;
printf("\n Enter the number of terms to be generated : ");
scanf("%d",&n);
if(n<=0)
{
printf("\nCount must be greater than zero");
}
else
{
printf("\nFibonacci series of %d numbers\n",n);
if(n==1)
printf("%d",f1);
else if(n==2)
printf("%d\t%d",f1,f2);
else
{
printf("\n%d\t%d",f1,f2);
for(i=2;i<n;i++)
{
f3=f1+f2;
printf("\t%d",f3);
f1=f2;
f2=f3;
}
}
}
getch();
}

2.Write a C program to find the GCD and LCM of two integer numbers.
//program to find the GCD and LCM of given two numbers
#include<stdio.h>
void main()
{
int a,b,gcd,lcm,x,y;
clrscr();
printf("\nEnter the first positive integer : ");
scanf("%d",&a);
print("\nEnter the second positive integer : ");
scanf("%d",&b);
if((a==0)||(b==0))
{
printf("\nGCD and LCM is computed for positive integers");
}
else
{
x=a;
y=b;
while(x!=y)
{
if(x>y)
x=x-y;
else
y=y-x;
}
gcd=x;
printf("\nGCD of %d and %d is %d",a,b,gcd);
lcm=(a*b)/gcd;
printf("\nLCM of %d and %d is %d",a,b,lcm);
}
getch();
}

3.Write a C program that reverses given integer number and checks whether
the number is palindrome or not.
//program to check whether a given number is palindrome or not
#include<stdio.h>
void main()
{
int m,n,s=0,r;
clrscr();
printf("\nEnter the number : ");
scanf("%d",&n);
m=n;
while(n>0)
{
r=n%10;
n=n/10;
s=s*10+r;
}
if(s==m)
printf("\n %d is a palindrome number",m);
else
printf("\n %d is not a palindrome number",m);
getch();
}
4.Write a C program to find whether a given number is prime number or not.
//program to check whether a given number is prime or not
#include<stdio.h>
void main()
{
int i,n,m,flag=0;
clrscr();
printf("\n Enter a positive integer greater than 1 : ");
scanf("%d",&n);
if(n<=1)
{
printf("\nThe number should be greater than 1");
getch();
exit();
}
else
{
for(i=2;i<n;i++)
{
m=n%i;
if(m==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("\n The number %d is a prime number",n);
else
printf("\n The number %d is not a prime number",n);
getch();
}
}

5.Write a C program to find the factorial of a number using function.


#include<stdio.h>
void main()
{
int no;
int factorial(int);
clrscr();
printf("\nEnter the value : ");
scanf("%d",&no);
if(no>=0)
printf("\nThe factorial of %d is %d",no,factorial(no));
else
printf("\nInput value must be greater than or equal to zero");
getch();
}
int factorial(int n)
{
int fact;
if((n==1)||(n==0))
return(1);
else
return(n*factorial(n-1));
}

6.Write a C program to compute the sum of even numbers and the sum of odd
numbers using function.
//program to find the sum of odd and even numbers
#include<stdio.h>
void main()
{
int n,i,a[100];
void oddevensum(int a[],int);
clrscr();
printf("\nEnter the number count : ");
scanf("%d",&n);
printf("\nEnter the numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The entered elements\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
if(n>0)
oddevensum(a,n);
else
printf("\nThe count should be greater than zero");
getch();
}
void oddevensum(int a[],int n)
{
int i,esum=0,osum=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
esum=esum+a[i];
else
osum=osum+a[i];
}
printf("\n\nThe sum of even numbers is %d",esum);
printf("\n\nThe sum of odd numbers is %d",osum);
}

7. Write a C program to find the length of the string without using


built-in function.
//program to find length of the string
#include<stdio.h>
#include<conio.h>
void main()
{
char a[200];
int i=0,length;
clrscr();
printf("\n Enter the string : \n");
gets(a);
while(a[i]!='\0')
{
i++;
}
printf("\n The length of the enetered string is %d",i);
getch();
}

8. Write a C program to read two matrices and perform addition and


subtraction of two matrices.

//Program to add and subtract two matrices

#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10];
int i,j,r1,c1,r2,c2;
clrscr();
printf("\nEnter the number of rows in the matrix A : ");
scanf("%d",&r1);
printf("\nEnter the number of columns in the matrix A : ");
scanf("%d",&c1);
printf("\nEnter the number of rows in the matrix B : ");
scanf("%d",&r2);
printf("\nEnter the number of columns in the matrix B : ");
scanf("%d",&c2);
if((r1!=r2)||(c1!=c2))
{
printf(" \nOperation not possible - Matrices are not of the
same order");
getch();
exit(0);
}
else
{
printf("\nEnter the matrix A elements\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the matrix B elements\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
//performing addition and subtraction
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}
printf("\nMatrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("\nMatrix B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\t%d",b[i][j]);
}
printf("\n");
}
printf("\nMatrix C=A+B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
printf("\nMatrix D=A-B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\t%d",d[i][j]);
}
printf("\n");
}
}
getch();
}

9. Write a C program to accept a sentence and convert all lowercase


characters to uppercase and vice-versa.
//Program to convert lowercase characters to uppercase and vice versa
#include<stdio.h>
#include<string.h>
void main()
{
char str[200];
int i=0;
clrscr();
printf("\nEnter the string\n");
gets(str);
while(str[i]!='\0')
{
if((str[i]>='a')&&(str[i]<='z'))
{
str[i]=str[i]-32;
}
else if((str[i]>='A')&&(str[i]<='Z'))
{
str[i]=str[i]+32;
}
i++;
}
printf("\nThe converted string\n");
puts(str);
getch();
}

10. Write a C program to count the number of vowels, consonants, digits,


special characters, whitespace in a given string.
//To count vowels,consonants,digits,special characters and white spaces in
a string
#include<stdio.h>
#include<ctype.h>
void main()
{
char str[100];
int i,l,vcount=0,ccount=0,dcount=0,wcount=0,scount=0;
clrscr();
printf("\nEnter the stirng : ");
gets(str);
strlwr(str);
l=strlen(str);
for(i=0;i<l;i++)
{
if(isalpha(str[i]))
{

if((str[i]=='a')||(str[i]=='e')||(str[i]=='i')||(str[i]=='o')||(str[i]=='u
'))
vcount++;
else
ccount++;
}
else
{
if(isdigit(str[i]))
dcount++;
else if(isspace(str[i]))
wcount++;
else
scount++;
}
}
printf("\nVowels - %d",vcount);
printf("\nConsonants - %d",ccount);
printf("\nDigits - %d",dcount);
printf("\nSpecial characters - %d",scount);
printf("\nWhite spces - %d",wcount);
getch();
}

11. Write a C program to accept different goods with the number, price and
date of purchase and display them using
structure.
#include<stdio.h>
struct date
{
int dd;
int mm;
int yy;
};
struct product
{
int number;
int price;
struct date dt;
};
void main()
{
struct product p[5];
int i;
clrscr();
printf("\nEnter the details of 3 products\n");
for(i=0;i<3;i++)
{
clrscr();
printf("\nPRODUCT %d DETAILS\n",i+1);
printf("\nEnter the product number -");
scanf("%d",&p[i].number);
printf("\nEnter the price of the product -");
scanf("%d",&p[i].price);
printf("\nEnter the date of purchase in DD MM YY format\n");
printf("\nEnter the day of purchase\n");
scanf("%d",&p[i].dt.dd);
printf("\nEnter the month of purchase\n");
scanf("%d",&p[i].dt.mm);
printf("\nEnter the year of purchase\n");
scanf("%d",&p[i].dt.yy);
}
clrscr();
printf("\nPRODUCT DETAILS\n");
printf("\nNUMBER COST DATE\n");
for(i=0;i<3;i++)
{
printf("\n%d\t%d\t%d-%d-%d\n",p[i].number,p[i].price,
p[i].dt.dd,p[i].dt.mm,p[i].dt.yy);
}
getch();
}

12.Write a C program to copy one string to another using pointer.


//program to copy a string to another using pointers
#include<stdio.h>
void main()

{
char s1[200],s2[100];
void copy(char *p1,char *p2);
clrscr();
printf("\nEnter the string\n");
gets(s1);
copy(s1,s2);
printf("\nThe original string is %s ",s1);
printf("\nThe copied string is %s ",s2);
getch();
}
void copy(char *p1,char *p2)
{
while(*p1!='\0')
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
}

13. Write a C program to create file for storing details of N students.


Student record should contain Roll No, Name and marks secured in two subjects.
Copy the contents of the above created file to another file.
//copying the contents of a file into another
#include<stdio.h>
void main()
{
FILE *fin,*fout;
int rollno,n,i;
char name[30];
clrscr();
fin=fopen("student.dat","w");
if (fin==NULL)
{
printf("\nFile cannot be opened\n");
getch();
exit(0);
}
printf("\nEnter the number of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nstudent %d \n",i+1);
printf("\nEnter the roll number : ");
scanf("%d",&rollno);
printf("\nEnter the name : ");
scanf("%s",&name);
fprintf(fin,"%d %s\n",rollno,name);
}
fclose(fin);
printf("\nStudent file created successfully");
fin=fopen("student.dat","r");

if(fin==NULL)
{
printf("\nFile cannot be opened");
getch();
exit(0);
}
fout=fopen("output.dat","w");
if(fout==NULL)
{
printf("\nFile cannot be opened\n");
getch();
exit(0);
}
fprintf(fout,"\nRoll.No Name\n\n");
while(fscanf(fin,"%d%s",&rollno,&name)!=EOF)
{
fprintf(fout,"%d\t%s\n",rollno,name);
}
fclose(fout);
}

You might also like