Tuesday 10 January 2012

C program for addition of two times



#include<stdio.h>
main()
{
int h,m,s,h1,m1,s1,h2,m2,s2,day;
printf("Enter first hours,minutes and seconds\n");
scanf("%d%d%d",&h1,&m1,&s1);
printf("Enter second hours,minutes and seconds\n");
scanf("%d%d%d",&h2,&m2,&s2);
s=h=m=day=0;
s=s1+s2;
if(s>60)
{
m=s/60;
s=s%60;
}
m=m+m1+m2;
if(m>60)
{
h=m/60;
m=m%60;
}
h=h+h1+h2;
if(h>24)
{
day=1;
h=h%24;
}
printf("First time = %d:%d:%d",h1,m1,s1);
printf("\nSecond time = %d:%d:%d",h2,m2,s2);
printf("\nAdded time =");
if(day==0)
printf("%d:%d:%d\n",h,m,s);
else
{
printf("%d day",day);
printf("%d:%d:%d\n",h,m,s);
}
}



C program to print a character pattern using pointers



#include<stdio.h>
main()
{
char a[20],*ptr;
int x,y;
printf("Enter any string\n");
scanf("%s",a);
printf("\nThe name is %s",a);
x=0;
while(x<=strlen(a))
{
printf("\n");
ptr=a;
for(y=0;y<x;y++)
{
printf("%c",*ptr);
ptr++;
}
x++;
}
}



C program to print the Fibonacci series using recursion



#include<stdio.h>
void fib(int);
main()
{
int z;
printf("Enter the limit\n");
scanf("%d",&z);
printf("Fibonacci sequence\n");
fib(z);
}
void fib(int z)
{
static int a,b;
int c;
if(z<2)
{
a=0;
b=1;
}
else
{
fib(z-1);
c=b;
b=a+b;
a=c;
}
printf("\n%d",a);
}




C program to find the largest and smallest values of a matrix



#include<stdio.h>
main()
{
int a[10][10],l,s,i,j,p,q;
printf("Enter the order of the matrix\n");
scanf("%d%d",&p,&q);
printf("Enter the values\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&a[i][j]);
}
l=a[0][0];
s=a[0][0];
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
if(l<a[i][j])
l=a[i][j];
if(a[i][j]<s)
s=a[i][j];
}
}
printf("The matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The biggest value = %d\n",l);
printf("The smallest value = %d\n",s);
}