4. C program to add and subtract two integers on user based input:-
#include <stdio.h>
int main() {
int a,b;
int sum =0, sub = 0;
printf("enter two numbers: \n");
scanf("%d %d",&a,&b);
sum = a+b;
sub = a-b;
printf("sum = %d\nsub = %d", sum, sub);
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main() {
int a,b;
int sum =0, sub = 0;
printf("enter two numbers: \n");
scanf("%d %d",&a,&b);
sum = a+b;
sub = a-b;
printf("sum = %d\nsub = %d", sum, sub);
return 0;
}
Programmed by: @miss_rupali
👍2
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;
}