Sample C PROGRAMS

Transcription

Sample C PROGRAMS
Sample C PROGRAMS
Write a C program to accept marks of three subjects, and then
print the total and average marks
1.
#include<stdio.h>
main()
{
int m1, m2, m3, total;
float average;
printf(“Enter marks of three subjects”);
scanf(“%d %d %d”,&m1, &m2, &m3);
total = m1 + m2 + m3;
printf("total marks is %d", total);
average = total/3;
printf("average marks is %d", total);
}
Write a C program to accept a number and display it in octal
and hexadecimal form
2.
#include<stdio.h>
main()
{
int n;
printf(“\nEnter a decimal number:”);
scanf(“%d”,&n);
printf(“\nNumber is %d in decimal, %o in octal, %x in hexadecimal”);
}
Write C program using conditional operator to determine
whether year entered through at the keyboard is a leap year or not
3.
#include<stdio.h>
main()
{
int year;
printf(“Enter an year: ”);
scanf(“%d”,&year);
if (year%4 == 0 && year%100 != 0 || year%400 == 0)
printf(“year is a leap year”);
else
printf(“year is not a leap year”);
}
4. Write a C program to find whether a number is odd or even
#include<stdio.h>
11
main()
{
int n;
printf(“Enter an integer: ”);
scanf(“%d”,&n);
if (n%2 == 0)
printf(“number is even”);
else
printf(“number is odd”);
}
5. Write a C program to find whether number is divisible by 7 or not
#include<stdio.h>
main()
{
int n;
printf(“Enter an integer: ”);
scanf(“%d”,&n);
if (n%7 == 0)
printf(“number is divisible by 7”);
else
printf(“number is not divisible by 7”);
}
6. Write a C program to find the factorial of a number
#include<stdio.h>
main()
{
int n, fact=1;
printf(“Enter a positive integer: ”);
scanf(“%d”,&n);
for (i=1;i<=n;i++)
fact = fact * i;
printf(“\nfactorial of %d is %d”, n,fact);
}
7. Write a C program to find whether a number is prime of not
#include<stdio.h>
main()
{
int i,n, isprime=0;
printf("Enter the number to be checked");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
22
}
}
if(n%i==0)
isprime=-1;
If (isprime == 0 )
printf("number is prime") ;
else
printf("number is not prime");
/* Program : Prime number test*/
#include <stdio.h>
main()
{
int n, count ;
printf (“Enter a number”);
scanf("%d",&n); for(i=2;i<n;i+
+)
{
if(n%i==0)
count++;
}
}
if(count>1)
printf("not prime no.");
else
printf("prime");
8. Write a C program to print sum of digits in 3 digit numbers
#include <stdio.h>
main()
{
int n, u, t, h;
printf(“Enter a 3 digit integer: ”);
scanf(“%d”,&n);
u = n%10;
n = n/10;
t = n%10;
n = n/10;
h = n%10;
printf(“\n Sum of digits : %d“, u+t+h);
}
Write a C program to reverse the three digit number entered as
input through keyboard
9.
#include <stdio.h>
33
main()
{
int n, u, t, h;
printf(“Enter a 3 digit integer: ”);
scanf(“%d”,&n);
u = n%10;
n = n/10;
t = n%10;
n = n/10;
}
h = n%10;
printf(“\nReversed number : %d%d%d“, u,t,h);
10. Write a C program to reverse
digits of a number entered as
input through keyboard
#include <stdio.h>
main()
{
int n, digit;
printf(“Enter a 3 digit integer: ”);
scanf(“%d”,&n);
printf(“\nThe reversed digits are: “);
do
{
digit=n%10;
printf(“%d”,digit);
n = n/10;
}
while (n>0);
printf(“\n”);
}
11. Write a C program using if else ladder to grade student a ccording
to following rules.
Marks
70 to 100
60 to 69
50 to 59
40 to 49
0 to 39
Grade
Distinction
I class
II class
pass class
fail
#include <stdio.h>
main ()
{
int marks ;
printf("Enter marks\n") ;
scanf("%d",&marks);
44
}
if (marks<=100 && marks>=70)
printf ("\n Distinction");
else if (marks>=60)
printf("\n First class");
else if (marks>=50)
printf ("\n second class");
else if (marks>=35)
printf ("\n pass class");
else
printf ("Fail");
12. Write a C program to find sum of series : 1+3+5+7+…+N
#include <stdio.h>
main ()
{
int i,n, sum = 0;
printf("Enter value of n: ");
scanf("%d",&n);
for (i=0;i<=n; i=i+2)
sum = sum + i;
printf ("sum = %d” , sum");
}
13. Write a C program to find sum of series :
0+1+3+6+10+15+21+…upto N numbers
#include <stdio.h>
main ()
{
int i,n, sum = 0,term=0 ;
printf("Enter value of n: ") ;
scanf("%d",&n);
for (i=0;i<=n-1; i++)
{ term = term + i;
sum = sum + term ;
}
printf ("sum = %d” , sum");
}
14. Write a C program to Check the given number is armstrong number or not
#include<stdio.h>
int main()
{
55
int num,r,sum=0,temp;
printf("Enter 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("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
15. Write a C program to print all armstrong numbers in a range.
#include<stdio.h>
int main()
{
int num,r,sum,temp;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Armstrong numbers in given range are: ");
for(num=min;num<=max;num++)
{
temp=num;
sum = 0;
while(temp!=0)
{
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
16. Write a c program to check given string is palindrome number or not.
#include<string.h>
66
#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;
}
17. Write a c program to add two numbers without using addition operator.
#include<stdio.h>
int main()
{
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}
18. Write a C program to generate the Fibonacci Series
#include<stdio.h>
int main()
{
int k,r;
long int i=0l,j=1,f;
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j);
for(k=2;k<r;k++)
{
f=i+j;
77
i=j;
j=f;
printf(" %ld",j);
}
return 0;
}
19. Write a c program to print Floyd’s triangle
#include<stdio.h>
int main()
{
int i,j,r,k=1;
printf("Enter the range: ");
scanf("%d",&r);
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
return 0;
}
20. Write a c program to print Pascal triangle.
#include<stdio.h>
long fact(int);
int main()
{
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i<line;i++)
{
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}
long fact(int num)
{
long f=1;
88
int i=1;
while(i<=num)
{
f=f*i;
i++;
}
return f;
}
21. Write a c program to print multiplication table
#include<stdio.h>
int main()
{
int r,i,j,k;
printf("Enter 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;
}
22. Write a c program to find out NCR factor of given number or C program to
find the ncr value by using recursive function
#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--;
}
99
return i;
}
23. Write a C program for swapping of two numbers
#include<stdio.h>
int main()
{
int a,b,temp;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}
24. Write C program to add two matrices using arrays
#include<stdio.h>
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]);
}
10
10
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;
}
25. Write a C Program to Multiply two Matrices
#include<stdio.h>
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]);
11
11
}
}
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++)
{
for(j=0;j<p;j++)
{
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;
}
26. Write a C program to find transpose of given matrix
#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++)
12
12
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;
}
27. Write a C Program to Concatenate two Strings using Pointer
#include<stdio.h>
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);
13
13
while(*str1)
{
str3[i++]=*str1++;
}
while(*str2)
{
str3[i++]=*str2++;
}
str3[i]='\0';
printf("After concatenation the strings are\n");
puts(str3);
return 0;
}
14
14