//x power y
#include <stdio.h>
int main()
{
int x, y, i, r=1;
x=3;
y=5;
for(i=1;i<=y;i++)
{
r=r*x;
}
printf("%d power %d is %d\n",x,y,r);
return 0;
}
//x power y - using functions
#include <stdio.h>
int power(int x, int y); //function declaration
int main()
{
int x, y, r;
x=3;
y=5;
r = power(x,y); //function call
printf("%d power %d is %d\n",x,y,r);
return 0;
}
int power(int x, int y) //function definition
{
int i, r=1;
for(i=1;i<=y;i++)
{
r=r*x;
}
return r;
}
//prime number
#include <stdio.h>
int main()
{
int n,i,count=0;
n=7;
for(i=1;i<=n;i++)
{
if(n%i == 0)
{
count++;
}
}
if(count==2)
{
printf("%d is a prime number\n", n);
}
else
{
printf("%d is not a prime number\n", n);
}
return 0;
}
//prime number - using functions - version 2
#include <stdio.h>
int is_prime(int n);
int main()
{
int n, r;
n=9;
r = is_prime(n);
if(r==1)
{
printf("%d is a prime number\n", n);
}
else if(r==0)
{
printf("%d is not a prime number\n", n);
}
return 0;
}
int is_prime(int n)
{
int i,count=0;
for(i=1;i<=n;i++)
{
if(n%i == 0)
{
count++;
}
}
if(count==2)
{
return 1; //prime number
}
else
{
return 0; // not a prime number
}
}
//prime number - using functions - version1
#include <stdio.h>
void is_prime(int n);
int main()
{
int n;
n=7;
is_prime(n);
return 0;
}
void is_prime(int n)
{
int i,count=0;
for(i=1;i<=n;i++)
{
if(n%i == 0)
{
count++;
}
}
if(count==2)
{
printf("%d is a prime number\n", n);
}
else
{
printf("%d is not a prime number\n", n);
}
}
//factorial
#include <stdio.h>
int main()
{
int n, f=1, i;
n=5;
for(i=n;i>0;i--)
{
f=f*i;
}
printf("%d! is %d\n",n,f);
return 0;
}
//factorial - recursion
#include <stdio.h>
int factorial(int n);
int main()
{
int n, f;
n=5;
f= factorial(n);
printf("%d! is %d\n",n,f);
return 0;
}
int factorial(int n)
{
if(n==0 || n==1)
{
return 1;
}
else
{
return n*factorial(n-1);
}
}
//factorial() - functions
#include <stdio.h>
int factorial(int n);
int main()
{
int n, f;
n=5;
f= factorial(n);
printf("%d! is %d\n",n,f);
return 0;
}
int factorial(int n)
{
int f=1, i;
for(i=n;i>0;i--)
{
f=f*i;
}
return f;
}
#include <stdio.h>
int main()
{
int n, s=0, q, r;
n = 505;
while(n!=0)
{
q = n/10;
r = n%10;
s = s + r;
n = q;
}
printf("sum of the digits = %d\n",s);
return 0;
}
//56465 ==> 5+6+4+6+5 ==> 26
Write a program to read roll number and marks of 10 students in 3 subjects. The valid range for roll numbers is 1000-
9999, if the roll number entered is not in this range, the user should be asked to enter again. Calculate total marks of only
those students who get more than or equal to 40 marks in each subject. Count the number of students whose total is more
than 200. Print the roll number of the student who gets the highest total.
#include <stdio.h>
int main()
{
int i, count=0;
struct student_record
{
int rollno;
int maths;
int english;
int science;
int sum;
};
struct student_record s[10];
for(i=0; i<10; i++)
{
while(1)
{
printf("enter roll number for %d student in range 1000 to 9999: \n", i+1);
scanf("%d", &s[i].rollno);
if(s[i].rollno>=1000 && s[i].rollno<=9999)
{
break;
}
else
{
printf("entered roll number is invalid, again ");
}
}
printf("enter Maths Subject marks for %d student: \n", i+1);
scanf("%d", &s[i].maths);
printf("enter English Subject marks for %d student: \n", i+1);
scanf("%d", &s[i].english);
printf("enter Science Subject marks for %d student: \n", i+1);
scanf("%d", &s[i].science);
}
for(i=0 ; i<10; i++)
{
if(s[i].maths>=40 && s[i].english>=40 && s[i].science>=40)
{
s[i].sum = s[i].maths + s[i].english + s[i].science;
}
else
{
s[i].sum = 0;
}
}
printf("Total marks for each student:\n");
for(i=0 ; i<10 ; i++)
{
printf("Roll No = %d, Total marks = %d\n", s[i].rollno, s[i].sum);
}
for(i=0 ; i<10 ; i++)
{
if(s[i].sum>=200)
{
count++;
}
}
printf("No of students score total marks greater than or equal to 200 = %d\n", count);
return 0;
}
Armstrong numbers 100 to 999:
#include<stdio.h>
int cubesum(int );
void PrintArmstrong(void);
void isArmstrong(int );
int main()
{
PrintArmstrong();
return 0;
}
//153 ==> 1 + 125 + 27
void PrintArmstrong(void)
{
int count,n,cube1,num, cube=0;
printf("Armstrong numbers from 100 to 999: ");
for(num=100;num<=999;num++)
{
isArmstrong(num);
}
}
void isArmstrong(int num)
{
int count,n,cube1, cube=0;
cube = cubesum(num);
if(num==cube)
{
printf("%d\t",cube);
}
}
int cubesum(int num)
{
int n,cube1,count, cube=0;
count = num;
while (count!=0)
{
n=count%10;
cube1=(n*n*n);
cube=cube+cube1;
count/=10;
}
return cube;
}
1. C Program to print "Hello World" on the screen.
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
2. fibonacci series
0 1 1 2 3 5 8 13 21 34 55 ......
#include <stdio.h>
int main()
{
int f1, f2, f3;
int n;
int i;
printf("enter no of values in fibonacci series: ");
scanf("%d", &n);
f1 = 0;
f2 = 1;
printf("%d\t%d\t", f1, f2);
f3 = f1 + f2;
printf("%d\t", f3);
for(i=2;i<=n-2; i++)
{
f1 = f2;
f2 = f3;
f3 = f1 + f2;
printf("%d\t", f3);
}
return 0;
}
3. sum of the digits of a number
6753 ==> 6+7+5+3 ==> 21
==> 3+5+7+6 ==> 21
n ==> 6753
10 | 6753 | 675
6750
-----------------
3
6753 ==> 675(n/10) 3 (n%10)
675 ==> 67 5
67 ==> 6 7
6 ==> 0 6
0 ==> stop
45784 ==> 4578 4
4578 ==> 457 8
457 ==> 45 7
45 ==> 4 5
4 ==> 0 4
0 ==> stop
-------------------------------------------
#include <stdio.h>
int main()
{
int n = 45784;
int sum=0;
int q, r;
while(n!=0)
{
q = n/10;
r = n%10;
sum = sum + r;
n = q;
}
printf("sum of the digits = %d\n", sum);
return 0;
}
--------------------------
#include <stdio.h>
int main()
{
int n = 6753;
int sum=0;
int q, r;
for( ; n!=0 ; n=q)
{
q = n/10;
r = n%10;
sum = sum + r;
}
printf("sum of the digits = %d\n", sum);
return 0;
}
4. multiplication of the digits of a number
n ==> 6753
10 | 6753 | 675
6750
-----------------
3
6753 ==> 675(n/10) 3 (n%10)
675 ==> 67 5
67 ==> 6 7
6 ==> 0 6
0 ==> stop
3*5*7*6 ==>
--------------------------------
#include <stdio.h>
int main()
{
int n = 6753;
int m=1;
int q, r;
while(n!=0)
{
q = n/10;
r = n%10;
m = m * r;
n = q;
}
printf("multiplication of the digits = %d\n", m);
return 0;
}
5. armstrong number
Armstrong Number - An Armstrong Number is a Number which is equal to it’s sum of digit’s cube. For example - 153 is an Armstrong number: here 153 = (1*1*1) + (5*5*5) + (3*3*3).
-----------------------
#include <stdio.h>
int main()
{
int n = 153;
int org;
int s=0;
int q, r;
org = n;
while(n!=0)
{
q = n/10;
r = n%10;
s = s + r*r*r;
n = q;
}
if(org == s)
{
printf("armstrong number\n");
}
else
{
printf("not an armstrong number\n");
}
return 0;
}
6. palindrome of a number
n ==> 12321
r ==> 12321
if n and r equal ==> palindrome
-------------------------------------------
#include <stdio.h>
int main()
{
int n = 12321;
int org;
int rev=0;
int q, r;
org = n;
while(n!=0)
{
q = n/10;
r = n%10;
rev = rev*10 + r;
n = q;
}
printf("original number = %d\n", org);
printf("reverse number = %d\n", rev);
if(org == rev)
{
printf("palindrome number\n");
}
else
{
printf("not a palindrome number\n");
}
return 0;
}
8. leap year or not
9. string palindrom
madam
madam
9. sum of the elements of an 1-D array using functions
10. sum of the elements of an 2-D array using functions
11. sum of the elements of an 3-D array using functions
12. sum of the two 2-D arrays using functions (sum of matrices)
13. multiplication of the two 2-D arrays using functions (multiplication of matrices)
14. find max element in an array.
#include <stdio.h>
int main()
{
int x[10]={7,5,8,2,1,6,17,9,30,4};
int i,max;
max = x[0];
for(i=1;i<10;i++)
{
if(x[i]>max)
{
max = x[i];
}
}
printf("max element of an array = %d\n", max);
return 0;
}
15. find min element in an array.