Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Monday, 5 November 2018

LESSON 3 Factorial of a number using recursion C Programming

Factorial of a number using recursion

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,f;
clrscr();
printf("Enter the limit");
scanf("%d",&n);
f=fact(n);
printf("factorial is %d",f);
getch();
}
int fact(int x)
{
int p;
if(x==0)
return(1);
else
{
p=x*fact(x-1);
}
return(p);
}

LESSON 4 Checking whether a string palindrome C Programming

Cheking whether a string palindrome

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 int n,i,flag=0,j;
 char a[32];
 clrscr();
 printf("enter the string");
 scanf("%s",a);
 n=strlen(a);
 for(i=0,j=n-1;(i<n)&&(j>0);j--,i++)
  {
     if(a[i]!=a[j])
       flag=1;
  }
     if(flag==1)
     printf("the string is not palindrom");
     else
     printf("the string is palindrome");
     getch();
}

LESSON 5 Product of two matrix C Programming

Product of two matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,i,j,k,a[20][20],b[20][20],c[20][20];
clrscr();
printf("enter the no of rows & columnsof matrix 1\n");
scanf("%d%d",&m,&n);
if(m==0||n==0)
{
printf("matrix is not possible");
}
printf("enter the array elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the no of rows & columnsof matrix 2\n");
scanf("%d%d",&p,&q);
if(p==0||q==0)
{
printf("matrix is not possible");
}
printf("enter the array elements\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("the  matrix 1 is \n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n\n\n");
}
printf("the  matrix 2 is \n\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n\n\n");
}
printf("\n");
if(n==p)
printf("multiplication is possible\n");
else
{
printf("multiplication is not possible");
exit(0);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("product is\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n\n\n");
}
getch();
}

LESSON 6 Details of two students and their three marks m1,m2,m3 C Programming

Details of two students and their three marks m1,m2,m3

#include<stdio.h>
#include<conio.h>
void main()
{
struct stud{
    char name[20];
    int m1;
    int m2;
    int m3;
    int m4;
    int total;
    char grade[20];
    }a[100],temp;
 int n,i,j ;
 clrscr();
 printf("enter no. of students");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
printf("enter details \nname,m1,m2,m3,m4");
    scanf("%s",a[i].name);
    scanf("%d%d%d%d",&a[i].m1,&a[i].m2,&a[i].m3,&a[i].m4);
    a[i].total=a[i].m1+a[i].m2+a[i].m3+a[i].m4;
    }
 for(i=0;i<n;i++)
 {
 for(j=0;j<n-i;j++)
 {
 if(a[j].total<a[j+1].total)
  {
  temp=a[j];
  a[j]=a[j+1];
  a[j+1]=temp;
  }
  }
  }printf("\nname\tm1\tm2\tm3\tm4\ttotal");
 for(i=0;i<n;i++)
 printf(" \n%s\t%d\t%d\t%d\t%d\t%d",a[i].name,a[i].m1,a[i].m2,a[i].m3,a[i].m4,a[i].total);
 getch();
 }

Tuesday, 17 July 2018

LESSON 2 Expansion of sin(x) with a limit(n) C Programming

Expansion of sin(x)  with a limit(n)

#include<stdio.h>
#include<conio.h>
#include<math.h>
int fact(int);
void main()
{
int x,n,k,i;
float s=0;
printf("Enter x and n");
scanf("%d%d",&x,&n);
for(i=0;i<n;i++)
{
k=fact(i*2);
if(i%2==0)
s=s+(float)(pow(x,i*2)/k);
else
s=s-(float)(pow(x,i*2)/k);
}
printf("sum is %f",s);
getch();
}
int fact(int a)
{
int f=1,j;
for(j=1;j<=a;j++)
f=f*j;
return(f);
}

LESSON 1 Prime number up to a limit C Programming

Prime number upto a limit

Here we importing to library (stdio.h)C Standard Input and Output Library for Input and Output operations. and conio is a c header file and not part of c standard library ,it is used MSDOS compilers to console Input/Output

here we have main function and declared n,i,j,k variables with integer properties
for storing the values to n we use %d followed by &n

Next is for loop section
for(i=2;i<n;i++) →means that i=2  to n , we have n value stored in int n variable , so if n is 10 then first it start from i=2 then check 2<10 ie is true so it add 1 to i  so i→3 then goto next for loop
and so on. for printing a variable i we use &d, then followed by i
At last we use getch() function to get a still screen 

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k=0;
clrscr();
printf("Enter the limit");
scanf("%d",&n);
for(i=2;i<n;i++)
 {
  for(j=2;j<i;j++)
  {
  if(i%j==0)
  k++;
  }
  if(k>2)
  printf(" %d",i);
 }
 getch();
}

Facebook Twitter Delicious Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes