6. C program to check whether a number is Armstrong number or not:-
#include <stdio.h>
// armstrong number in c
int main() {
int rem, num, sum=0, temp;
printf ("Enter a number: ");
scanf("%d", &num);
temp = num;
while(num > 0){
rem = num % 10;
sum += (rem*rem*rem);
num /= 10;
}
if(temp == sum){
printf ("Armstrong number!");
}
else {
printf ("Not a armstrong number!");
}
return 0;
}
#include <stdio.h>
// armstrong number in c
int main() {
int rem, num, sum=0, temp;
printf ("Enter a number: ");
scanf("%d", &num);
temp = num;
while(num > 0){
rem = num % 10;
sum += (rem*rem*rem);
num /= 10;
}
if(temp == sum){
printf ("Armstrong number!");
}
else {
printf ("Not a armstrong number!");
}
return 0;
}
7. C program to find the next date of menstrual cycle according to months:-
#include <stdio.h>
/*
{
input format: you have to give first number for periods date and second for total days of a month
for example: 24 30 output will be 22
}*/
int main() {
// int month = 31;
int m;
int date;
int nextDate;
printf ("Enter a valid date and month(28 or 31 or 30):");
scanf("%d %d",&date,&m);
if(date >31){
printf ("Enter a valid date\n");
}
else{
switch (m){
case 28:
nextDate = 28+date-m;
break;
case 30:
nextDate = 28+date-m;
break;
case 31:
nextDate = 28+date-m;
break;
default:
printf("Invalid month");
}
}
printf("Next Date = %d",nextDate );
return 0;
}
#include <stdio.h>
/*
{
input format: you have to give first number for periods date and second for total days of a month
for example: 24 30 output will be 22
}*/
int main() {
// int month = 31;
int m;
int date;
int nextDate;
printf ("Enter a valid date and month(28 or 31 or 30):");
scanf("%d %d",&date,&m);
if(date >31){
printf ("Enter a valid date\n");
}
else{
switch (m){
case 28:
nextDate = 28+date-m;
break;
case 30:
nextDate = 28+date-m;
break;
case 31:
nextDate = 28+date-m;
break;
default:
printf("Invalid month");
}
}
printf("Next Date = %d",nextDate );
return 0;
}
👍1
8. Write a program in C to copy a string value from variable to another using user defined functions.
#include <stdio.h>
void copy(char str1[300], char str2[300]){
int i;
for(i =1; str1[i] != '\0'; i++){
str2[i] = str1[i];
}
str2[i] = '\0';
printf("%s is copied into %s",str1, str2);
}
int main()
{
char str1[300];
char str2[300];
printf("Enter firdt string: ");
//for taking a string with white spaces we will use gets() function otherwise we will use scanf() function
// scanf("%s", str1);
gets(str1);
printf("Enter your second string: ");
// scanf("%s", str2);
gets(str2);
copy(str1, str2);
return 0;
}
#include <stdio.h>
void copy(char str1[300], char str2[300]){
int i;
for(i =1; str1[i] != '\0'; i++){
str2[i] = str1[i];
}
str2[i] = '\0';
printf("%s is copied into %s",str1, str2);
}
int main()
{
char str1[300];
char str2[300];
printf("Enter firdt string: ");
//for taking a string with white spaces we will use gets() function otherwise we will use scanf() function
// scanf("%s", str1);
gets(str1);
printf("Enter your second string: ");
// scanf("%s", str2);
gets(str2);
copy(str1, str2);
return 0;
}
9. Write a program in C to accept marks of five papers of five learners in a two-dimensional array and print the average marks scored by each learner. Also identify the total number of learners getting first class, pass class and those failing. First class is when the marks are greater 59, and pass class is when the marks are greater 39.
#include<stdio.h>
int main(){
int students = 5;
int subjects = 5;
float avg;
int sum = 0, pass =0, fail=0, first=0;
int marks[3][5];
for(int i=0; i<students; i++){
for(int j=0; j<subjects; j++){
printf("Enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d",&marks[i][j]);
}
}
for(int i=0; i<students; i++)
{
sum =0;
for(int j=0; j<subjects; j++)
{
printf("The marks of student %d in subject %d is: %d\n", i+1, j+1, marks[i][j]);
sum += marks[i][j];
}
avg = sum / 5.0;
printf("average of marks of student %d is %.2f\n", i+1, avg);
}
if(sum > 59){
first++;
}
if(sum > 39){
pass++;
}
else{
fail++;
}
printf("No. of first class students: %d\n", first);
printf("No. of passed students : %d\n", pass);
printf("No. of fail students : %d\n",fail);
return 0;
}
#include<stdio.h>
int main(){
int students = 5;
int subjects = 5;
float avg;
int sum = 0, pass =0, fail=0, first=0;
int marks[3][5];
for(int i=0; i<students; i++){
for(int j=0; j<subjects; j++){
printf("Enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d",&marks[i][j]);
}
}
for(int i=0; i<students; i++)
{
sum =0;
for(int j=0; j<subjects; j++)
{
printf("The marks of student %d in subject %d is: %d\n", i+1, j+1, marks[i][j]);
sum += marks[i][j];
}
avg = sum / 5.0;
printf("average of marks of student %d is %.2f\n", i+1, avg);
}
if(sum > 59){
first++;
}
if(sum > 39){
pass++;
}
else{
fail++;
}
printf("No. of first class students: %d\n", first);
printf("No. of passed students : %d\n", pass);
printf("No. of fail students : %d\n",fail);
return 0;
}
10. C program to find whether a number is perfect number or not.
#include<stdio.h>
// c code
int main() {
int n,i,s=0;
scanf("%d", &n);
printf("you have entered %d\n",n);
for(i=1; i < n; i++){
if(n % i == 0)
s += i;
}
if(s == n){
printf ("The number %d is a perfect number", n);
}
else {
printf ("The number %d is not a perfect number",n);
}
return 0;
}
#include<stdio.h>
// c code
int main() {
int n,i,s=0;
scanf("%d", &n);
printf("you have entered %d\n",n);
for(i=1; i < n; i++){
if(n % i == 0)
s += i;
}
if(s == n){
printf ("The number %d is a perfect number", n);
}
else {
printf ("The number %d is not a perfect number",n);
}
return 0;
}
11. C program to find whether a number is a palindrome number or not.
#include <stdio.h>
int ispalindrome(int number){
int p,rem,rev=0;
p= number ;
while(number !=0){
rem = number % 10;
rev = rev*10+rem;
number /= 10;
}
printf ("%d\n",rev);
if(p == rev){
printf ("The number you have entered %d is a palindrome number!!\n",p);
return 1;
}
else{
printf ("The number you have entered %d is not a palindrome number!!\n",p);
return 0;
}
}
int main() {
int num;
printf("Enter a number for check, it is palindrome or not\n");
scanf("%d",&num);
ispalindrome (num);
return 0;
}
#include <stdio.h>
int ispalindrome(int number){
int p,rem,rev=0;
p= number ;
while(number !=0){
rem = number % 10;
rev = rev*10+rem;
number /= 10;
}
printf ("%d\n",rev);
if(p == rev){
printf ("The number you have entered %d is a palindrome number!!\n",p);
return 1;
}
else{
printf ("The number you have entered %d is not a palindrome number!!\n",p);
return 0;
}
}
int main() {
int num;
printf("Enter a number for check, it is palindrome or not\n");
scanf("%d",&num);
ispalindrome (num);
return 0;
}
14. C program to find your age.
#include <stdio.h>
#define PRESENT_YEAR 2020
int main() {
printf ("***HOW OLD ARE YOU***\n");
int BORN_YEAR;
int AGE;
printf ("You have entered your born year\n");
scanf("%d",&BORN_YEAR);
if(BORN_YEAR > PRESENT_YEAR ){
printf("invalide number\nplease try again!");
}
AGE = PRESENT_YEAR - BORN_YEAR ;
printf ("This year you will be %d years old\n",AGE );
//To check your birth year was a leal year!
if((BORN_YEAR % 400 == 0) ||((BORN_YEAR % 400 == 0 ) && (BORN_YEAR % 100 !=0))){
printf ("you are borned in a leap year\n ");
}
else{
printf("you are not borned in a leal year\n");
}
return 0;
}
#include <stdio.h>
#define PRESENT_YEAR 2020
int main() {
printf ("***HOW OLD ARE YOU***\n");
int BORN_YEAR;
int AGE;
printf ("You have entered your born year\n");
scanf("%d",&BORN_YEAR);
if(BORN_YEAR > PRESENT_YEAR ){
printf("invalide number\nplease try again!");
}
AGE = PRESENT_YEAR - BORN_YEAR ;
printf ("This year you will be %d years old\n",AGE );
//To check your birth year was a leal year!
if((BORN_YEAR % 400 == 0) ||((BORN_YEAR % 400 == 0 ) && (BORN_YEAR % 100 !=0))){
printf ("you are borned in a leap year\n ");
}
else{
printf("you are not borned in a leal year\n");
}
return 0;
}
15. C program to find the factorial of a given number.
#include <stdio.h>
int factorial(int facto){
if((facto==1) || (facto==0)){
return 1;
}
else{
return facto * factorial(facto-1);
}
}
int main() {
printf("***FACTORIAL***\n\n");
int fact;
printf ("Enter a number for factorial\n");
scanf("%d",&fact);
printf("you have entered %d and its factirial is %d\n",fact,factorial(fact));
return 0;
}
#include <stdio.h>
int factorial(int facto){
if((facto==1) || (facto==0)){
return 1;
}
else{
return facto * factorial(facto-1);
}
}
int main() {
printf("***FACTORIAL***\n\n");
int fact;
printf ("Enter a number for factorial\n");
scanf("%d",&fact);
printf("you have entered %d and its factirial is %d\n",fact,factorial(fact));
return 0;
}
18. C program to find n power of a number.
#include <stdio.h>
int main()
{
int a, b, temp, res=1;
printf("Enter a numbers: ");
scanf("%d", &a);
printf("Enter power of %d: ",a);
scanf("%d",&b);
for(int i =1; i<=b; i++){
temp = a;
res = res * temp;
}
printf("result = %d", res);
return 0;
}
#include <stdio.h>
int main()
{
int a, b, temp, res=1;
printf("Enter a numbers: ");
scanf("%d", &a);
printf("Enter power of %d: ",a);
scanf("%d",&b);
for(int i =1; i<=b; i++){
temp = a;
res = res * temp;
}
printf("result = %d", res);
return 0;
}
20. C program to find multiplication of two matrices.
#include <stdio.h>
int main()
{
printf ("***matrices multiplication***\n");
int a[2][2], b[2][2], c[2][2];
int i, j;
printf("enterthe elements of 1st matric:\n");
for(i = 0;i< 2;i++){
for(j = 0;j< 2;j++)
scanf("%d",&a[i][j]);
}
printf("enter the elements of 2nd matric:\n");
for(i = 0;i< 2;i++){
for(j = 0;j< 2;j++)
scanf("%d",&b[i][j]);
}
printf ("resulted matric..\n");
for(i = 0;i< 2;i++){
for(j = 0;j< 2;j++){
c[i][j]=0;
for(int k=0; k<2; k++){
c[i][j] += a[i][k] * b[k][j];
}
printf ("%d ",c[i][j]);
}
printf ("\n");
}
return 0;
}
i
#include <stdio.h>
int main()
{
printf ("***matrices multiplication***\n");
int a[2][2], b[2][2], c[2][2];
int i, j;
printf("enterthe elements of 1st matric:\n");
for(i = 0;i< 2;i++){
for(j = 0;j< 2;j++)
scanf("%d",&a[i][j]);
}
printf("enter the elements of 2nd matric:\n");
for(i = 0;i< 2;i++){
for(j = 0;j< 2;j++)
scanf("%d",&b[i][j]);
}
printf ("resulted matric..\n");
for(i = 0;i< 2;i++){
for(j = 0;j< 2;j++){
c[i][j]=0;
for(int k=0; k<2; k++){
c[i][j] += a[i][k] * b[k][j];
}
printf ("%d ",c[i][j]);
}
printf ("\n");
}
return 0;
}
i
21. C program to swap two characters.
#include <stdio.h>
void character_swaping(char *a, char *b);
int main()
{
char ch1 = 's';
char ch2 = 'r';
printf("character before swaping:\nch1 = %c ch2 = %c\n",ch1, ch2);
character_swaping(&ch1, &ch2);
printf("character after swaping:\nch1 = %c ch2 = %c\n",ch1, ch2);
return 0;
}
void character_swaping(char *a, char *b){
char temp;
temp = *a;
*a = *b;
*b = temp;
}
#include <stdio.h>
void character_swaping(char *a, char *b);
int main()
{
char ch1 = 's';
char ch2 = 'r';
printf("character before swaping:\nch1 = %c ch2 = %c\n",ch1, ch2);
character_swaping(&ch1, &ch2);
printf("character after swaping:\nch1 = %c ch2 = %c\n",ch1, ch2);
return 0;
}
void character_swaping(char *a, char *b){
char temp;
temp = *a;
*a = *b;
*b = temp;
}
23. C program to convert numbers into roman digit's.
#include<stdio.h>
int rom(int, char);
int roman(int n){
int m = n/1000;
n = n % 1000;
rom(m,'m');
int d = n /500;
n = n % 500;
rom(d,'D');
int c = n /100;
n = n % 100;
rom(c,'c');
int l = n /50;
n = n % 50;
rom(l,'L');
int x = n /10;
n = n % 10;
rom(x,'x');
int v = n /5;
n = n % 5;
rom(v,'v');
int i = n /1;
n = n % 1;
rom(i,'i');
return 0;
}
int rom(int q,char h){
for(int i =0;i<q;i++){
printf ("%c",h);
}
return 0;
}
int main() {
int num;
scanf("%d",&num);
printf("Roman value of %d is: ", num);
roman(num);
return 0;
}
#include<stdio.h>
int rom(int, char);
int roman(int n){
int m = n/1000;
n = n % 1000;
rom(m,'m');
int d = n /500;
n = n % 500;
rom(d,'D');
int c = n /100;
n = n % 100;
rom(c,'c');
int l = n /50;
n = n % 50;
rom(l,'L');
int x = n /10;
n = n % 10;
rom(x,'x');
int v = n /5;
n = n % 5;
rom(v,'v');
int i = n /1;
n = n % 1;
rom(i,'i');
return 0;
}
int rom(int q,char h){
for(int i =0;i<q;i++){
printf ("%c",h);
}
return 0;
}
int main() {
int num;
scanf("%d",&num);
printf("Roman value of %d is: ", num);
roman(num);
return 0;
}
If you've any query or doubt about these programs , kindly DM me , I'll try to solve them.
Thank you..
Happy Coding :)
Thank you..
Happy Coding :)