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;
}