C Programming Sollution
C Programming Sollution
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("\nenter a number:-");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("\nthe no %d is a perfect number",i);
else
printf("\nthe no %d is not a perfect number",i);
return 0;
}
int main(){
int num,r,sum=0,temp;
printf("\nenter a number:-");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("\nthe number %d is an armstrong number",temp);
else
printf("\nthe number %d is not an armstrong number",temp);
return 0;
}
int main(){
int num,i,count=0;
printf("\nenter a number:");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
int main(){
int num,out;
scanf("%d",&num);
out=rev(num);
printf("%d",out);
return 0;
}
int rev(int num){
static sum,r;
if(num){
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else return 0;
return sum;
}
#include<stdio.h>
int main(){
int num,i,f,r,sum=0,temp;
printf("\nenter a number");
scanf("%d",&num);
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
return 0;
}
int main(){
int num,sum=0,r;
printf("\nenter a number:");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("sum=%d",sum);
return 0;
}
int main(){
int num,r,sum=0,temp;
printf("\nenter a number:");
scanf("%d",&num);
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a palindrome",temp);
return 0;
}
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nenter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nthe string is not a palindrome");
else
printf("\nthe string is a palindrome");
return 0;
}
10. write a c program to add two numbers without using addition operator.
11. write a c program to subtract two numbers without using subtraction operator.
12. write a c program to find largest among three numbers using binary minus operator.
#include<stdio.h>
int main(){
int a,b,c;
printf("\nenter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a-b>0 && a-c>0)
printf("\ngreatest is a :%d",a);
else
if(b-c>0)
printf("\ngreatest is b :%d",b);
else
printf("\ngreatest is c :%d",c);
return 0;
}
13. write a c program to find largest among three numbers using conditional operator
#include<stdio.h>
int main(){
int a,b,c,big;
printf("\nenter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nthe biggest number is:%d",big);
return 0;
}
#include <stdio.h>
#define n 5
void fstore1d(int a[], int a_size);
void fretrieve1d(int a[], int a_size);
void fedit1d(int a[], int a_size);
int main(){
int a[n];
printf("input data into the matrix:\n");
fstore1d(a, n);
fretrieve1d(a, n);
fedit1d(a, n);
fretrieve1d(a, n);
return 0;
}
void fstore1d(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
scanf("%d", &a[i]);
}
void fretrieve1d(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
printf("%6d ", a[i]);
printf("\n");
}
void fedit1d(int a[], int n){
int i, q;
for ( i = 0; i < n; ++i ){
printf("prev. data: %d\nenter 1 to edit 0 to skip.", a[i]);
scanf("%d", &q);
if ( q == 1 ){
printf("enter new value: ");
scanf("%d", &a[i]);
}
}
}
17. write a c program which passes two dimension array to function.
#include <stdio.h>
#define m 3
#define n 5
void fstore2d(int a[][n]);
void fretrieve2d(int a[][n]);
int main(){
int a[m][n];
printf("input data in matrix (%d x %d)\n", m, n);
fstore2d(a);
fretrieve2d(a);
return 0;
}
void fstore2d(int a[][n]){
int i, j;
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j)
scanf("%d", &a[i][j]);
}
}
void fretrieve2d(int a[][n]){
int i, j;
for ( i = 0; i < m; ++i ){
for ( j = 0; j < n; ++j)
printf("%6d ", a[i][j]);
printf("\n");
}
}
#include<stdio.h>
void main(){
int i,j,r,k=1;
printf("\nenter the range:");
scanf("%d",&r);
printf("\nfloyd's triangle\n\n");
for(i=1;i<=r;i++){
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
return 0;
}
int main()
{
int r,i,j,k;
printf("\nenter the number range:-");
scanf("%d",&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=10;j++)
printf(" %d*%d=%d",i,j,i*j);
printf("\n");
}
return 0;
}
int main(){
int num,i=1,j,k;
printf("\nenter a number:");
scanf("%d",&num);
while(i<=num){
k=0;
if(num%i==0){
j=1;
while(j<=i){
if(i%j==0)
k++;
j++;
}
if(k==2)
printf("\n%d is a prime factor",i);
}
i++;
}
return 0;
}
#include<stdio.h>
int main()
{
int n,r,ncr;
printf("enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("the ncr factor of %d and %d is %d",n,r,ncr);
return 0;
}
int fact(int n)
{
int i=1;
while(n!=0)
{
i=i*n;
n--;
}
return i;
}
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter any year: ");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
getch();
}
#include <stdio.h>
main()
{
int i, j;
i = 1;
while ( i < 300 )
{
j = 2;
while ( j < sqrt(i) )
{
if ( i % j == 0 )
break;
else
{
++j;
continue;
}
}
if ( j > sqrt(i) )
printf("%d\t", i);
++i;
}
return 0;
}
in hcf we try to find any largest number which can divide both the number.
for example: hcf or gcd of 20 and 30
both number 20 and 30 are divisible by 1, 2,5,10.
hcf=max (1, 2, 3, 4, 10) =10
logic for writing program:
it is clear that any number is not divisible by more than that number. in case of more than one number s,
a possible maximum number which can divide all of the number s must be minimum of all of that
numbers.
#include<stdio.h>
int main(){
int x,y,m,i;
printf("insert any two number: ");
scanf("%d%d",&x,&y);
m=x
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nhcf of two number is : %d",i) ;
break;
}
}
return 0;
}
swapping
or
swapping of two number
#include<stdio.h>
int main(){
int a=5,b=10;
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
//process two
a=5;
b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
//process three
a=5;
b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);
//process four
a=5;
b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);
//process five
a=5,
b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
getch();
#include<stdio.h>
int main(){
int a,b;
printf("\nenter two numbers:");
scanf("%d %d",&a,&b);
printf("\nbefore swapping a=%d b=%d",a,b);
a=a^b;
b=b^a;
a=a^b;
printf("\nafter swapping a=%d b=%d",a,b);
return 0;
}
or
swapping of two number
#include<stdio.h>
int main(){
int a=5,b=10;
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
//process two
a=5;
b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
//process three
a=5;
b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);
//process four
a=5;
b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);
//process five
a=5,
b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
getch();
#include<stdio.h>
int main(){
int i=0,j=0,k=0;
char str1[20],str2[20],temp[20];
puts("enter first string");
gets(str1);
puts("enter second string");
gets(str2);
printf("before swaping the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
temp[j++]=str1[i++];
}
temp[j]='\0';
i=0,j=0;
while(str2[i]!='\0'){
str1[j++]=str2[i++];
}
str1[j]='\0';
i=0,j=0;
while(temp[i]!='\0'){
str2[j++]=temp[i++];
}
str2[j]='\0';
printf("after swaping the strings are\n");
puts(str1);
puts(str2);
return 0;
}
conversion
#include<stdio.h>
int main(){
long int m,no=0,a=1;
int n,rem;
printf("enter any decimal number->");
scanf("%d",&n);
m=n;
while(n!=0){
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf("the value %ld in binary is->",m);
printf("%ld",no);
return 0;
}
#include<stdio.h>
int main(){
long int no,n=0,j=1,rem,no1;
printf("enter any number any binary form->");
scanf("%ld",&no);
no1=no;
while(no!=0){
rem=no%10;
n=n+rem*j;
j=j*2;
no=no/10;
}
printf("\nthe value of binary no. %ld is ->%ld",no1,n);
return 0;
}
#include<stdio.h>
int main(){
int i=0,j=0,rem=0,a[10],b[10];
long int num;
printf("\nenter a number :");
scanf("%ld",&num);
while(num){
if(num<8){
a[j++]=num;
break;
}
else{
a[j++]=num%8;
num=num/8;
}
}
for(i=j-1;i>=0;i--)
b[rem++]=a[i];
printf("\noctal equivalent :");
for(j=0;j<rem;j++)
printf("%d",b[j]);
return 0;
}
int main(){
long int no,n=0,j=1,rem,no1;
printf("enter any number any binary form->");
scanf("%ld",&no);
no1=no;
while(no!=0){
rem=no%10;
n=n+rem*j;
j=j*2;
no=no/10;
}
printf("\nthe value of binary no. %ld is ->%ld",no1,n);
return 0;
}
1. write a c program to convert the string from upper case to lower case.
#include<stdio.h>
int main(){
char str[20];
int i;
printf("enter any string->");
scanf("%s",str);
printf("the string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nthe string in uppercase is->%s",str);
return 0;
}
2. write a c program to convert the string from lower case to upper case.
#include<stdio.h>
int main(){
char str[20];
int i;
printf("enter any string->");
scanf("%s",str);
printf("the string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nthe string in lowercase is->%s",str);
return 0;
}
#include<stdio.h>
int main(){
int i,j,n;
char str[20][20],temp[20];
puts("enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("the sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
return 0;
}
6. write a c program for concatenation two strings without using string.h header file.
#include<stdio.h>
int main(){
int i=0,j=0;
char str1[20],str2[20];
puts("enter first string");
gets(str1);
puts("enter second string");
gets(str2);
printf("before concatenation the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i++]=str2[j++];
}
str1[i]='\0';
printf("after concatenation the strings are\n");
puts(str1);
return 0;
}
int main(){
int i=0,j=0;
char *str1,*str2,*str3;
puts("enter first string");
gets(str1);
puts("enter second string");
gets(str2);
printf("before concatenation the strings are\n");
puts(str1);
puts(str2);
while(*str1){
str3[i++]=*str1++;
}
while(*str2){
str3[i++]=*str2++;
}
str3[i]='\0';
printf("after concatenation the strings are\n");
puts(str3);
return 0;
}
int main(){
char str[20];
int i=0;
puts("enter a string");
gets(str);
printf("%c",*str);
while(str[i]!='\0'){
if(str[i]==' '){
i++;
printf("%c",*(str+i));
}
i++;
}
return 0;
}
#include<stdio.h>
int main(){
char *p;
char s[20],s1[1];
printf("\nenter a string: ");
scanf("%[^\n]",s);
fflush(stdin);
printf("\nenter character: ");
gets(s1);
p=strpbrk(s,s1);
printf("\nthe string from the given character is: %s",p);
return 0;
}
matrix
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
printf("enter the first matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nenter the second matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\nthe first matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nthe second matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nthe addition of two matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
int main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf("\nenter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nenter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o){
printf("matrix mutiplication is not possible");
printf("\ncolumn of first matrix must be same as row of second matrix");
}
else{
printf("\nenter the first matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nenter the second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nthe first matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
printf("\nthe second matrix is\n");
for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nthe multiplication of two matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
return 0;
}
#include<stdio.h>
int main(){
int a[10][10],i,j,sum=0,m,n;
printf("\nenter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nenter the first matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nthe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nsum of the diagonal elements of a matrix is -> ");
printf("%d",sum);
return 0;
}
#include<stdio.h>
int main(){
int a[10][10],b[10][10],i,j,k=0,m,n;
printf("\nenter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nenter the first matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nthe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=a[j][i];
printf("\n%d",b[i][j]);
}
}
printf("\n\ntraspose of a matrix is -> ");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",b[i][j]);
}
}
return 0;
}
file
1. write a c program to open a file and write some text and close its.
#include<stdio.h>
int main(){
file *fp;
char ch;
fp=fopen("file.txt","w");
printf("\nenter data to be stored in to the file:");
while((ch=getchar())!=eof)
putc(ch,fp);
fclose(fp);
return 0;
}
int main(){
file *p,*q;
char file1[20],file2[20];
char ch;
printf("\nenter the source file name to be copied:");
gets(file1);
p=fopen(file1,"r");
if(p==null){
printf("cannot open %s",file1);
exit(0);
}
printf("\nenter the destination file name:");
gets(file2);
q=fopen(file2,"w");
if(q==null){
printf("cannot open %s",file2);
exit(0);
}
while((ch=getc(p))!=eof)
putc(ch,q);
printf("\ncompleted");
fclose(p);
fclose(q);
return 0;
}
#include<stdio.h>
int main(){
file *p;
char ch;
p=fopen("raja.c","r");
while((ch=getc(p))!=-1)
putchar(ch);
fclose(p);
return 0;
}
int main(){
char str[70];
file *p;
if((p=fopen("string.txt","r"))==null){
printf("\nunable t open file string.txt");
exit(1);
}
while(fgets(str,70,p)!=null)
puts(str);
fclose(p);
return 0;
}
int main(){
file *p;
int i,a[10];
if((p=fopen("myfile.dat","wb"))==null){
printf("\nunable to open file myfile.dat");
exit(1);
}
printf("\nenter ten values, one value on each line\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
fwrite(a,sizeof(a),1,p);
fclose(p);
return 0;
}
9. write a c program which concatenate two file and write it third file.
#include<stdio.h>
#include <time.h>
#include <sys\stat.h>
#include <stdio.h>
void main()
{
struct stat status;
file *fp;
fp=fopen("test.txt","r");
fstat(fileno(fp),&status);
clrscr();
printf("size of file : %d",status.st_size);
printf("drive name : %c",65+status.st_dev);
getch();
}
explanation:
function int fstat (char *, struct stat *) store the information of open file in form of structure struct stat
structure struct stat has been defined in sys\stat.h as
struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
here
(a)st_dev: it describe file has stored in which
drive of your computer ,it returns a number.
(b)st_mode: it describes various modes of file
like file is read only, write only, folder,
character file etc.
(c)st_size: it tells the size of file in byte.
(d)st_ctime:it tells last data of modification of
the file in date format.
note: 65 is ascii value of a .so after adding status.st_dev with 65 it will return appropriate drvie name as
in your computer.
}
output: it is directory.
explanation:
function int stat (char *, struct stat *) store the information of open file in form of structure struct stat
so masking or bitwise and operation between status.st_mode and s_ifdir return true if it is directory and
so on
12. write a c program to know permission of any file.
answer:
#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
void main()
struct stat status;
file *fp;
stat("test.txt",&status);
clrscr();
getch();
explanation:
struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
here
(j)st_mode: it describe various mode of file like file is read only, write only, folder, character file etc.
name meaning
s_ifdir directory
s_ififo fifo special
s_ifchr character special
s_ifblk block special
s_ifreg regular file
so masking or bit wise and operation between status.st_mode and s_iread return true you have read
permission and so on
#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
void main()
struct stat status;
file *fp;
fp=fopen("test.txt","r");
fstat(fileno(fp),&status);
clrscr();
getch();
explanation:
struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
here
(f)st_mode: it describes various modes of file like file is read only, write only, folder, character file etc.
answer:
#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
void main()
struct stat status;
file *fp;
fp=fopen("test.txt","r");
fstat(fileno(fp),&status);
clrscr();
getch();
explanation:
struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
here
note: 65 is ascii value of a .so after adding status.st_dev with 65 it will return appropriate drvie name as
in your computer.
complex number
int main(){
int a,b,c,d,x,y;
printf("\nenter the first complex number:");
scanf("%d%d",&a,&b);
printf("\nenter the second complex number:");
scanf("%d%d",&c,&d);
if(b<0)
printf("%d-i\n",a-b);
else
printf("d+i\n",a+b);
if(d<0)
printf("d-i\n",c-d);
else
printf("%d+i\n",c+d);
printf("\naddition ");
x=a+c;
y=b+d;
if(y>0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
printf("\n\nsubtraction ");
x=a-c;
y=b-d;
if(y<0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
return 0;
}
int main(){
int a[50],size,i,big;
printf("\nenter the size of the array: ");
scanf("%d",&size);
printf("\nenter %d elements in to the array: ”, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nbiggest: %d",big);
return 0;
}
#include<stdio.h>
int main(){
int a[50],size,i,big;
printf("\nenter the size of the array: ");
scanf("%d",&size);
printf("\nenter %d elements in to the array: ”, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nbiggest: %d",big);
return 0;
}
int main(){
int a[50],size,i,big;
printf("\nenter the size of the array: ");
scanf("%d",&size);
printf("\nenter %d elements in to the array: ”, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nbiggest: %d",big);
return 0;
}
#include<stdio.h>
int main(){
int arr[50];
int *p;
int i,j,k,size,n;
printf("\nenter size of the array: ");
scanf("%d",&n);
printf("\nenter %d elements into the array: ",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
size=n;
p=arr;
for(i=0;i<size;i++){
for(j=0;j<size;j++){
if(i==j){
continue;
}
else if(*(p+i)==*(p+j)){
k=j;
size--;
while(k < size){
*(p+k)=*(p+k+1);
k++;
}
j=0;
}
}
}
printf("\nthe array after removing duplicates is: ");
for(i=0;i < size;i++){
printf(" %d",arr[i]);
}
return 0;
}
{
int a[50],i,pos,size;
clrscr();
printf("\nenter size of the array: ");
scanf("%d",&size);
printf("\nenter %d elements in to the array: ",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\nenter position where to delete: ");
scanf("%d",&pos);
i=0;
while(i!=pos-1)
i++;
while(i<10)
{
a[i]=a[i+1];
i++;
}
size--;
for(i=0;i<size;i++)
printf(" %d",a[i]);
getch();
}
int main(){
int a[50],size,num,i,pos,temp;
printf("\nenter size of the array: ");
scanf("%d",&size);
printf("\nenter %d elements in to the array: ",size);
for(i=0;iscanf("%d",&a[i]);
printf("\nenter position and number to insert: ");
scanf("%d %d",&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp--;
}
a[i]=num;
for(i=0;iprintf(" %d",a[i]);
return 0;
}
sorting
int main(){
int i,j,s,temp,a[20];
printf("\nenter size of the array: ");
scanf("%d",&s);
printf("\nenter %d elements in to the array:",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nafter sorting the elements are: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
int main(){
int s,i,j,temp,a[20];
printf("\nenter size of the array :");
scanf("%d",&s);
printf("\nenter %d elements in to the array:");
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nthe array after sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
int main(){
int x[20],size,i;
printf("\nenter size of the array :");
scanf("%d",&size);
printf("\nenter %d elements :",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("\nsorted elements :");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
5. write a c program for heap sort.
6. write a c program for merge sort.
7. write a c program for shell sort.
recursion
int main(){
int num,f;
printf("\nenter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nfactorial of %d is: %d",num,f);
return 0;
}
int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));
}
int main(){
int n1,n2,gcd;
printf("\nenter two numbers: ");
scanf("%d %d",&n1,&n2);
gcd=findgcd(n1,n2);
printf("\ngcd of %d and %d is: %d",n1,n2,gcd);
return 0;
}
int findgcd(int x,int y){
while(x!=y){
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}
int main(){
int num,x;
clrscr();
printf("\nenter a number: ");
scanf("%d",&num);
x=findsum(num);
printf("sum of the digits of %d is: %d",num,x);
return 0;
}
int r,s;
int findsum(int n){
if(n){
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}
int main(){
int pow,num;
long int res;
long int power(int,int);
printf("\nenter a number: ");
scanf("%d",&num);
printf("\nenter power: ");
scanf("%d",&pow);
res=power(num,pow);
printf("\n%d to the power %d is: %ld",num,pow,res);
return 0;
}
int i=1;
long int sum=1;
long int power(int num,int pow){
if(i<=pow){
sum=sum*num;
power(num,pow-1);
}
else
return sum;
}
#include<stdio.h>
int main(){
int num,rev;
printf("\nenter a number :");
scanf("%d",&num);
rev=reverse(num);
printf("\nafter reverse the no is :%d",rev);
return 0;
}
int sum=0,r;
reverse(int num){
if(num){
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
return sum;
}
1. write a c program to find the size of int without using sizeof operator.
2. write a c program to find the size of double without using sizeof operator.
3. write a c program to find the size of structure without using sizeof operator.
4. write a c program to find the size of union without using sizeof operator.
using pointer
searching
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0;
printf("enter the size of an array");
scanf("%d",&n);
printf("\nenter the elements of the array");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("\nthe elements of an array are");
for(i=0;i<=n-1;i++){
printf(" %d",a[i]);
}
printf("\nenter the number to be search");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("\nthe number is not in the list");
else
printf("\nthe number is found");
return 0;
}
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0,l,u,mid;
printf("enter the size of an array->");
scanf("%d",&n);
printf("\nenter the elements of the array->");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\nthe elements of an array are->");
for(i=0;i<n;i++){
printf(" %d",a[i]);
}
printf("\nenter the number to be search->");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("\nthe number is not in the list");
else
printf("\nthe number is found");
return 0;
}
#include<stdio.h>
int main(){
int a[10],i,n,m,c,l,u;
printf("enter the size of an array->");
scanf("%d",&n);
printf("\nenter the elements of the array->");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\nthe elements of an array are->");
for(i=0;i<n;i++){
printf(" %d",a[i]);
}
printf("\nenter the number to be search->");
scanf("%d",&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf("\nthe number is not in the list");
else
printf("\nthe number is found");
return 0;
}
int binary(int a[],int n,int m,int l,int u){
int mid,c=0;
if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
http://cquestionbank.blogspot.com/2010/07/c-program-examples.html
general
1. write a c program to convert the string from upper case to lower case.
2. write a c program to convert the string from lower case to upper case.
3. write a c program to delete the all consonants from given string.
4. write a c program to count the different types of characters in given string.
5. write a c program to sort the characters of a string.
6. write a c program for concatenation two strings without using string.h header file.
7. write a c program to find the length of a string using pointer.
8. write a c program which prints initial of any name.
9. write a c program to print the string from given character.
matrix
1. write a c program to open a file and write some text and close its.
2. write a c program to delete a file.
3. write a c program to copy a file from one location to other location.
4. write a c program to copy a data of file to other file.
5. write a c program which display source code as a output.
6. write a c program which writes string in the file.
7. write a c program which reads string from file.
8. write a c program which writes array in the file.
9. write a c program which concatenate two file and write it third file.
10. write a c program to find out size of any file.
11. write a c program to know type of file.
12. write a c program to know permission of any file.
13. write a c program to know last date of modification of any file.
14. write a c program to find size and drive of any file.
complex number
1. write a c program to find the size of int without using sizeof operator.
2. write a c program to find the size of double without using sizeof operator.
3. write a c program to find the size of structure without using sizeof operator.
4. write a c program to find the size of union without using sizeof operator.
using pointer